Make Store a generic class (#74617)

This commit is contained in:
epenet 2022-07-09 22:32:57 +02:00 committed by GitHub
parent d37ad20894
commit 16900dcef1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 106 additions and 97 deletions

View file

@ -2,7 +2,7 @@
from __future__ import annotations
from typing import Any, TypedDict, cast
from typing import Any, TypedDict
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.storage import Store
@ -46,7 +46,9 @@ class EntityMapStorage:
def __init__(self, hass: HomeAssistant) -> None:
"""Create a new entity map store."""
self.hass = hass
self.store = Store(hass, ENTITY_MAP_STORAGE_VERSION, ENTITY_MAP_STORAGE_KEY)
self.store = Store[StorageLayout](
hass, ENTITY_MAP_STORAGE_VERSION, ENTITY_MAP_STORAGE_KEY
)
self.storage_data: dict[str, Pairing] = {}
async def async_initialize(self) -> None:
@ -55,8 +57,7 @@ class EntityMapStorage:
# There is no cached data about HomeKit devices yet
return
storage = cast(StorageLayout, raw_storage)
self.storage_data = storage.get("pairings", {})
self.storage_data = raw_storage.get("pairings", {})
def get_map(self, homekit_id: str) -> Pairing | None:
"""Get a pairing cache item."""
@ -87,6 +88,6 @@ class EntityMapStorage:
self.store.async_delay_save(self._data_to_save, ENTITY_MAP_SAVE_DELAY)
@callback
def _data_to_save(self) -> dict[str, Any]:
def _data_to_save(self) -> StorageLayout:
"""Return data of entity map to store in a file."""
return {"pairings": self.storage_data}
return StorageLayout(pairings=self.storage_data)