Split availability and data subscriptions in homekit_controller (#97337)
This commit is contained in:
parent
cbc8ebb427
commit
cc47ff30b3
2 changed files with 15 additions and 5 deletions
|
@ -142,7 +142,7 @@ class HKDevice:
|
|||
function=self.async_update,
|
||||
)
|
||||
|
||||
self._all_subscribers: set[CALLBACK_TYPE] = set()
|
||||
self._availability_callbacks: set[CALLBACK_TYPE] = set()
|
||||
self._subscriptions: dict[tuple[int, int], set[CALLBACK_TYPE]] = {}
|
||||
|
||||
@property
|
||||
|
@ -189,7 +189,7 @@ class HKDevice:
|
|||
if self.available == available:
|
||||
return
|
||||
self.available = available
|
||||
for callback_ in self._all_subscribers:
|
||||
for callback_ in self._availability_callbacks:
|
||||
callback_()
|
||||
|
||||
async def _async_populate_ble_accessory_state(self, event: Event) -> None:
|
||||
|
@ -811,12 +811,10 @@ class HKDevice:
|
|||
self, characteristics: Iterable[tuple[int, int]], callback_: CALLBACK_TYPE
|
||||
) -> CALLBACK_TYPE:
|
||||
"""Add characteristics to the watch list."""
|
||||
self._all_subscribers.add(callback_)
|
||||
for aid_iid in characteristics:
|
||||
self._subscriptions.setdefault(aid_iid, set()).add(callback_)
|
||||
|
||||
def _unsub():
|
||||
self._all_subscribers.remove(callback_)
|
||||
for aid_iid in characteristics:
|
||||
self._subscriptions[aid_iid].remove(callback_)
|
||||
if not self._subscriptions[aid_iid]:
|
||||
|
@ -824,6 +822,16 @@ class HKDevice:
|
|||
|
||||
return _unsub
|
||||
|
||||
@callback
|
||||
def async_subscribe_availability(self, callback_: CALLBACK_TYPE) -> CALLBACK_TYPE:
|
||||
"""Add characteristics to the watch list."""
|
||||
self._availability_callbacks.add(callback_)
|
||||
|
||||
def _unsub():
|
||||
self._availability_callbacks.remove(callback_)
|
||||
|
||||
return _unsub
|
||||
|
||||
async def get_characteristics(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
|
||||
"""Read latest state from homekit accessory."""
|
||||
return await self.pairing.get_characteristics(*args, **kwargs)
|
||||
|
|
|
@ -58,7 +58,9 @@ class HomeKitEntity(Entity):
|
|||
self.all_characteristics, self._async_write_ha_state
|
||||
)
|
||||
)
|
||||
|
||||
self.async_on_remove(
|
||||
self._accessory.async_subscribe_availability(self._async_write_ha_state)
|
||||
)
|
||||
self._accessory.add_pollable_characteristics(self.pollable_characteristics)
|
||||
await self._accessory.add_watchable_characteristics(
|
||||
self.watchable_characteristics
|
||||
|
|
Loading…
Add table
Reference in a new issue