mirror of
https://github.com/basnijholt/adaptive-lighting.git
synced 2026-09-13 15:24:03 +02:00
Add auto_reset_time_remaining attribute (#558)
* Add auto_reset_time_remaining attribute * fix attr * Add test
This commit is contained in:
parent
39e9d0e74f
commit
fe7bdd3940
2 changed files with 19 additions and 0 deletions
|
|
@ -1014,6 +1014,12 @@ class AdaptiveSwitch(SwitchEntity, RestoreEntity):
|
||||||
if self.turn_on_off_listener.manual_control.get(light)
|
if self.turn_on_off_listener.manual_control.get(light)
|
||||||
]
|
]
|
||||||
extra_state_attributes.update(self._settings)
|
extra_state_attributes.update(self._settings)
|
||||||
|
timers = self.turn_on_off_listener.auto_reset_manual_control_timers
|
||||||
|
extra_state_attributes["autoreset_time_remaining"] = {
|
||||||
|
light: time
|
||||||
|
for light in self._lights
|
||||||
|
if (timer := timers.get(light)) and (time := timer.remaining_time()) > 0
|
||||||
|
}
|
||||||
return extra_state_attributes
|
return extra_state_attributes
|
||||||
|
|
||||||
def create_context(
|
def create_context(
|
||||||
|
|
@ -2106,3 +2112,10 @@ class _AsyncSingleShotTimer:
|
||||||
if self.task:
|
if self.task:
|
||||||
self.task.cancel()
|
self.task.cancel()
|
||||||
self.callback = None
|
self.callback = None
|
||||||
|
|
||||||
|
def remaining_time(self):
|
||||||
|
"""Return the remaining time before the timer expires."""
|
||||||
|
if self.start_time is not None:
|
||||||
|
elapsed_time = (dt_util.utcnow() - self.start_time).total_seconds()
|
||||||
|
return max(0, self.delay - elapsed_time)
|
||||||
|
return 0
|
||||||
|
|
|
||||||
|
|
@ -692,9 +692,15 @@ async def test_auto_reset_manual_control(hass):
|
||||||
await turn_light(True, brightness=1)
|
await turn_light(True, brightness=1)
|
||||||
await turn_light(True, brightness=10)
|
await turn_light(True, brightness=10)
|
||||||
assert manual_control[light.entity_id]
|
assert manual_control[light.entity_id]
|
||||||
|
assert (
|
||||||
|
switch.extra_state_attributes["autoreset_time_remaining"][light.entity_id] > 0
|
||||||
|
)
|
||||||
await asyncio.sleep(0.3) # Should be enough time for auto reset
|
await asyncio.sleep(0.3) # Should be enough time for auto reset
|
||||||
await update()
|
await update()
|
||||||
assert not manual_control[light.entity_id], (light, manual_control)
|
assert not manual_control[light.entity_id], (light, manual_control)
|
||||||
|
assert (
|
||||||
|
light.entity_id not in switch.extra_state_attributes["autoreset_time_remaining"]
|
||||||
|
)
|
||||||
|
|
||||||
# Do a couple of quick changes and check that light is not reset
|
# Do a couple of quick changes and check that light is not reset
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue