From 83cc871edfb49850e686e65969c28e3004a05ea6 Mon Sep 17 00:00:00 2001 From: akasma74 Date: Fri, 3 Apr 2020 07:48:41 +0100 Subject: [PATCH] Add force_update to timer integration (#31646) * force_update added As per this discussion we need to update last_changed when active timer restarted. One way to do that is to force HA update the state on each request even if it remains the same. More details here - https://github.com/home-assistant/architecture/issues/345 * add test for force_update make sure state_change event fired every time timer (re)started * remove whitespaces * remove whitespace * Update tests/components/timer/test_init.py Co-Authored-By: Alexei Chetroi * fix lint * fix isort Co-authored-by: Alexei Chetroi --- homeassistant/components/timer/__init__.py | 5 +++ tests/components/timer/test_init.py | 42 ++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/homeassistant/components/timer/__init__.py b/homeassistant/components/timer/__init__.py index 5172322a63d..e47ac69be5b 100644 --- a/homeassistant/components/timer/__init__.py +++ b/homeassistant/components/timer/__init__.py @@ -201,6 +201,11 @@ class Timer(RestoreEntity): """If entity should be polled.""" return False + @property + def force_update(self) -> bool: + """Return True to fix restart issues.""" + return True + @property def name(self): """Return name of the timer.""" diff --git a/tests/components/timer/test_init.py b/tests/components/timer/test_init.py index dcf9c36474f..dea116b3905 100644 --- a/tests/components/timer/test_init.py +++ b/tests/components/timer/test_init.py @@ -33,6 +33,7 @@ from homeassistant.const import ( ATTR_ID, ATTR_NAME, CONF_ENTITY_ID, + EVENT_STATE_CHANGED, SERVICE_RELOAD, ) from homeassistant.core import Context, CoreState @@ -406,6 +407,47 @@ async def test_timer_restarted_event(hass): assert len(results) == 4 +async def test_state_changed_when_timer_restarted(hass): + """Ensure timer's state changes when it restarted.""" + hass.state = CoreState.starting + + await async_setup_component(hass, DOMAIN, {DOMAIN: {"test1": {CONF_DURATION: 10}}}) + + state = hass.states.get("timer.test1") + assert state + assert state.state == STATUS_IDLE + + results = [] + + def fake_event_listener(event): + """Fake event listener for trigger.""" + results.append(event) + + hass.bus.async_listen(EVENT_STATE_CHANGED, fake_event_listener) + + await hass.services.async_call( + DOMAIN, SERVICE_START, {CONF_ENTITY_ID: "timer.test1"} + ) + await hass.async_block_till_done() + state = hass.states.get("timer.test1") + assert state + assert state.state == STATUS_ACTIVE + + assert results[-1].event_type == EVENT_STATE_CHANGED + assert len(results) == 1 + + await hass.services.async_call( + DOMAIN, SERVICE_START, {CONF_ENTITY_ID: "timer.test1"} + ) + await hass.async_block_till_done() + state = hass.states.get("timer.test1") + assert state + assert state.state == STATUS_ACTIVE + + assert results[-1].event_type == EVENT_STATE_CHANGED + assert len(results) == 2 + + async def test_load_from_storage(hass, storage_setup): """Test set up from storage.""" assert await storage_setup()