Ensure restore state is not written after the stop event (#49329)

If everything lined up, the states could be written
while Home Assistant is shutting down after the stop
event because the interval tracker was not canceled on
the stop event.
This commit is contained in:
J. Nick Koston 2021-04-16 21:03:18 -10:00 committed by GitHub
parent 41ed1f818c
commit f96a6e878f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 60 additions and 4 deletions

View file

@ -177,10 +177,18 @@ class RestoreStateData:
self.hass.async_create_task(_async_dump_states())
# Dump states periodically
async_track_time_interval(self.hass, _async_dump_states, STATE_DUMP_INTERVAL)
cancel_interval = async_track_time_interval(
self.hass, _async_dump_states, STATE_DUMP_INTERVAL
)
async def _async_dump_states_at_stop(*_: Any) -> None:
cancel_interval()
await self.async_dump_states()
# Dump states when stopping hass
self.hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_dump_states)
self.hass.bus.async_listen_once(
EVENT_HOMEASSISTANT_STOP, _async_dump_states_at_stop
)
@callback
def async_restore_entity_added(self, entity_id: str) -> None: