Migrate restore_state helper to use registry loading pattern (#93773)

* Migrate restore_state helper to use registry loading pattern

As more entities have started using restore_state over time, it
has become a startup bottleneck as each entity being added is
creating a task to load restore state data that is already loaded
since it is a singleton

We now use the same pattern as the registry helpers

* fix refactoring error -- guess I am tired

* fixes

* fix tests

* fix more

* fix more

* fix zha tests

* fix zha tests

* comments

* fix error

* add missing coverage

* s/DATA_RESTORE_STATE_TASK/DATA_RESTORE_STATE/g
This commit is contained in:
J. Nick Koston 2023-05-30 20:48:17 -05:00 committed by GitHub
parent b91c6911d9
commit fba826ae9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 147 additions and 86 deletions

View file

@ -47,11 +47,7 @@ from homeassistant.const import (
from homeassistant.core import Context, CoreState, HomeAssistant, State
from homeassistant.exceptions import HomeAssistantError, Unauthorized
from homeassistant.helpers import config_validation as cv, entity_registry as er
from homeassistant.helpers.restore_state import (
DATA_RESTORE_STATE_TASK,
RestoreStateData,
StoredState,
)
from homeassistant.helpers.restore_state import StoredState, async_get
from homeassistant.setup import async_setup_component
from homeassistant.util.dt import utcnow
@ -838,12 +834,9 @@ async def test_restore_idle(hass: HomeAssistant) -> None:
utc_now,
)
data = await RestoreStateData.async_get_instance(hass)
await hass.async_block_till_done()
data = async_get(hass)
await data.store.async_save([stored_state.as_dict()])
# Emulate a fresh load
hass.data.pop(DATA_RESTORE_STATE_TASK)
await data.async_load()
entity = Timer.from_storage(
{
@ -878,12 +871,9 @@ async def test_restore_paused(hass: HomeAssistant) -> None:
utc_now,
)
data = await RestoreStateData.async_get_instance(hass)
await hass.async_block_till_done()
data = async_get(hass)
await data.store.async_save([stored_state.as_dict()])
# Emulate a fresh load
hass.data.pop(DATA_RESTORE_STATE_TASK)
await data.async_load()
entity = Timer.from_storage(
{
@ -922,12 +912,9 @@ async def test_restore_active_resume(hass: HomeAssistant) -> None:
utc_now,
)
data = await RestoreStateData.async_get_instance(hass)
await hass.async_block_till_done()
data = async_get(hass)
await data.store.async_save([stored_state.as_dict()])
# Emulate a fresh load
hass.data.pop(DATA_RESTORE_STATE_TASK)
await data.async_load()
entity = Timer.from_storage(
{
@ -973,12 +960,9 @@ async def test_restore_active_finished_outside_grace(hass: HomeAssistant) -> Non
utc_now,
)
data = await RestoreStateData.async_get_instance(hass)
await hass.async_block_till_done()
data = async_get(hass)
await data.store.async_save([stored_state.as_dict()])
# Emulate a fresh load
hass.data.pop(DATA_RESTORE_STATE_TASK)
await data.async_load()
entity = Timer.from_storage(
{