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.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 .entity import ProtectDeviceEntity, async_all_device_entities
from .models import PermRequired, ProtectEntityDescription, ProtectSetableKeysMixin, T
from .utils import async_dispatch_id as _ufpd
_LOGGER = logging.getLogger(__name__)
@ -149,9 +148,7 @@ async def async_setup_entry(
data.async_subscribe_adopt(_add_new_device)
entry.async_on_unload(
async_dispatcher_connect(
hass, _ufpd(entry, DISPATCH_ADD), _async_add_unadopted_device
)
async_dispatcher_connect(hass, data.add_signal, _async_add_unadopted_device)
)
async_add_entities(

View File

@ -26,12 +26,11 @@ from .const import (
ATTR_FPS,
ATTR_HEIGHT,
ATTR_WIDTH,
DISPATCH_CHANNELS,
DOMAIN,
)
from .data import ProtectData, UFPConfigEntry
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__)
@ -153,7 +152,7 @@ async def async_setup_entry(
data.async_subscribe_adopt(_add_new_device)
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))

View File

@ -43,7 +43,7 @@ from .const import (
DISPATCH_CHANNELS,
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__)
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
@callback
def _async_dispatch_id(entry: UFPConfigEntry, dispatch: str) -> str:
"""Generate entry specific dispatch ID."""
return f"{DOMAIN}.{entry.entry_id}.{dispatch}"
class ProtectData:
"""Coordinate updates."""
@ -69,9 +75,6 @@ class ProtectData:
entry: UFPConfigEntry,
) -> None:
"""Initialize an subscriber."""
super().__init__()
self._hass = hass
self._entry = entry
self._hass = hass
self._update_interval = update_interval
@ -80,10 +83,11 @@ class ProtectData:
self._unsub_interval: CALLBACK_TYPE | None = None
self._unsub_websocket: CALLBACK_TYPE | None = None
self._auth_failures = 0
self.last_update_success = False
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
def disable_stream(self) -> bool:
@ -101,7 +105,7 @@ class ProtectData:
) -> None:
"""Add an callback for on device adopt."""
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(
@ -184,12 +188,10 @@ class ProtectData:
def _async_add_device(self, device: ProtectAdoptableDeviceModel) -> None:
if device.is_adopted_by_us:
_LOGGER.debug("Device adopted: %s", device.id)
async_dispatcher_send(
self._hass, _ufpd(self._entry, DISPATCH_ADOPT), device
)
async_dispatcher_send(self._hass, self.adopt_signal, device)
else:
_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
def _async_remove_device(self, device: ProtectAdoptableDeviceModel) -> None:
@ -214,9 +216,7 @@ class ProtectData:
and "channels" in changed_data
):
self._pending_camera_ids.remove(device.id)
async_dispatcher_send(
self._hass, _ufpd(self._entry, DISPATCH_CHANNELS), device
)
async_dispatcher_send(self._hass, self.channels_signal, device)
# trigger update for all Cameras with LCD screens when NVR Doorbell settings updates
if "doorbell_settings" in changed_data:

View File

@ -36,7 +36,6 @@ from .const import (
CONF_ALL_UPDATES,
CONF_OVERRIDE_CHOST,
DEVICES_FOR_SUBSCRIBE,
DOMAIN,
ModelType,
)
@ -121,13 +120,6 @@ def async_get_light_motion_current(obj: Light) -> str:
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
def async_create_api_client(
hass: HomeAssistant, entry: UFPConfigEntry