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 .data import ProtectData, UFPConfigEntry
from .entity import (
BaseProtectEntity,
EventEntityMixin,
ProtectDeviceEntity,
ProtectNVREntity,
@ -630,7 +631,7 @@ async def async_setup_entry(
@callback
def _add_new_device(device: ProtectAdoptableDeviceModel) -> None:
entities: list[ProtectDeviceEntity] = async_all_device_entities(
entities = async_all_device_entities(
data,
ProtectDeviceBinarySensor,
model_descriptions=_MODEL_DESCRIPTIONS,
@ -644,7 +645,7 @@ async def async_setup_entry(
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
)
entities += _async_event_entities(data)
@ -679,8 +680,8 @@ def _async_event_entities(
@callback
def _async_nvr_entities(
data: ProtectData,
) -> list[ProtectDeviceEntity]:
entities: list[ProtectDeviceEntity] = []
) -> list[BaseProtectEntity]:
entities: list[BaseProtectEntity] = []
device = data.api.bootstrap.nvr
if device.system_info.ustorage is None:
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):
_LOGGER.debug("Device is not adoptable: %s", device.id)
return
entities = async_all_device_entities(
data,
ProtectButton,
unadopted_descs=[ADOPT_BUTTON],
ufp_device=device,
async_add_entities(
async_all_device_entities(
data,
ProtectButton,
unadopted_descs=[ADOPT_BUTTON],
ufp_device=device,
)
)
async_add_entities(entities)
entry.async_on_unload(
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(
data,
ProtectButton,
all_descs=ALL_DEVICE_BUTTONS,
unadopted_descs=[ADOPT_BUTTON],
model_descriptions=_MODEL_DESCRIPTIONS,
async_add_entities(
async_all_device_entities(
data,
ProtectButton,
all_descs=ALL_DEVICE_BUTTONS,
unadopted_descs=[ADOPT_BUTTON],
model_descriptions=_MODEL_DESCRIPTIONS,
)
)
async_add_entities(entities)
for device in data.get_by_types(DEVICES_THAT_ADOPT):
_async_remove_adopt_button(hass, device)

View File

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

View File

@ -37,16 +37,16 @@ _LOGGER = logging.getLogger(__name__)
@callback
def _async_device_entities(
data: ProtectData,
klass: type[ProtectDeviceEntity],
klass: type[BaseProtectEntity],
model_type: ModelType,
descs: Sequence[ProtectRequiredKeysMixin],
unadopted_descs: Sequence[ProtectRequiredKeysMixin] | None = None,
ufp_device: ProtectAdoptableDeviceModel | None = None,
) -> list[ProtectDeviceEntity]:
) -> list[BaseProtectEntity]:
if not descs and not unadopted_descs:
return []
entities: list[ProtectDeviceEntity] = []
entities: list[BaseProtectEntity] = []
devices = (
[ufp_device]
if ufp_device is not None
@ -130,16 +130,16 @@ def _combine_model_descs(
@callback
def async_all_device_entities(
data: ProtectData,
klass: type[ProtectDeviceEntity],
klass: type[BaseProtectEntity],
model_descriptions: dict[ModelType, Sequence[ProtectRequiredKeysMixin]]
| None = None,
all_descs: Sequence[ProtectRequiredKeysMixin] | None = None,
unadopted_descs: list[ProtectRequiredKeysMixin] | None = None,
ufp_device: ProtectAdoptableDeviceModel | None = None,
) -> list[ProtectDeviceEntity]:
) -> list[BaseProtectEntity]:
"""Generate a list of all the device entities."""
if ufp_device is None:
entities: list[ProtectDeviceEntity] = []
entities: list[BaseProtectEntity] = []
for model_type in _ALL_MODEL_TYPES:
descs = _combine_model_descs(model_type, model_descriptions, all_descs)
entities.extend(
@ -155,17 +155,17 @@ def async_all_device_entities(
)
class ProtectDeviceEntity(Entity):
class BaseProtectEntity(Entity):
"""Base class for UniFi protect entities."""
device: ProtectAdoptableDeviceModel
device: ProtectAdoptableDeviceModel | NVR
_attr_should_poll = False
def __init__(
self,
data: ProtectData,
device: ProtectAdoptableDeviceModel,
device: ProtectAdoptableDeviceModel | NVR,
description: EntityDescription | None = None,
) -> None:
"""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."""
# separate subclass on purpose
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]
device: NVR
@callback
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)
)
entities = []
for device in data.get_by_types({ModelType.DOORLOCK}):
device = cast(Doorlock, device)
entities.append(ProtectLock(data, device))
async_add_entities(entities)
async_add_entities(
ProtectLock(data, cast(Doorlock, device))
for device in data.get_by_types({ModelType.DOORLOCK})
)
class ProtectLock(ProtectDeviceEntity, LockEntity):

View File

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

View File

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

View File

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

View File

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