Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions homeassistant/components/automation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
54 changes: 54 additions & 0 deletions tests/components/automation/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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