mirror of
https://github.com/home-assistant/core
synced 2024-09-06 10:29:55 +02:00
Split availability and data subscriptions in homekit_controller (#97337)
This commit is contained in:
parent
cbc8ebb427
commit
cc47ff30b3
@ -142,7 +142,7 @@ class HKDevice:
|
|||||||
function=self.async_update,
|
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]] = {}
|
self._subscriptions: dict[tuple[int, int], set[CALLBACK_TYPE]] = {}
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -189,7 +189,7 @@ class HKDevice:
|
|||||||
if self.available == available:
|
if self.available == available:
|
||||||
return
|
return
|
||||||
self.available = available
|
self.available = available
|
||||||
for callback_ in self._all_subscribers:
|
for callback_ in self._availability_callbacks:
|
||||||
callback_()
|
callback_()
|
||||||
|
|
||||||
async def _async_populate_ble_accessory_state(self, event: Event) -> None:
|
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
|
self, characteristics: Iterable[tuple[int, int]], callback_: CALLBACK_TYPE
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Add characteristics to the watch list."""
|
"""Add characteristics to the watch list."""
|
||||||
self._all_subscribers.add(callback_)
|
|
||||||
for aid_iid in characteristics:
|
for aid_iid in characteristics:
|
||||||
self._subscriptions.setdefault(aid_iid, set()).add(callback_)
|
self._subscriptions.setdefault(aid_iid, set()).add(callback_)
|
||||||
|
|
||||||
def _unsub():
|
def _unsub():
|
||||||
self._all_subscribers.remove(callback_)
|
|
||||||
for aid_iid in characteristics:
|
for aid_iid in characteristics:
|
||||||
self._subscriptions[aid_iid].remove(callback_)
|
self._subscriptions[aid_iid].remove(callback_)
|
||||||
if not self._subscriptions[aid_iid]:
|
if not self._subscriptions[aid_iid]:
|
||||||
@ -824,6 +822,16 @@ class HKDevice:
|
|||||||
|
|
||||||
return _unsub
|
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]:
|
async def get_characteristics(self, *args: Any, **kwargs: Any) -> dict[str, Any]:
|
||||||
"""Read latest state from homekit accessory."""
|
"""Read latest state from homekit accessory."""
|
||||||
return await self.pairing.get_characteristics(*args, **kwargs)
|
return await self.pairing.get_characteristics(*args, **kwargs)
|
||||||
|
@ -58,7 +58,9 @@ class HomeKitEntity(Entity):
|
|||||||
self.all_characteristics, self._async_write_ha_state
|
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)
|
self._accessory.add_pollable_characteristics(self.pollable_characteristics)
|
||||||
await self._accessory.add_watchable_characteristics(
|
await self._accessory.add_watchable_characteristics(
|
||||||
self.watchable_characteristics
|
self.watchable_characteristics
|
||||||
|
Loading…
Reference in New Issue
Block a user