From 4faa39a99be95a24b28548fb3b030162d944ae41 Mon Sep 17 00:00:00 2001 From: Benjamin Auquite Date: Wed, 5 Apr 2023 21:31:28 -0500 Subject: [PATCH] add transition_timer test and debug --- custom_components/adaptive_lighting/switch.py | 20 +++++++++++++------ tests/test_switch.py | 19 ++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 347ee4cb..10d05ff4 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -1167,14 +1167,22 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity): if lights is None: lights = self._lights - if not force and self._only_once: - return - filtered_lights = [] - for light in lights: - # Don't adapt lights that haven't finished prior transitions. - if force or not self.turn_on_off_listener.transition_timers.get(light): + if not force: + if self._only_once: + return + for light in lights: + # Don't adapt lights that haven't finished prior transitions. + if self.turn_on_off_listener.transition_timers.get(light): + _LOGGER.debug( + "%s: Light '%s' is still transitioning for %s more seconds", + self._name, + light, + ) + continue filtered_lights.append(light) + else: + filtered_lights = lights if not filtered_lights: return diff --git a/tests/test_switch.py b/tests/test_switch.py index db4900dc..2c60b320 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -638,6 +638,25 @@ async def test_auto_reset_manual_control(hass): assert not manual_control[light.entity_id] +async def test_transition_timers(hass): + switch, (light, *_) = await setup_lights_and_switch() + + async def update(force): + await switch._update_attrs_and_maybe_adapt_lights( + transition=1, + context=switch.create_context("test"), + force=force, + ) + await hass.async_block_till_done() + + _LOGGER.debug("Start test of transition timers") + await update(True) + await asyncio.sleep(0.5) + assert switch.turn_on_off_listener.transition_timers.get(light) + await asyncio.sleep(2) + assert not switch.turn_on_off_listener.transition_timers.get(light) + + async def test_apply_service(hass): """Test adaptive_lighting.apply service.""" switch, (_, _, light) = await setup_lights_and_switch(hass)