Remove useless delegation in unifiprotect (#119514)

pull/119540/head
J. Nick Koston 2024-06-12 15:23:18 -05:00 committed by GitHub
parent 2661581d4e
commit a586e7fb72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 83 additions and 88 deletions

View File

@ -32,6 +32,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DISPATCH_ADOPT from .const import DISPATCH_ADOPT
from .data import ProtectData, UFPConfigEntry from .data import ProtectData, UFPConfigEntry
from .entity import ( from .entity import (
BaseProtectEntity,
EventEntityMixin, EventEntityMixin,
ProtectDeviceEntity, ProtectDeviceEntity,
ProtectNVREntity, ProtectNVREntity,
@ -630,7 +631,7 @@ async def async_setup_entry(
@callback @callback
def _add_new_device(device: ProtectAdoptableDeviceModel) -> None: def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
entities: list[ProtectDeviceEntity] = async_all_device_entities( entities = async_all_device_entities(
data, data,
ProtectDeviceBinarySensor, ProtectDeviceBinarySensor,
model_descriptions=_MODEL_DESCRIPTIONS, model_descriptions=_MODEL_DESCRIPTIONS,
@ -644,7 +645,7 @@ async def async_setup_entry(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
) )
entities: list[ProtectDeviceEntity] = async_all_device_entities( entities = async_all_device_entities(
data, ProtectDeviceBinarySensor, model_descriptions=_MODEL_DESCRIPTIONS data, ProtectDeviceBinarySensor, model_descriptions=_MODEL_DESCRIPTIONS
) )
entities += _async_event_entities(data) entities += _async_event_entities(data)
@ -679,8 +680,8 @@ def _async_event_entities(
@callback @callback
def _async_nvr_entities( def _async_nvr_entities(
data: ProtectData, data: ProtectData,
) -> list[ProtectDeviceEntity]: ) -> list[BaseProtectEntity]:
entities: list[ProtectDeviceEntity] = [] entities: list[BaseProtectEntity] = []
device = data.api.bootstrap.nvr device = data.api.bootstrap.nvr
if device.system_info.ustorage is None: if device.system_info.ustorage is None:
return entities return entities

View File

@ -138,14 +138,14 @@ async def async_setup_entry(
if not device.can_adopt or not device.can_create(data.api.bootstrap.auth_user): if not device.can_adopt or not device.can_create(data.api.bootstrap.auth_user):
_LOGGER.debug("Device is not adoptable: %s", device.id) _LOGGER.debug("Device is not adoptable: %s", device.id)
return return
async_add_entities(
entities = async_all_device_entities( async_all_device_entities(
data, data,
ProtectButton, ProtectButton,
unadopted_descs=[ADOPT_BUTTON], unadopted_descs=[ADOPT_BUTTON],
ufp_device=device, ufp_device=device,
)
) )
async_add_entities(entities)
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
@ -156,14 +156,15 @@ async def async_setup_entry(
) )
) )
entities: list[ProtectDeviceEntity] = async_all_device_entities( async_add_entities(
data, async_all_device_entities(
ProtectButton, data,
all_descs=ALL_DEVICE_BUTTONS, ProtectButton,
unadopted_descs=[ADOPT_BUTTON], all_descs=ALL_DEVICE_BUTTONS,
model_descriptions=_MODEL_DESCRIPTIONS, unadopted_descs=[ADOPT_BUTTON],
model_descriptions=_MODEL_DESCRIPTIONS,
)
) )
async_add_entities(entities)
for device in data.get_by_types(DEVICES_THAT_ADOPT): for device in data.get_by_types(DEVICES_THAT_ADOPT):
_async_remove_adopt_button(hass, device) _async_remove_adopt_button(hass, device)

View File

@ -155,9 +155,7 @@ async def async_setup_entry(
def _add_new_device(device: ProtectAdoptableDeviceModel) -> None: def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
if not isinstance(device, UFPCamera): if not isinstance(device, UFPCamera):
return return
async_add_entities(_async_camera_entities(hass, entry, data, ufp_device=device))
entities = _async_camera_entities(hass, entry, data, ufp_device=device)
async_add_entities(entities)
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
@ -165,9 +163,7 @@ async def async_setup_entry(
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_CHANNELS), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_CHANNELS), _add_new_device)
) )
async_add_entities(_async_camera_entities(hass, entry, data))
entities = _async_camera_entities(hass, entry, data)
async_add_entities(entities)
class ProtectCamera(ProtectDeviceEntity, Camera): class ProtectCamera(ProtectDeviceEntity, Camera):

View File

@ -37,16 +37,16 @@ _LOGGER = logging.getLogger(__name__)
@callback @callback
def _async_device_entities( def _async_device_entities(
data: ProtectData, data: ProtectData,
klass: type[ProtectDeviceEntity], klass: type[BaseProtectEntity],
model_type: ModelType, model_type: ModelType,
descs: Sequence[ProtectRequiredKeysMixin], descs: Sequence[ProtectRequiredKeysMixin],
unadopted_descs: Sequence[ProtectRequiredKeysMixin] | None = None, unadopted_descs: Sequence[ProtectRequiredKeysMixin] | None = None,
ufp_device: ProtectAdoptableDeviceModel | None = None, ufp_device: ProtectAdoptableDeviceModel | None = None,
) -> list[ProtectDeviceEntity]: ) -> list[BaseProtectEntity]:
if not descs and not unadopted_descs: if not descs and not unadopted_descs:
return [] return []
entities: list[ProtectDeviceEntity] = [] entities: list[BaseProtectEntity] = []
devices = ( devices = (
[ufp_device] [ufp_device]
if ufp_device is not None if ufp_device is not None
@ -130,16 +130,16 @@ def _combine_model_descs(
@callback @callback
def async_all_device_entities( def async_all_device_entities(
data: ProtectData, data: ProtectData,
klass: type[ProtectDeviceEntity], klass: type[BaseProtectEntity],
model_descriptions: dict[ModelType, Sequence[ProtectRequiredKeysMixin]] model_descriptions: dict[ModelType, Sequence[ProtectRequiredKeysMixin]]
| None = None, | None = None,
all_descs: Sequence[ProtectRequiredKeysMixin] | None = None, all_descs: Sequence[ProtectRequiredKeysMixin] | None = None,
unadopted_descs: list[ProtectRequiredKeysMixin] | None = None, unadopted_descs: list[ProtectRequiredKeysMixin] | None = None,
ufp_device: ProtectAdoptableDeviceModel | None = None, ufp_device: ProtectAdoptableDeviceModel | None = None,
) -> list[ProtectDeviceEntity]: ) -> list[BaseProtectEntity]:
"""Generate a list of all the device entities.""" """Generate a list of all the device entities."""
if ufp_device is None: if ufp_device is None:
entities: list[ProtectDeviceEntity] = [] entities: list[BaseProtectEntity] = []
for model_type in _ALL_MODEL_TYPES: for model_type in _ALL_MODEL_TYPES:
descs = _combine_model_descs(model_type, model_descriptions, all_descs) descs = _combine_model_descs(model_type, model_descriptions, all_descs)
entities.extend( entities.extend(
@ -155,17 +155,17 @@ def async_all_device_entities(
) )
class ProtectDeviceEntity(Entity): class BaseProtectEntity(Entity):
"""Base class for UniFi protect entities.""" """Base class for UniFi protect entities."""
device: ProtectAdoptableDeviceModel device: ProtectAdoptableDeviceModel | NVR
_attr_should_poll = False _attr_should_poll = False
def __init__( def __init__(
self, self,
data: ProtectData, data: ProtectData,
device: ProtectAdoptableDeviceModel, device: ProtectAdoptableDeviceModel | NVR,
description: EntityDescription | None = None, description: EntityDescription | None = None,
) -> None: ) -> None:
"""Initialize the entity.""" """Initialize the entity."""
@ -275,20 +275,16 @@ class ProtectDeviceEntity(Entity):
) )
class ProtectNVREntity(ProtectDeviceEntity): class ProtectDeviceEntity(BaseProtectEntity):
"""Base class for UniFi protect entities."""
device: ProtectAdoptableDeviceModel
class ProtectNVREntity(BaseProtectEntity):
"""Base class for unifi protect entities.""" """Base class for unifi protect entities."""
# separate subclass on purpose device: NVR
device: NVR # type: ignore[assignment]
def __init__(
self,
entry: ProtectData,
device: NVR,
description: EntityDescription | None = None,
) -> None:
"""Initialize the entity."""
super().__init__(entry, device, description) # type: ignore[arg-type]
@callback @callback
def _async_set_device_info(self) -> None: def _async_set_device_info(self) -> None:

View File

@ -43,12 +43,10 @@ async def async_setup_entry(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
) )
entities = [] async_add_entities(
for device in data.get_by_types({ModelType.DOORLOCK}): ProtectLock(data, cast(Doorlock, device))
device = cast(Doorlock, device) for device in data.get_by_types({ModelType.DOORLOCK})
entities.append(ProtectLock(data, device)) )
async_add_entities(entities)
class ProtectLock(ProtectDeviceEntity, LockEntity): class ProtectLock(ProtectDeviceEntity, LockEntity):

View File

@ -236,26 +236,27 @@ async def async_setup_entry(
@callback @callback
def _add_new_device(device: ProtectAdoptableDeviceModel) -> None: def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
entities = async_all_device_entities( async_add_entities(
data, async_all_device_entities(
ProtectNumbers, data,
model_descriptions=_MODEL_DESCRIPTIONS, ProtectNumbers,
ufp_device=device, model_descriptions=_MODEL_DESCRIPTIONS,
ufp_device=device,
)
) )
async_add_entities(entities)
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
) )
entities: list[ProtectDeviceEntity] = async_all_device_entities( async_add_entities(
data, async_all_device_entities(
ProtectNumbers, data,
model_descriptions=_MODEL_DESCRIPTIONS, ProtectNumbers,
model_descriptions=_MODEL_DESCRIPTIONS,
)
) )
async_add_entities(entities)
class ProtectNumbers(ProtectDeviceEntity, NumberEntity): class ProtectNumbers(ProtectDeviceEntity, NumberEntity):
"""A UniFi Protect Number Entity.""" """A UniFi Protect Number Entity."""

View File

@ -337,24 +337,24 @@ async def async_setup_entry(
@callback @callback
def _add_new_device(device: ProtectAdoptableDeviceModel) -> None: def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
entities = async_all_device_entities( async_add_entities(
data, async_all_device_entities(
ProtectSelects, data,
model_descriptions=_MODEL_DESCRIPTIONS, ProtectSelects,
ufp_device=device, model_descriptions=_MODEL_DESCRIPTIONS,
ufp_device=device,
)
) )
async_add_entities(entities)
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
) )
async_add_entities(
entities: list[ProtectDeviceEntity] = async_all_device_entities( async_all_device_entities(
data, ProtectSelects, model_descriptions=_MODEL_DESCRIPTIONS data, ProtectSelects, model_descriptions=_MODEL_DESCRIPTIONS
)
) )
async_add_entities(entities)
class ProtectSelects(ProtectDeviceEntity, SelectEntity): class ProtectSelects(ProtectDeviceEntity, SelectEntity):
"""A UniFi Protect Select Entity.""" """A UniFi Protect Select Entity."""

View File

@ -43,6 +43,7 @@ from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DISPATCH_ADOPT from .const import DISPATCH_ADOPT
from .data import ProtectData, UFPConfigEntry from .data import ProtectData, UFPConfigEntry
from .entity import ( from .entity import (
BaseProtectEntity,
EventEntityMixin, EventEntityMixin,
ProtectDeviceEntity, ProtectDeviceEntity,
ProtectNVREntity, ProtectNVREntity,
@ -644,7 +645,7 @@ async def async_setup_entry(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
) )
entities: list[ProtectDeviceEntity] = async_all_device_entities( entities = async_all_device_entities(
data, data,
ProtectDeviceSensor, ProtectDeviceSensor,
all_descs=ALL_DEVICES_SENSORS, all_descs=ALL_DEVICES_SENSORS,
@ -695,8 +696,8 @@ def _async_event_entities(
@callback @callback
def _async_nvr_entities( def _async_nvr_entities(
data: ProtectData, data: ProtectData,
) -> list[ProtectDeviceEntity]: ) -> list[BaseProtectEntity]:
entities: list[ProtectDeviceEntity] = [] entities: list[BaseProtectEntity] = []
device = data.api.bootstrap.nvr device = data.api.bootstrap.nvr
for description in NVR_SENSORS + NVR_DISABLED_SENSORS: for description in NVR_SENSORS + NVR_DISABLED_SENSORS:
entities.append(ProtectNVRSensor(data, device, description)) entities.append(ProtectNVRSensor(data, device, description))

View File

@ -498,7 +498,7 @@ async def async_setup_entry(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
) )
entities: list[ProtectDeviceEntity] = async_all_device_entities( entities = async_all_device_entities(
data, data,
ProtectSwitch, ProtectSwitch,
model_descriptions=_MODEL_DESCRIPTIONS, model_descriptions=_MODEL_DESCRIPTIONS,

View File

@ -69,24 +69,25 @@ async def async_setup_entry(
@callback @callback
def _add_new_device(device: ProtectAdoptableDeviceModel) -> None: def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
entities = async_all_device_entities( async_add_entities(
data, async_all_device_entities(
ProtectDeviceText, data,
model_descriptions=_MODEL_DESCRIPTIONS, ProtectDeviceText,
ufp_device=device, model_descriptions=_MODEL_DESCRIPTIONS,
ufp_device=device,
)
) )
async_add_entities(entities)
entry.async_on_unload( entry.async_on_unload(
async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device) async_dispatcher_connect(hass, _ufpd(entry, DISPATCH_ADOPT), _add_new_device)
) )
entities: list[ProtectDeviceEntity] = async_all_device_entities( async_add_entities(
data, ProtectDeviceText, model_descriptions=_MODEL_DESCRIPTIONS async_all_device_entities(
data, ProtectDeviceText, model_descriptions=_MODEL_DESCRIPTIONS
)
) )
async_add_entities(entities)
class ProtectDeviceText(ProtectDeviceEntity, TextEntity): class ProtectDeviceText(ProtectDeviceEntity, TextEntity):
"""A Ubiquiti UniFi Protect Sensor.""" """A Ubiquiti UniFi Protect Sensor."""