From a586e7fb7282a3777f4ecf00f8841936c9e47221 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 12 Jun 2024 15:23:18 -0500 Subject: [PATCH] Remove useless delegation in unifiprotect (#119514) --- .../components/unifiprotect/binary_sensor.py | 9 +++-- .../components/unifiprotect/button.py | 29 +++++++------- .../components/unifiprotect/camera.py | 8 +--- .../components/unifiprotect/entity.py | 38 +++++++++---------- homeassistant/components/unifiprotect/lock.py | 10 ++--- .../components/unifiprotect/number.py | 25 ++++++------ .../components/unifiprotect/select.py | 22 +++++------ .../components/unifiprotect/sensor.py | 7 ++-- .../components/unifiprotect/switch.py | 2 +- homeassistant/components/unifiprotect/text.py | 21 +++++----- 10 files changed, 83 insertions(+), 88 deletions(-) diff --git a/homeassistant/components/unifiprotect/binary_sensor.py b/homeassistant/components/unifiprotect/binary_sensor.py index f42d2d09211..396894c997a 100644 --- a/homeassistant/components/unifiprotect/binary_sensor.py +++ b/homeassistant/components/unifiprotect/binary_sensor.py @@ -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 diff --git a/homeassistant/components/unifiprotect/button.py b/homeassistant/components/unifiprotect/button.py index 98d226e9e76..a1b1ec21f6a 100644 --- a/homeassistant/components/unifiprotect/button.py +++ b/homeassistant/components/unifiprotect/button.py @@ -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) diff --git a/homeassistant/components/unifiprotect/camera.py b/homeassistant/components/unifiprotect/camera.py index 5a703dc5458..dc41310ab3f 100644 --- a/homeassistant/components/unifiprotect/camera.py +++ b/homeassistant/components/unifiprotect/camera.py @@ -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): diff --git a/homeassistant/components/unifiprotect/entity.py b/homeassistant/components/unifiprotect/entity.py index 137f8c532ee..a41aadfcd89 100644 --- a/homeassistant/components/unifiprotect/entity.py +++ b/homeassistant/components/unifiprotect/entity.py @@ -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: diff --git a/homeassistant/components/unifiprotect/lock.py b/homeassistant/components/unifiprotect/lock.py index 4deeafa0782..7ffa3c6bfc5 100644 --- a/homeassistant/components/unifiprotect/lock.py +++ b/homeassistant/components/unifiprotect/lock.py @@ -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): diff --git a/homeassistant/components/unifiprotect/number.py b/homeassistant/components/unifiprotect/number.py index 05d07203191..08e07536f87 100644 --- a/homeassistant/components/unifiprotect/number.py +++ b/homeassistant/components/unifiprotect/number.py @@ -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.""" diff --git a/homeassistant/components/unifiprotect/select.py b/homeassistant/components/unifiprotect/select.py index 678d0007347..57e0c806c69 100644 --- a/homeassistant/components/unifiprotect/select.py +++ b/homeassistant/components/unifiprotect/select.py @@ -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.""" diff --git a/homeassistant/components/unifiprotect/sensor.py b/homeassistant/components/unifiprotect/sensor.py index 7624a659d38..26103d21bb5 100644 --- a/homeassistant/components/unifiprotect/sensor.py +++ b/homeassistant/components/unifiprotect/sensor.py @@ -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)) diff --git a/homeassistant/components/unifiprotect/switch.py b/homeassistant/components/unifiprotect/switch.py index fafa9d1f90d..3dd8bc2dbda 100644 --- a/homeassistant/components/unifiprotect/switch.py +++ b/homeassistant/components/unifiprotect/switch.py @@ -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, diff --git a/homeassistant/components/unifiprotect/text.py b/homeassistant/components/unifiprotect/text.py index 5fc11546fae..30c54d4c15c 100644 --- a/homeassistant/components/unifiprotect/text.py +++ b/homeassistant/components/unifiprotect/text.py @@ -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."""