From 9235b5282860abc4136c45f3794d84b7b6971d25 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sat, 8 Jun 2019 21:48:37 +0200 Subject: [PATCH] Restore automation last_triggered with initial_state override (#24400) * Restore automation last_triggered with initial_state override * Made test async/await * Fixes linter warning * Update test_init.py --- .../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 90b5857b13c..6c230089990 100644 --- a/homeassistant/components/automation/__init__.py +++ b/homeassistant/components/automation/__init__.py @@ -223,23 +223,25 @@ class AutomationEntity(ToggleEntity, RestoreEntity): 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 81d7a8b257f..f8748b20efb 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 + + +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() + + 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'} + }]} + + await async_setup_component(hass, automation.DOMAIN, config) + + state = hass.states.get('automation.hello') + assert state + assert state.state == STATE_OFF + assert state.attributes['last_triggered'] is None + + state = hass.states.get('automation.bye') + assert state + assert state.state == STATE_OFF + assert state.attributes['last_triggered'] == time + + state = hass.states.get('automation.solong') + assert state + assert state.state == STATE_ON + assert state.attributes['last_triggered'] == time