fix: check member turn_on before blocking on context ID match

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
This commit is contained in:
Bas Nijholt 2026-01-13 03:15:27 -08:00
commit f7c8e0dcca

View file

@ -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.",