1 """Helper functions for the homekit_controller component."""
3 from functools
import lru_cache
4 from typing
import cast
6 from aiohomekit
import Controller
12 from .const
import CONTROLLER
13 from .storage
import async_get_entity_storage
15 type IidTuple = tuple[int, int |
None, int |
None]
19 """Convert a unique_id to a tuple of accessory id, service iid and characteristic iid.
21 Depending on the field in the accessory map that is referenced, some of these may be None.
23 Returns None if this unique_id doesn't follow the homekit_controller scheme and is invalid.
26 match unique_id.split(
"_"):
27 case (unique_id, aid, sid, cid):
29 case (unique_id, aid, sid):
30 return (
int(aid),
int(sid),
None)
31 case (unique_id, aid):
32 return (
int(aid),
None,
None)
43 """Return a name that is used for matching a similar string."""
44 return name.casefold().replace(
" ",
"")
48 """Get or create an aiohomekit Controller instance."""
49 if existing := hass.data.get(CONTROLLER):
50 return cast(Controller, existing)
52 async_zeroconf_instance = await zeroconf.async_get_async_instance(hass)
59 if existing := hass.data.get(CONTROLLER):
60 return cast(Controller, existing)
62 bleak_scanner_instance = bluetooth.async_get_scanner(hass)
64 controller = Controller(
65 async_zeroconf_instance=async_zeroconf_instance,
66 bleak_scanner_instance=bleak_scanner_instance,
67 char_cache=char_cache,
70 hass.data[CONTROLLER] = controller
72 async
def _async_stop_homekit_controller(event: Event) ->
None:
75 hass.data.pop(CONTROLLER,
None)
76 await controller.async_stop()
80 hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _async_stop_homekit_controller)
82 await controller.async_start()
EntityMapStorage async_get_entity_storage(HomeAssistant hass)
Controller async_get_controller(HomeAssistant hass)
IidTuple|None unique_id_to_iids(str unique_id)
str folded_name(str name)