Cleanup unifiprotect ProtectData object (#119787)

This commit is contained in:
J. Nick Koston 2024-06-16 17:08:09 -05:00 committed by GitHub
parent 85ca6f15be
commit 8c613bc869
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 30 deletions

View File

@ -20,11 +20,10 @@ from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DEVICES_THAT_ADOPT, DISPATCH_ADD, DOMAIN from .const import DEVICES_THAT_ADOPT, DOMAIN
from .data import UFPConfigEntry from .data import UFPConfigEntry
from .entity import ProtectDeviceEntity, async_all_device_entities from .entity import ProtectDeviceEntity, async_all_device_entities
from .models import PermRequired, ProtectEntityDescription, ProtectSetableKeysMixin, T from .models import PermRequired, ProtectEntityDescription, ProtectSetableKeysMixin, T
from .utils import async_dispatch_id as _ufpd
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -149,9 +148,7 @@ async def async_setup_entry(
data.async_subscribe_adopt(_add_new_device) data.async_subscribe_adopt(_add_new_device)
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect( async_dispatcher_connect(hass, data.add_signal, _async_add_unadopted_device)
hass, _ufpd(entry, DISPATCH_ADD), _async_add_unadopted_device
)
) )
async_add_entities( async_add_entities(

View File

@ -26,12 +26,11 @@ from .const import (
ATTR_FPS, ATTR_FPS,
ATTR_HEIGHT, ATTR_HEIGHT,
ATTR_WIDTH, ATTR_WIDTH,
DISPATCH_CHANNELS,
DOMAIN, DOMAIN,
) )
from .data import ProtectData, UFPConfigEntry from .data import ProtectData, UFPConfigEntry
from .entity import ProtectDeviceEntity from .entity import ProtectDeviceEntity
from .utils import async_dispatch_id as _ufpd, get_camera_base_name from .utils import get_camera_base_name
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -153,7 +152,7 @@ async def async_setup_entry(
data.async_subscribe_adopt(_add_new_device) data.async_subscribe_adopt(_add_new_device)
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_CHANNELS), _add_new_device) async_dispatcher_connect(hass, data.channels_signal, _add_new_device)
) )
async_add_entities(_async_camera_entities(hass, entry, data)) async_add_entities(_async_camera_entities(hass, entry, data))

View File

@ -43,7 +43,7 @@ from .const import (
DISPATCH_CHANNELS, DISPATCH_CHANNELS,
DOMAIN, DOMAIN,
) )
from .utils import async_dispatch_id as _ufpd, async_get_devices_by_type from .utils import async_get_devices_by_type
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
type ProtectDeviceType = ProtectAdoptableDeviceModel | NVR type ProtectDeviceType = ProtectAdoptableDeviceModel | NVR
@ -58,6 +58,12 @@ def async_last_update_was_successful(
return hasattr(entry, "runtime_data") and entry.runtime_data.last_update_success return hasattr(entry, "runtime_data") and entry.runtime_data.last_update_success
@callback
def _async_dispatch_id(entry: UFPConfigEntry, dispatch: str) -> str:
"""Generate entry specific dispatch ID."""
return f"{DOMAIN}.{entry.entry_id}.{dispatch}"
class ProtectData: class ProtectData:
"""Coordinate updates.""" """Coordinate updates."""
@ -69,9 +75,6 @@ class ProtectData:
entry: UFPConfigEntry, entry: UFPConfigEntry,
) -> None: ) -> None:
"""Initialize an subscriber.""" """Initialize an subscriber."""
super().__init__()
self._hass = hass
self._entry = entry self._entry = entry
self._hass = hass self._hass = hass
self._update_interval = update_interval self._update_interval = update_interval
@ -80,10 +83,11 @@ class ProtectData:
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
self._auth_failures = 0 self._auth_failures = 0
self.last_update_success = False self.last_update_success = False
self.api = protect self.api = protect
self._adopt_signal = _ufpd(self._entry, DISPATCH_ADOPT) self.adopt_signal = _async_dispatch_id(entry, DISPATCH_ADOPT)
self.add_signal = _async_dispatch_id(entry, DISPATCH_ADD)
self.channels_signal = _async_dispatch_id(entry, DISPATCH_CHANNELS)
@property @property
def disable_stream(self) -> bool: def disable_stream(self) -> bool:
@ -101,7 +105,7 @@ class ProtectData:
) -> None: ) -> None:
"""Add an callback for on device adopt.""" """Add an callback for on device adopt."""
self._entry.async_on_unload( self._entry.async_on_unload(
async_dispatcher_connect(self._hass, self._adopt_signal, add_callback) async_dispatcher_connect(self._hass, self.adopt_signal, add_callback)
) )
def get_by_types( def get_by_types(
@ -184,12 +188,10 @@ class ProtectData:
def _async_add_device(self, device: ProtectAdoptableDeviceModel) -> None: def _async_add_device(self, device: ProtectAdoptableDeviceModel) -> None:
if device.is_adopted_by_us: if device.is_adopted_by_us:
_LOGGER.debug("Device adopted: %s", device.id) _LOGGER.debug("Device adopted: %s", device.id)
async_dispatcher_send( async_dispatcher_send(self._hass, self.adopt_signal, device)
self._hass, _ufpd(self._entry, DISPATCH_ADOPT), device
)
else: else:
_LOGGER.debug("New device detected: %s", device.id) _LOGGER.debug("New device detected: %s", device.id)
async_dispatcher_send(self._hass, _ufpd(self._entry, DISPATCH_ADD), device) async_dispatcher_send(self._hass, self.add_signal, device)
@callback @callback
def _async_remove_device(self, device: ProtectAdoptableDeviceModel) -> None: def _async_remove_device(self, device: ProtectAdoptableDeviceModel) -> None:
@ -214,9 +216,7 @@ class ProtectData:
and "channels" in changed_data and "channels" in changed_data
): ):
self._pending_camera_ids.remove(device.id) self._pending_camera_ids.remove(device.id)
async_dispatcher_send( async_dispatcher_send(self._hass, self.channels_signal, device)
self._hass, _ufpd(self._entry, DISPATCH_CHANNELS), device
)
# trigger update for all Cameras with LCD screens when NVR Doorbell settings updates # trigger update for all Cameras with LCD screens when NVR Doorbell settings updates
if "doorbell_settings" in changed_data: if "doorbell_settings" in changed_data:

View File

@ -36,7 +36,6 @@ from .const import (
CONF_ALL_UPDATES, CONF_ALL_UPDATES,
CONF_OVERRIDE_CHOST, CONF_OVERRIDE_CHOST,
DEVICES_FOR_SUBSCRIBE, DEVICES_FOR_SUBSCRIBE,
DOMAIN,
ModelType, ModelType,
) )
@ -121,13 +120,6 @@ def async_get_light_motion_current(obj: Light) -> str:
return obj.light_mode_settings.mode.value return obj.light_mode_settings.mode.value
@callback
def async_dispatch_id(entry: UFPConfigEntry, dispatch: str) -> str:
"""Generate entry specific dispatch ID."""
return f"{DOMAIN}.{entry.entry_id}.{dispatch}"
@callback @callback
def async_create_api_client( def async_create_api_client(
hass: HomeAssistant, entry: UFPConfigEntry hass: HomeAssistant, entry: UFPConfigEntry