Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
basnijholt committed Aug 6, 2023
1 parent 58b3d65 commit 5d1ba17
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions custom_components/adaptive_lighting/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
ATTR_ADAPT_COLOR,
ATTR_ADAPTIVE_LIGHTING_MANAGER,
CONF_ADAPT_DELAY,
CONF_ADAPT_ONLY_ON_BARE_TURN_ON,
CONF_ADAPT_UNTIL_SLEEP,
CONF_AUTORESET_CONTROL,
CONF_BRIGHTNESS_MODE,
Expand Down Expand Up @@ -888,6 +889,7 @@ def _set_changeable_settings(
self._auto_reset_manual_control_time = data[CONF_AUTORESET_CONTROL]
self._skip_redundant_commands = data[CONF_SKIP_REDUNDANT_COMMANDS]
self._multi_light_intercept = data[CONF_MULTI_LIGHT_INTERCEPT]
self._adapt_only_on_bare_turn_on = data[CONF_ADAPT_ONLY_ON_BARE_TURN_ON]
self._expand_light_groups() # updates manual control timers
location, _ = get_astral_location(self.hass)

Expand Down Expand Up @@ -1446,13 +1448,14 @@ async def _update_attrs_and_maybe_adapt_lights(

async def _respond_to_off_to_on_event(self, entity_id: str, event: Event) -> None:
assert not self.manager.is_proactively_adapting(event.context.id)
from_turn_on = self.manager._off_to_on_state_event_is_from_turn_on(
entity_id,
event,
)
if (
self._take_over_control
and not self._detect_non_ha_changes
and not self.manager._off_to_on_state_event_is_from_turn_on(
entity_id,
event,
)
and not from_turn_on
):
# There is an edge case where 2 switches control the same light, e.g.,
# one for brightness and one for color. Now we will mark both switches
Expand All @@ -1468,6 +1471,22 @@ async def _respond_to_off_to_on_event(self, entity_id: str, event: Event) -> Non
self.manager.mark_as_manual_control(entity_id)
return

# TODO: do I need to do this in the proactively adapting case too or only there?
if (
self._take_over_control
and self._adapt_only_on_bare_turn_on
and from_turn_on
):
service_data = self.turn_on_event[entity_id].data[ATTR_SERVICE_DATA]
if any(attr in service_data for attr in COLOR_ATTRS | BRIGHTNESS_ATTRS):
_LOGGER.debug(
"Skipping responding to 'off' → 'on' event for '%s' with context.id='%s' because"
" we only adapt on bare `light.turn_on` events",
entity_id,
event.context.id,
)
return

if self._adapt_delay > 0:
await asyncio.sleep(self._adapt_delay)

Expand Down Expand Up @@ -2570,8 +2589,6 @@ async def state_changed_event_listener(self, event: Event) -> None:
elif old_off and new_on:
# Tracks 'off' → 'on' state changes

if False:
...
self.off_to_on_event[entity_id] = event
_LOGGER.debug(
"Detected an 'off' → 'on' event for '%s' with context.id='%s'",
Expand Down

0 comments on commit 5d1ba17

Please sign in to comment.