Update switch.py

This commit is contained in:
Benjamin Auquite 2023-04-10 05:26:11 -05:00
commit dbc47c8a36

View file

@ -1303,13 +1303,17 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
adapt_brightness = self.adapt_brightness_switch.is_on adapt_brightness = self.adapt_brightness_switch.is_on
adapt_color = self.adapt_color_switch.is_on adapt_color = self.adapt_color_switch.is_on
for light in lights: all_lights = {k: k for k in lights}
if not is_on(self.hass, light): all_lights.update(self._watched_lights)
for wlight, mlight in all_lights:
if not is_on(self.hass, mlight):
continue continue
manually_controlled = self.turn_on_off_listener.is_manually_controlled( manually_controlled = self.turn_on_off_listener.is_manually_controlled(
self, self,
light, wlight,
mlight,
force, force,
adapt_brightness, adapt_brightness,
adapt_color, adapt_color,
@ -1320,7 +1324,8 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
and not force and not force
and await self.turn_on_off_listener.significant_change( and await self.turn_on_off_listener.significant_change(
self, self,
light, wlight,
mlight,
adapt_brightness, adapt_brightness,
adapt_color, adapt_color,
context, context,
@ -1332,13 +1337,15 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
_LOGGER.debug( _LOGGER.debug(
"%s: '%s' is being manually controlled, stop adapting, context.id=%s.", "%s: '%s' is being manually controlled, stop adapting, context.id=%s.",
self._name, self._name,
light, mlight,
context.id, context.id,
) )
else: else:
_fire_manual_control_event(self, light, context) _fire_manual_control_event(self, mlight, context)
else: else:
await self._adapt_light(light, transition, force=force, context=context) await self._adapt_light(
wlight, transition, force=force, context=context
)
async def _sleep_mode_switch_state_event(self, event: Event) -> None: async def _sleep_mode_switch_state_event(self, event: Event) -> None:
if not match_switch_state_event(event, (STATE_ON, STATE_OFF)): if not match_switch_state_event(event, (STATE_ON, STATE_OFF)):
@ -1941,18 +1948,19 @@ class TurnOnOffListener:
def is_manually_controlled( def is_manually_controlled(
self, self,
switch: AdaptiveSwitch, switch: AdaptiveSwitch,
light: str, wlight: str,
mlight: str,
force: bool, force: bool,
adapt_brightness: bool, adapt_brightness: bool,
adapt_color: bool, adapt_color: bool,
) -> bool: ) -> bool:
"""Check if the light has been 'on' and is now manually controlled.""" """Check if the light has been 'on' and is now manually controlled."""
manual_control = self.manual_control.setdefault(light, False) manual_control = self.manual_control.setdefault(wlight, False)
if manual_control: if manual_control:
# Manually controlled until light is turned on and off # Manually controlled until light is turned on and off
return True return True
turn_on_event = self.turn_on_event.get(light) turn_on_event = self.turn_on_event.get(wlight)
if ( if (
turn_on_event is not None turn_on_event is not None
and not is_our_context(turn_on_event.context) and not is_our_context(turn_on_event.context)
@ -1965,13 +1973,13 @@ class TurnOnOffListener:
# Light was already on and 'light.turn_on' was not called by # Light was already on and 'light.turn_on' was not called by
# the adaptive_lighting integration. # the adaptive_lighting integration.
manual_control = True manual_control = True
_fire_manual_control_event(switch, light, turn_on_event.context) _fire_manual_control_event(switch, wlight, turn_on_event.context)
_LOGGER.debug( _LOGGER.debug(
"'%s' was already on and 'light.turn_on' was not called by the" "'%s' was already on and 'light.turn_on' was not called by the"
" adaptive_lighting integration (context.id='%s'), the Adaptive" " adaptive_lighting integration (context.id='%s'), the Adaptive"
" Lighting will stop adapting the light until the switch or the" " Lighting will stop adapting the light until the switch or the"
" light turns off and then on again.", " light turns off and then on again.",
light, mlight,
turn_on_event.context.id, turn_on_event.context.id,
) )
return manual_control return manual_control
@ -1979,7 +1987,8 @@ class TurnOnOffListener:
async def significant_change( async def significant_change(
self, self,
switch: AdaptiveSwitch, switch: AdaptiveSwitch,
light: str, wlight: str,
mlight: str,
adapt_brightness: bool, adapt_brightness: bool,
adapt_color: bool, adapt_color: bool,
context: Context, context: Context,
@ -1991,23 +2000,23 @@ class TurnOnOffListener:
detected, we mark the light as 'manually controlled' until the light detected, we mark the light as 'manually controlled' until the light
or switch is turned 'off' and 'on' again. or switch is turned 'off' and 'on' again.
""" """
last_service_data = self.last_service_data.get(light) last_service_data = self.last_service_data.get(wlight)
if last_service_data is None: if last_service_data is None:
return return
compare_to = functools.partial( compare_to = functools.partial(
_attributes_have_changed, _attributes_have_changed,
light=light, light=wlight,
adapt_brightness=adapt_brightness, adapt_brightness=adapt_brightness,
adapt_color=adapt_color, adapt_color=adapt_color,
context=context, context=context,
) )
if switch._alt_detect_method: if switch._alt_detect_method or wlight != mlight:
old_states: list[State] = self.last_state_change[light] old_states: list[State] = self.last_state_change[wlight]
_LOGGER.debug("Total state changes detected: %s", len(old_states)) _LOGGER.debug("Total state changes detected: %s", len(old_states))
_LOGGER.debug( _LOGGER.debug(
"%s: 'alt_detect_method: true', check all state changes made to light %s", "%s: 'alt_detect_method: true', check all state changes made to light %s",
switch._name, switch._name,
light, wlight,
) )
for index, old_state in enumerate(old_states): for index, old_state in enumerate(old_states):
# The first entry of old_states should always be the # The first entry of old_states should always be the
@ -2029,7 +2038,7 @@ class TurnOnOffListener:
_LOGGER.info( _LOGGER.info(
"Found unexpected state_change event for %s nr. %s (context.id=%s)" "Found unexpected state_change event for %s nr. %s (context.id=%s)"
" old_state=%s\nprior_state=%s", " old_state=%s\nprior_state=%s",
light, wlight,
index, index,
context.id, context.id,
old_state, old_state,
@ -2037,7 +2046,7 @@ class TurnOnOffListener:
) )
_LOGGER.info( _LOGGER.info(
"We will now set %s as manually controlled. (context.id=%s)", "We will now set %s as manually controlled. (context.id=%s)",
light, wlight,
context.id, context.id,
) )
return True return True
@ -2046,20 +2055,22 @@ class TurnOnOffListener:
# light.turn_on calls if any problems arise. This # light.turn_on calls if any problems arise. This
# can happen e.g. using zigbee2mqtt with 'report: false' in device settings. # can happen e.g. using zigbee2mqtt with 'report: false' in device settings.
if switch._detect_non_ha_changes: if switch._detect_non_ha_changes:
if wlight != mlight:
return # only supported with alt_detect_method
_LOGGER.debug( _LOGGER.debug(
"%s: 'detect_non_ha_changes: true', calling update_entity(%s)" "%s: 'detect_non_ha_changes: true', calling update_entity(%s)"
" and check if it's last adapt succeeded.", " and check if it's last adapt succeeded.",
switch._name, switch._name,
light, wlight,
) )
# This update_entity probably isn't necessary now that we're checking # This update_entity probably isn't necessary now that we're checking
# if transitions finished from our last adapt. # if transitions finished from our last adapt.
await self.hass.helpers.entity_component.async_update_entity(light) await self.hass.helpers.entity_component.async_update_entity(wlight)
refreshed_state = self.hass.states.get(light) refreshed_state = self.hass.states.get(wlight)
_LOGGER.debug( _LOGGER.debug(
"%s: Current state of %s: %s", "%s: Current state of %s: %s",
switch._name, switch._name,
light, wlight,
refreshed_state, refreshed_state,
) )
changed = compare_to( changed = compare_to(
@ -2069,7 +2080,7 @@ class TurnOnOffListener:
if changed: if changed:
_LOGGER.debug( _LOGGER.debug(
"State of '%s' didn't change wrt 'last_service_data' (context.id=%s)", "State of '%s' didn't change wrt 'last_service_data' (context.id=%s)",
light, wlight,
context.id, context.id,
) )
return True return True
@ -2077,7 +2088,7 @@ class TurnOnOffListener:
"%s: Light '%s' correctly matches our last adapt's service data, continuing..." "%s: Light '%s' correctly matches our last adapt's service data, continuing..."
" context.id=%s.", " context.id=%s.",
switch._name, switch._name,
light, wlight,
context.id, context.id,
) )
return False return False