From bc91ff0cc3e730d0d74b7bf30bd5cec3f82c841e Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 8 Jun 2019 09:18:24 +0200 Subject: [PATCH 1/4] Restore automation last_triggered with initial_state override --- .../components/automation/__init__.py | 32 +++++------ tests/components/automation/test_init.py | 54 +++++++++++++++++++ 2 files changed, 71 insertions(+), 15 deletions(-) diff --git a/homeassistant/components/automation/__init__.py b/homeassistant/components/automation/__init__.py index 90b5857b13c563..6c230089990cfc 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -223,23 +223,25 @@ def is_on(self) -> bool: async def async_added_to_hass(self) -> None: """Startup with initial state or previous state.""" await super().async_added_to_hass() + + state = await self.async_get_last_state() + if state: + enable_automation = state.state == STATE_ON + self._last_triggered = state.attributes.get('last_triggered') + _LOGGER.debug("Loaded automation %s with state %s from state " + " storage last state %s", self.entity_id, + enable_automation, state) + else: + enable_automation = DEFAULT_INITIAL_STATE + _LOGGER.debug("Automation %s not in state storage, state %s from " + "default is used.", self.entity_id, + enable_automation) + if self._initial_state is not None: enable_automation = self._initial_state - _LOGGER.debug("Automation %s initial state %s from config " - "initial_state", self.entity_id, enable_automation) - else: - state = await self.async_get_last_state() - if state: - enable_automation = state.state == STATE_ON - self._last_triggered = state.attributes.get('last_triggered') - _LOGGER.debug("Automation %s initial state %s from recorder " - "last state %s", self.entity_id, - enable_automation, state) - else: - enable_automation = DEFAULT_INITIAL_STATE - _LOGGER.debug("Automation %s initial state %s from default " - "initial state", self.entity_id, - enable_automation) + _LOGGER.debug("Automation %s initial state %s overridden from " + "config initial_state", self.entity_id, + enable_automation) if enable_automation: await self.async_enable() diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 81d7a8b257f236..4c3d28cba76321 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -894,3 +894,57 @@ async def test_automation_with_error_in_script(hass, caplog): hass.bus.async_fire('test_event') await hass.async_block_till_done() assert 'Service not found' in caplog.text + +@asyncio.coroutine +def test_automation_restore_last_triggered_with_initial_state(hass): + """Ensure last_triggered is restored, even when initial state is set.""" + time = dt_util.utcnow() + + mock_restore_cache(hass, ( + State('automation.hello', STATE_ON), + State('automation.bye', STATE_ON, {'last_triggered': time}), + State('automation.solong', STATE_OFF, {'last_triggered': time}), + )) + + config = {automation.DOMAIN: [{ + 'alias': 'hello', + 'initial_state': 'off', + 'trigger': { + 'platform': 'event', + 'event_type': 'test_event', + }, + 'action': {'service': 'test.automation'} + }, { + 'alias': 'bye', + 'initial_state': 'off', + 'trigger': { + 'platform': 'event', + 'event_type': 'test_event', + }, + 'action': {'service': 'test.automation'} + }, { + 'alias': 'solong', + 'initial_state': 'on', + 'trigger': { + 'platform': 'event', + 'event_type': 'test_event', + }, + 'action': {'service': 'test.automation'} + }]} + + assert (yield from async_setup_component(hass, automation.DOMAIN, config)) + + state = hass.states.get('automation.hello') + assert state + assert state.state == STATE_OFF + assert state.attributes.get('last_triggered') is None + + state = hass.states.get('automation.bye') + assert state + assert state.state == STATE_OFF + assert state.attributes.get('last_triggered') == time + + state = hass.states.get('automation.solong') + assert state + assert state.state == STATE_ON + assert state.attributes.get('last_triggered') == time From a91db6118936f4bd9770f7bdebcf6cb19bcfa6f3 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 8 Jun 2019 09:32:21 +0200 Subject: [PATCH 2/4] Made test async/await --- tests/components/automation/test_init.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 4c3d28cba76321..c58f1de3ee0ca6 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -895,8 +895,7 @@ async def test_automation_with_error_in_script(hass, caplog): await hass.async_block_till_done() assert 'Service not found' in caplog.text -@asyncio.coroutine -def test_automation_restore_last_triggered_with_initial_state(hass): +async def test_automation_restore_last_triggered_with_initial_state(hass): """Ensure last_triggered is restored, even when initial state is set.""" time = dt_util.utcnow() @@ -932,7 +931,7 @@ def test_automation_restore_last_triggered_with_initial_state(hass): 'action': {'service': 'test.automation'} }]} - assert (yield from async_setup_component(hass, automation.DOMAIN, config)) + await async_setup_component(hass, automation.DOMAIN, config) state = hass.states.get('automation.hello') assert state From fdf4161eb8edefe9b8e08d9487af804f5d031f86 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 8 Jun 2019 09:53:29 +0200 Subject: [PATCH 3/4] Fixes linter warning --- tests/components/automation/test_init.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index c58f1de3ee0ca6..46cf2a6fc3a00c 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -895,6 +895,7 @@ async def test_automation_with_error_in_script(hass, caplog): await hass.async_block_till_done() assert 'Service not found' in caplog.text + async def test_automation_restore_last_triggered_with_initial_state(hass): """Ensure last_triggered is restored, even when initial state is set.""" time = dt_util.utcnow() From 8ba5d499c5a730ceb40b8324a44add77f7970f57 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Sat, 8 Jun 2019 12:48:17 -0700 Subject: [PATCH 4/4] Update test_init.py --- tests/components/automation/test_init.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/components/automation/test_init.py b/tests/components/automation/test_init.py index 46cf2a6fc3a00c..f8748b20efb0d8 100644 --- a/tests/components/automation/test_init.py +++ b/tests/components/automation/test_init.py @@ -937,14 +937,14 @@ async def test_automation_restore_last_triggered_with_initial_state(hass): state = hass.states.get('automation.hello') assert state assert state.state == STATE_OFF - assert state.attributes.get('last_triggered') is None + assert state.attributes['last_triggered'] is None state = hass.states.get('automation.bye') assert state assert state.state == STATE_OFF - assert state.attributes.get('last_triggered') == time + assert state.attributes['last_triggered'] == time state = hass.states.get('automation.solong') assert state assert state.state == STATE_ON - assert state.attributes.get('last_triggered') == time + assert state.attributes['last_triggered'] == time