From bd6e72403097be6fe3b0cb7b4bf5a75d0b2dfbef Mon Sep 17 00:00:00 2001 From: Bas Nijholt Date: Tue, 8 Sep 2026 05:06:26 -0700 Subject: [PATCH] Normalize turn-off transitions during light recovery --- custom_components/adaptive_lighting/switch.py | 2 +- tests/test_switch.py | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index e951f6b3..49252ce1 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -2948,7 +2948,7 @@ class AdaptiveLightingManager: turn_off = self.turn_off_event.get(entity_id) if turn_off is None: return False - transition = turn_off.data[ATTR_SERVICE_DATA].get(ATTR_TRANSITION) + transition = _turn_off_transition(turn_off) elapsed = (dt_util.utcnow() - turn_off.time_fired).total_seconds() if not 0 <= elapsed <= max(transition or 0, TURNING_OFF_DELAY): return False diff --git a/tests/test_switch.py b/tests/test_switch.py index 138004ee..8740992b 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -6922,6 +6922,31 @@ async def test_recovery_revalidates_after_delay(hass, monkeypatch, freezer, chan await hass.async_block_till_done() +@pytest.mark.parametrize( + ("transition", "window"), + [(10, 10), ("10", 10), (10000, 6553), ("10000", 6553), ("inf", 6553), (None, 5)], +) +async def test_recovery_normalizes_turn_off_transition( + hass, + cleanup, + transition, + window, +): + """Recovery suppression ends after the light service's normalized window.""" + switch, _ = await setup_lights_and_switch(hass) + manager = switch.manager + manager.turn_on_event.pop(ENTITY_LIGHT_1, None) + now = dt_util.utcnow().timestamp() + for elapsed, expected in [(window - 1, True), (window + 1, False)]: + manager.turn_off_event[ENTITY_LIGHT_1] = _turn_off_service_event( + [ENTITY_LIGHT_1], + now - elapsed, + Context(), + transition, + ) + assert manager.recovery_is_during_turn_off(ENTITY_LIGHT_1) is expected + + async def test_recovery_preserves_active_transition(hass, cleanup): """Availability does not reset a transition that still suppresses adaptation.""" switch, _ = await setup_lights_and_switch(hass, {CONF_DETECT_NON_HA_CHANGES: False})