From 92edf8e0145c28aded2ee3f0f346eb6f5bd43acd Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Mon, 31 Jul 2023 14:28:14 -0700 Subject: [PATCH] Track toggle --- custom_components/adaptive_lighting/switch.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index dd7d293f..0a093893 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1478,8 +1478,9 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): ) ): _LOGGER.debug( - "%s: The 'off' → 'on' event for '%s' with context.id='%s'" - " is not associated with a 'light.turn_on' event", + "%s: Ignoring 'off' → 'on' event for '%s' with context.id='%s'" + " because 'light.turn_on' was not called by HA and" + " 'detect_non_ha_changes' is False", self._name, entity_id, event.context.id, @@ -1832,6 +1833,8 @@ class AdaptiveLightingManager: self.turn_off_event: dict[str, Event] = {} # Tracks 'light.turn_on' service calls self.turn_on_event: dict[str, Event] = {} + # Tracks 'light.toggle' service calls + self.toggle_event: dict[str, Event] = {} # Tracks 'on' → 'off' state changes self.on_to_off_event: dict[str, Event] = {} # Tracks 'off' → 'on' state changes @@ -2251,6 +2254,16 @@ class AdaptiveLightingManager: # Restart the auto reset timer timer.start() + elif service == SERVICE_TOGGLE: + _LOGGER.debug( + "Detected an 'light.toggle('%s')' event with context.id='%s' and event.data='%s'", + entity_ids, + event.context.id, + event.data, + ) + for eid in entity_ids: + self.toggle_event[eid] = event + async def state_changed_event_listener(self, event: Event) -> None: """Track 'state_changed' events.""" entity_id = event.data.get(ATTR_ENTITY_ID, "")