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 <lexoid@gmail.com> * fix lint * fix isort Co-authored-by: Alexei Chetroi <lexoid@gmail.com>pull/33564/head
parent
d2cd557523
commit
83cc871edf
homeassistant/components/timer
tests/components/timer
|
@ -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."""
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Reference in New Issue