mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-27 20:34:20 +02:00
## Summary When a parent light group is turned off and then child lights are turned on by an automation (e.g., motion sensor), Home Assistant may reuse the old turn_off context ID for the parent group's state change. This caused `just_turned_off()` to incorrectly treat the group's turn-on as a "false positive" polling artifact, blocking adaptation. ## Root Cause The `just_turned_off()` function checked if the off→on context ID matched the on→off context ID. If they matched, it assumed this was a polling artifact (HA briefly seeing the light as ON during a turn_off transition) and cancelled adaptation. However, for light groups, HA reuses the context ID when the parent group turns on as a side effect of child lights turning on. This is a valid turn-on that should be adapted. ## Fix Use causality-based detection instead of just context ID matching: 1. Enhanced `_off_to_on_state_event_is_from_turn_on()` to check if any member light of a group has a `turn_on_event` that happened after the group's on→off event. If so, the member's turn_on explains why the group turned on. 2. Restructured `just_turned_off()` to check for turn_on events BEFORE checking for matching context IDs. Only treat matching context IDs as a false positive if no turn_on event explains the state change. ## Why This is Robust | Old Approach | New Approach | |--------------|--------------| | Magic 1-second threshold | Actual causal relationship | | Fails with different timing | Works regardless of timing | | No explanation in logs | Clear log: "group turned on because member X was turned on" | ## Test Plan - [x] Added test for light group context reuse scenario - [x] Added test to verify polling artifacts are still detected |
||
|---|---|---|
| .. | ||
| translations | ||
| __init__.py | ||
| _docs_helpers.py | ||
| adaptation_utils.py | ||
| color_and_brightness.py | ||
| config_flow.py | ||
| const.py | ||
| docs_gen.py | ||
| hass_utils.py | ||
| helpers.py | ||
| manifest.json | ||
| services.yaml | ||
| strings.json | ||
| switch.py | ||