Add force_update to timer integration ()

* 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 <lexoid@gmail.com>

* fix lint

* fix isort

Co-authored-by: Alexei Chetroi <lexoid@gmail.com>
pull/33564/head
akasma74 2020-04-03 07:48:41 +01:00 committed by GitHub
parent d2cd557523
commit 83cc871edf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions
homeassistant/components/timer
tests/components/timer

View File

@ -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."""

View File

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