From fc54a02f051027f4af3b7500e53da82f458cf16a Mon Sep 17 00:00:00 2001 From: akasma74 Date: Sat, 8 Feb 2020 21:39:13 +0000 Subject: [PATCH 1/7] 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 --- homeassistant/components/timer/__init__.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/homeassistant/components/timer/__init__.py b/homeassistant/components/timer/__init__.py index abf3a6ab0f7949..d4ca003a105575 100644 --- a/homeassistant/components/timer/__init__.py +++ b/homeassistant/components/timer/__init__.py @@ -200,6 +200,13 @@ def from_yaml(cls, config: typing.Dict) -> "Timer": def should_poll(self): """If entity should be polled.""" return False + + @property + def force_update(self) -> bool: + """Return True to get last_changed attribute updated + when active timer restarted. + """ + return True @property def name(self): From 2e304b389412eeafc53651e4f76d9f14249710a2 Mon Sep 17 00:00:00 2001 From: akasma74 Date: Fri, 3 Apr 2020 00:48:53 +0100 Subject: [PATCH 2/7] add test for force_update make sure state_change event fired every time timer (re)started --- tests/components/timer/test_init.py | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/components/timer/test_init.py b/tests/components/timer/test_init.py index dcf9c36474fcad..12a799b53e4447 100644 --- a/tests/components/timer/test_init.py +++ b/tests/components/timer/test_init.py @@ -34,6 +34,7 @@ ATTR_NAME, CONF_ENTITY_ID, SERVICE_RELOAD, + EVENT_STATE_CHANGED ) from homeassistant.core import Context, CoreState from homeassistant.exceptions import Unauthorized @@ -405,7 +406,48 @@ def fake_event_listener(event): assert results[-1].event_type == EVENT_TIMER_RESTARTED 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() From 7e5a50e147c4c74a01591701edb07af5960a68c5 Mon Sep 17 00:00:00 2001 From: akasma74 Date: Fri, 3 Apr 2020 01:08:18 +0100 Subject: [PATCH 3/7] remove whitespaces --- tests/components/timer/test_init.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/components/timer/test_init.py b/tests/components/timer/test_init.py index 12a799b53e4447..a82497381915dd 100644 --- a/tests/components/timer/test_init.py +++ b/tests/components/timer/test_init.py @@ -406,7 +406,7 @@ def fake_event_listener(event): assert results[-1].event_type == EVENT_TIMER_RESTARTED 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 @@ -447,7 +447,7 @@ def fake_event_listener(event): 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() From 4fec034c380f5b8c6480305e662132ef8ce78dba Mon Sep 17 00:00:00 2001 From: akasma74 Date: Fri, 3 Apr 2020 01:14:17 +0100 Subject: [PATCH 4/7] remove whitespace --- homeassistant/components/timer/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/components/timer/__init__.py b/homeassistant/components/timer/__init__.py index d4ca003a105575..7859a03cbd6b42 100644 --- a/homeassistant/components/timer/__init__.py +++ b/homeassistant/components/timer/__init__.py @@ -200,7 +200,7 @@ def from_yaml(cls, config: typing.Dict) -> "Timer": def should_poll(self): """If entity should be polled.""" return False - + @property def force_update(self) -> bool: """Return True to get last_changed attribute updated From 077e63f7b595d4d86ac7ac3dbf1ef73d55bd8f15 Mon Sep 17 00:00:00 2001 From: akasma74 Date: Fri, 3 Apr 2020 01:33:42 +0100 Subject: [PATCH 5/7] Update tests/components/timer/test_init.py Co-Authored-By: Alexei Chetroi --- tests/components/timer/test_init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/timer/test_init.py b/tests/components/timer/test_init.py index a82497381915dd..1951f5f928759b 100644 --- a/tests/components/timer/test_init.py +++ b/tests/components/timer/test_init.py @@ -34,7 +34,7 @@ ATTR_NAME, CONF_ENTITY_ID, SERVICE_RELOAD, - EVENT_STATE_CHANGED + EVENT_STATE_CHANGED, ) from homeassistant.core import Context, CoreState from homeassistant.exceptions import Unauthorized From 61bcd23dba90068cc426582097d755a11eebde5d Mon Sep 17 00:00:00 2001 From: akasma74 Date: Fri, 3 Apr 2020 01:33:59 +0100 Subject: [PATCH 6/7] fix lint --- homeassistant/components/timer/__init__.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/homeassistant/components/timer/__init__.py b/homeassistant/components/timer/__init__.py index 7859a03cbd6b42..3e2b7117313e25 100644 --- a/homeassistant/components/timer/__init__.py +++ b/homeassistant/components/timer/__init__.py @@ -203,9 +203,7 @@ def should_poll(self): @property def force_update(self) -> bool: - """Return True to get last_changed attribute updated - when active timer restarted. - """ + """Return True to fix restart issues.""" return True @property From ca5fefb084fb516eabcc293ebb0541e5f3b1e011 Mon Sep 17 00:00:00 2001 From: akasma74 Date: Fri, 3 Apr 2020 01:46:49 +0100 Subject: [PATCH 7/7] fix isort --- tests/components/timer/test_init.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/components/timer/test_init.py b/tests/components/timer/test_init.py index 1951f5f928759b..dea116b3905a67 100644 --- a/tests/components/timer/test_init.py +++ b/tests/components/timer/test_init.py @@ -33,8 +33,8 @@ ATTR_ID, ATTR_NAME, CONF_ENTITY_ID, - SERVICE_RELOAD, EVENT_STATE_CHANGED, + SERVICE_RELOAD, ) from homeassistant.core import Context, CoreState from homeassistant.exceptions import Unauthorized