From f7c8e0dccab4d45197acca94a734b12c3a5cb8ed Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 13 Jan 2026 03:15:27 -0800 Subject: [PATCH] fix: check member turn_on before blocking on context ID match MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The context ID match check at line 2804 was returning True (blocking adaptation) before checking if a member light's turn_on event explained the group's turn-on. This caused the issue where light groups wouldn't adapt when a member was turned on by automation. The fix adds a check for _member_turn_on_explains_group_turn_on() inside the context ID match block. If a member's turn_on event happened after the group's on→off event, the group's turn-on is legitimate and not a polling artifact, so we return False (allow adaptation). This is a more targeted fix than reordering the checks, which broke test_separate_turn_on_commands. By keeping the context ID check first but adding the member check inside it, we: 1. Preserve the original polling artifact detection for non-groups 2. Properly handle light groups where HA reuses context IDs Fixes: #1378 --- custom_components/adaptive_lighting/switch.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 1fdf509a..eded357a 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -2774,7 +2774,7 @@ class AdaptiveLightingManager: # For light groups: check if a member's turn_on explains the group's turn-on return self._member_turn_on_explains_group_turn_on(entity_id) - async def just_turned_off( # noqa: PLR0911 + async def just_turned_off( # noqa: PLR0911, PLR0912 self, entity_id: str, ) -> bool: @@ -2802,6 +2802,18 @@ class AdaptiveLightingManager: return False if off_to_on_event.context.id == on_to_off_event.context.id: + # For light groups: check if a member's turn_on explains the group's turn_on. + # If so, this is NOT a polling artifact - it's a legitimate turn-on. + # HA may reuse the turn_off context for the group's state change when a + # member is turned on, which would incorrectly trigger this check. + # See: https://github.com/basnijholt/adaptive-lighting/issues/1378 + if self._member_turn_on_explains_group_turn_on(entity_id): + _LOGGER.debug( + "just_turned_off: Context IDs match for '%s' but a member light was " + "turned on, so this is a legitimate turn-on, not a polling artifact.", + entity_id, + ) + return False _LOGGER.debug( "just_turned_off: 'on' → 'off' state change has the same context.id as the" " 'off' → 'on' state change for '%s'. This is probably a false positive.",