adaptive-lighting/custom_components/adaptive_lighting
Bas Nijholt b7ad2cdd35 Fix light groups not adapting when child lights turned on (#1378)
## 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
2026-01-13 00:34:10 -08:00
..
translations Fix regression: lights not adapting when turned on by automation (#1380) 2026-01-12 13:40:55 +01:00
__init__.py Code cleanup (#1348) 2025-12-12 13:37:42 -08:00
_docs_helpers.py Individual manual control of brightness and color (#1356) 2025-12-22 22:55:16 -08:00
adaptation_utils.py Individual manual control of brightness and color (#1356) 2025-12-22 22:55:16 -08:00
color_and_brightness.py Code cleanup (#1348) 2025-12-12 13:37:42 -08:00
config_flow.py Code cleanup (#1348) 2025-12-12 13:37:42 -08:00
const.py Fix regression: lights not adapting when turned on by automation (#1380) 2026-01-12 13:40:55 +01:00
docs_gen.py Add documentation site with Zensical framework (#1385) 2026-01-12 23:39:57 +01:00
hass_utils.py Friendly names (#1258) 2025-11-27 12:01:05 -08:00
helpers.py Friendly names (#1258) 2025-11-27 12:01:05 -08:00
manifest.json Bump to v1.30.1 (#1384) 2026-01-12 13:46:19 +01:00
services.yaml Individual manual control of brightness and color (#1356) 2025-12-22 22:55:16 -08:00
strings.json Fix regression: lights not adapting when turned on by automation (#1380) 2026-01-12 13:40:55 +01:00
switch.py Fix light groups not adapting when child lights turned on (#1378) 2026-01-13 00:34:10 -08:00