mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Cleanup unifiprotect subscriptions logic (#120049)
This commit is contained in:
parent
ee85c0e44c
commit
fb25902de9
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from collections import defaultdict
|
||||||
from collections.abc import Callable, Iterable
|
from collections.abc import Callable, Iterable
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from functools import partial
|
from functools import partial
|
||||||
@ -78,7 +79,9 @@ class ProtectData:
|
|||||||
self._entry = entry
|
self._entry = entry
|
||||||
self._hass = hass
|
self._hass = hass
|
||||||
self._update_interval = update_interval
|
self._update_interval = update_interval
|
||||||
self._subscriptions: dict[str, list[Callable[[ProtectDeviceType], None]]] = {}
|
self._subscriptions: defaultdict[
|
||||||
|
str, set[Callable[[ProtectDeviceType], None]]
|
||||||
|
] = defaultdict(set)
|
||||||
self._pending_camera_ids: set[str] = set()
|
self._pending_camera_ids: set[str] = set()
|
||||||
self._unsub_interval: CALLBACK_TYPE | None = None
|
self._unsub_interval: CALLBACK_TYPE | None = None
|
||||||
self._unsub_websocket: CALLBACK_TYPE | None = None
|
self._unsub_websocket: CALLBACK_TYPE | None = None
|
||||||
@ -302,7 +305,7 @@ class ProtectData:
|
|||||||
)
|
)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_subscribe_device_id(
|
def async_subscribe(
|
||||||
self, mac: str, update_callback: Callable[[ProtectDeviceType], None]
|
self, mac: str, update_callback: Callable[[ProtectDeviceType], None]
|
||||||
) -> CALLBACK_TYPE:
|
) -> CALLBACK_TYPE:
|
||||||
"""Add an callback subscriber."""
|
"""Add an callback subscriber."""
|
||||||
@ -310,11 +313,11 @@ class ProtectData:
|
|||||||
self._unsub_interval = async_track_time_interval(
|
self._unsub_interval = async_track_time_interval(
|
||||||
self._hass, self._async_poll, self._update_interval
|
self._hass, self._async_poll, self._update_interval
|
||||||
)
|
)
|
||||||
self._subscriptions.setdefault(mac, []).append(update_callback)
|
self._subscriptions[mac].add(update_callback)
|
||||||
return partial(self.async_unsubscribe_device_id, mac, update_callback)
|
return partial(self._async_unsubscribe, mac, update_callback)
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def async_unsubscribe_device_id(
|
def _async_unsubscribe(
|
||||||
self, mac: str, update_callback: Callable[[ProtectDeviceType], None]
|
self, mac: str, update_callback: Callable[[ProtectDeviceType], None]
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Remove a callback subscriber."""
|
"""Remove a callback subscriber."""
|
||||||
@ -328,9 +331,10 @@ class ProtectData:
|
|||||||
@callback
|
@callback
|
||||||
def _async_signal_device_update(self, device: ProtectDeviceType) -> None:
|
def _async_signal_device_update(self, device: ProtectDeviceType) -> None:
|
||||||
"""Call the callbacks for a device_id."""
|
"""Call the callbacks for a device_id."""
|
||||||
if not (subscriptions := self._subscriptions.get(device.mac)):
|
mac = device.mac
|
||||||
|
if not (subscriptions := self._subscriptions.get(mac)):
|
||||||
return
|
return
|
||||||
_LOGGER.debug("Updating device: %s (%s)", device.name, device.mac)
|
_LOGGER.debug("Updating device: %s (%s)", device.name, mac)
|
||||||
for update_callback in subscriptions:
|
for update_callback in subscriptions:
|
||||||
update_callback(device)
|
update_callback(device)
|
||||||
|
|
||||||
|
@ -261,9 +261,7 @@ class BaseProtectEntity(Entity):
|
|||||||
"""When entity is added to hass."""
|
"""When entity is added to hass."""
|
||||||
await super().async_added_to_hass()
|
await super().async_added_to_hass()
|
||||||
self.async_on_remove(
|
self.async_on_remove(
|
||||||
self.data.async_subscribe_device_id(
|
self.data.async_subscribe(self.device.mac, self._async_updated_event)
|
||||||
self.device.mac, self._async_updated_event
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user