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()