mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
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
This commit is contained in:
parent
3fa84039f8
commit
9235b52828
@ -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()
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user