From 5789aa1ef1cccd3a6e916c2307123d92778083a6 Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 13 Jan 2026 02:45:23 -0800 Subject: [PATCH] fix: restore original order of checks in just_turned_off MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous commit incorrectly moved the _off_to_on_state_event_is_from_turn_on check to before the context ID match check, which broke test_separate_turn_on_commands. The original order must be: 1. Check on_to_off_event is None → return False 2. Check context IDs match → return True (polling artifact) 3. Get transition info 4. Check _off_to_on_state_event_is_from_turn_on → return False This commit restores that order while keeping the light group detection logic in _off_to_on_state_event_is_from_turn_on. --- custom_components/adaptive_lighting/switch.py | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 66aeb5c8..921d2c0d 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -2781,21 +2781,6 @@ class AdaptiveLightingManager: ) return False - # Check if the off→on state change was triggered by a light.turn_on call. - # This check now also handles light groups: if a member light was turned on - # after the group turned off, the group's turn-on is considered valid. - # See: https://github.com/basnijholt/adaptive-lighting/issues/1378 - if self._off_to_on_state_event_is_from_turn_on(entity_id, off_to_on_event): - is_toggle = off_to_on_event == self.toggle_event.get(entity_id) - from_service = "light.toggle" if is_toggle else "light.turn_on" - _LOGGER.debug( - "just_turned_off: State change 'off' → 'on' triggered by '%s'", - from_service, - ) - return False - - # If context IDs match but no turn_on event was found, this is likely a polling - # artifact (HA briefly seeing the light as ON during a turn_off transition). if off_to_on_event.context.id == on_to_off_event.context.id: _LOGGER.debug( "just_turned_off: 'on' → 'off' state change has the same context.id as the" @@ -2812,6 +2797,19 @@ class AdaptiveLightingManager: else: transition = None + # Check if the off→on state change was triggered by a light.turn_on call. + # This check now also handles light groups: if a member light was turned on + # after the group turned off, the group's turn-on is considered valid. + # See: https://github.com/basnijholt/adaptive-lighting/issues/1378 + if self._off_to_on_state_event_is_from_turn_on(entity_id, off_to_on_event): + is_toggle = off_to_on_event == self.toggle_event.get(entity_id) + from_service = "light.toggle" if is_toggle else "light.turn_on" + _LOGGER.debug( + "just_turned_off: State change 'off' → 'on' triggered by '%s'", + from_service, + ) + return False + if ( turn_off_event is not None and id_on_to_off == turn_off_event.context.id