1 """Helpers for HomeKit data stored in HA storage."""
3 from __future__
import annotations
8 from aiohomekit.characteristic_cache
import Pairing, StorageLayout
13 from .const
import DOMAIN, ENTITY_MAP
15 ENTITY_MAP_STORAGE_KEY = f
"{DOMAIN}-entity-map"
16 ENTITY_MAP_STORAGE_VERSION = 1
17 ENTITY_MAP_SAVE_DELAY = 10
18 _LOGGER = logging.getLogger(__name__)
22 """Holds a cache of entity structure data from a paired HomeKit device.
24 HomeKit has a cacheable entity map that describes how an IP or BLE
25 endpoint is structured. This object holds the latest copy of that data.
27 An endpoint is made of accessories, services and characteristics. It is
28 safe to cache this data until the c# discovery data changes.
30 Caching this data means we can add HomeKit devices to HA immediately at
31 start even if discovery hasn't seen them yet or they are out of range. It
32 is also important for BLE devices - accessing the entity structure is
33 very slow for these devices.
36 def __init__(self, hass: HomeAssistant) ->
None:
37 """Create a new entity map store."""
39 self.
storestore = Store[StorageLayout](
40 hass, ENTITY_MAP_STORAGE_VERSION, ENTITY_MAP_STORAGE_KEY
45 """Get the pairing cache data."""
52 def get_map(self, homekit_id: str) -> Pairing |
None:
53 """Get a pairing cache item."""
61 accessories: list[Any],
62 broadcast_key: str |
None =
None,
63 state_num: int |
None =
None,
65 """Create a new pairing cache."""
66 _LOGGER.debug(
"Creating or updating entity map for %s", homekit_id)
68 config_num=config_num,
69 accessories=accessories,
70 broadcast_key=broadcast_key,
79 """Delete pairing cache."""
83 for hkid
in (homekit_id, homekit_id.lower()):
86 _LOGGER.debug(
"Deleting entity map for %s", hkid)
94 """Schedule saving the entity map cache."""
99 """Return data of entity map to store in a file."""
100 return StorageLayout(pairings=self.
storage_datastorage_data)
104 """Get entity storage."""
105 if ENTITY_MAP
in hass.data:
106 map_storage: EntityMapStorage = hass.data[ENTITY_MAP]
109 await map_storage.async_initialize()
Pairing|None get_map(self, str homekit_id)
None async_initialize(self)
StorageLayout _data_to_save(self)
None _async_schedule_save(self)
None async_delete_map(self, str homekit_id)
None __init__(self, HomeAssistant hass)
Pairing async_create_or_update_map(self, str homekit_id, int config_num, list[Any] accessories, str|None broadcast_key=None, int|None state_num=None)
web.Response get(self, web.Request request, str config_key)
EntityMapStorage async_get_entity_storage(HomeAssistant hass)
None async_delay_save(self, Callable[[], _T] data_func, float delay=0)