Refactor the core config store (#80457)

* Add helper for config Store

* Use internal class
This commit is contained in:
epenet 2022-10-17 10:13:53 +02:00 committed by GitHub
parent d49a223a02
commit bbb0b2a0e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1962,17 +1962,7 @@ class Config:
async def async_load(self) -> None: async def async_load(self) -> None:
"""Load [homeassistant] core config.""" """Load [homeassistant] core config."""
# Circular dep store = self._ConfigStore(self.hass)
# pylint: disable=import-outside-toplevel
from .helpers.storage import Store
store = Store[dict[str, Any]](
self.hass,
CORE_STORAGE_VERSION,
CORE_STORAGE_KEY,
private=True,
atomic_writes=True,
)
if not (data := await store.async_load()): if not (data := await store.async_load()):
return return
@ -2006,10 +1996,6 @@ class Config:
async def async_store(self) -> None: async def async_store(self) -> None:
"""Store [homeassistant] core config.""" """Store [homeassistant] core config."""
# Circular dep
# pylint: disable=import-outside-toplevel
from .helpers.storage import Store
data = { data = {
"latitude": self.latitude, "latitude": self.latitude,
"longitude": self.longitude, "longitude": self.longitude,
@ -2024,11 +2010,22 @@ class Config:
"currency": self.currency, "currency": self.currency,
} }
store: Store[dict[str, Any]] = Store( store = self._ConfigStore(self.hass)
self.hass, await store.async_save(data)
# Circular dependency prevents us from generating the class at top level
# pylint: disable-next=import-outside-toplevel
from .helpers.storage import Store
class _ConfigStore(Store[dict[str, Any]]):
"""Class to help storing Config data."""
def __init__(self, hass: HomeAssistant) -> None:
"""Initialize storage class."""
super().__init__(
hass,
CORE_STORAGE_VERSION, CORE_STORAGE_VERSION,
CORE_STORAGE_KEY, CORE_STORAGE_KEY,
private=True, private=True,
atomic_writes=True, atomic_writes=True,
) )
await store.async_save(data)