Fix timer.cancel action fires timer.cancelled event even on canceled timers (#134507)

* Fixes https://github.com/home-assistant/core/issues/116105

* Fixes unit test in accordance to documentation

Timer needs to be active before it can be canceled

* Allow canceling of paused timers

* Add test for canceling/finishing already canceled/finished timers

* Add test for finishing a paused timer, this should not be possible

* Revert finish related tests

* Merge branch 'timer.cancelled_fix' of
git@github.com:rrooggiieerr/homeassistant-core.git into
timer.cancelled_fix

---------

Co-authored-by: Franck Nijhof <git@frenck.dev>
pull/137856/head^2
rrooggiieerr 2025-02-11 22:08:18 +01:00 committed by GitHub
parent 8d5f927b42
commit 0ffbe076be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 0 deletions

View File

@ -374,6 +374,9 @@ class Timer(collection.CollectionEntity, RestoreEntity):
@callback
def async_cancel(self) -> None:
"""Cancel a timer."""
if self._state == STATUS_IDLE:
return
if self._listener:
self._listener()
self._listener = None

View File

@ -196,6 +196,12 @@ async def test_methods_and_events(hass: HomeAssistant) -> None:
"event": EVENT_TIMER_CANCELLED,
"data": {},
},
{
"call": SERVICE_CANCEL,
"state": STATUS_IDLE,
"event": None,
"data": {},
},
{
"call": SERVICE_START,
"state": STATUS_ACTIVE,