Enhance error handling when changing a timer's duration (#121786)

* Update remaining before checking duration

* fix comment

* calculation based on transient field

* lint

* remove useless brackets
This commit is contained in:
Nerdix 2024-09-03 19:47:00 +02:00 committed by GitHub
parent 8e03f3a045
commit 7b35c3036e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -338,7 +338,9 @@ class Timer(collection.CollectionEntity, RestoreEntity):
raise HomeAssistantError(
f"Timer {self.entity_id} is not running, only active timers can be changed"
)
if self._remaining and (self._remaining + duration) > self._running_duration:
# Check against new remaining time before checking boundaries
new_remaining = (self._end + duration) - dt_util.utcnow().replace(microsecond=0)
if self._remaining and new_remaining > self._running_duration:
raise HomeAssistantError(
f"Not possible to change timer {self.entity_id} beyond duration"
)
@ -349,7 +351,7 @@ class Timer(collection.CollectionEntity, RestoreEntity):
self._listener()
self._end += duration
self._remaining = self._end - dt_util.utcnow().replace(microsecond=0)
self._remaining = new_remaining
self.async_write_ha_state()
self.hass.bus.async_fire(EVENT_TIMER_CHANGED, {ATTR_ENTITY_ID: self.entity_id})
self._listener = async_track_point_in_utc_time(