Reduce duplicate code in unifiprotect entities (#119779)

pull/119787/head
J. Nick Koston 2024-06-16 14:14:59 -05:00 committed by GitHub
parent 2713a3fdb1
commit 151b3b3b0a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 40 deletions

View File

@ -706,8 +706,8 @@ def _async_nvr_entities(
return entities
class ProtectDeviceSensor(ProtectDeviceEntity, SensorEntity):
"""A Ubiquiti UniFi Protect Sensor."""
class BaseProtectSensor(BaseProtectEntity, SensorEntity):
"""A UniFi Protect Sensor Entity."""
entity_description: ProtectSensorEntityDescription
_state_attrs = ("_attr_available", "_attr_native_value")
@ -717,15 +717,12 @@ class ProtectDeviceSensor(ProtectDeviceEntity, SensorEntity):
self._attr_native_value = self.entity_description.get_ufp_value(self.device)
class ProtectNVRSensor(ProtectNVREntity, SensorEntity):
class ProtectDeviceSensor(BaseProtectSensor, ProtectDeviceEntity):
"""A Ubiquiti UniFi Protect Sensor."""
entity_description: ProtectSensorEntityDescription
_state_attrs = ("_attr_available", "_attr_native_value")
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
super()._async_update_device_from_protect(device)
self._attr_native_value = self.entity_description.get_ufp_value(self.device)
class ProtectNVRSensor(BaseProtectSensor, ProtectNVREntity):
"""A Ubiquiti UniFi Protect Sensor."""
class ProtectEventSensor(EventEntityMixin, SensorEntity):

View File

@ -472,43 +472,32 @@ _PRIVACY_DESCRIPTIONS: dict[ModelType, Sequence[ProtectEntityDescription]] = {
}
class ProtectSwitch(ProtectDeviceEntity, SwitchEntity):
class ProtectBaseSwitch(BaseProtectEntity, SwitchEntity):
"""Base class for UniFi Protect Switch."""
entity_description: ProtectSwitchEntityDescription
_state_attrs = ("_attr_available", "_attr_is_on")
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
super()._async_update_device_from_protect(device)
self._attr_is_on = self.entity_description.get_ufp_value(self.device) is True
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on."""
await self.entity_description.ufp_set(self.device, True)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
await self.entity_description.ufp_set(self.device, False)
class ProtectSwitch(ProtectBaseSwitch, ProtectDeviceEntity):
"""A UniFi Protect Switch."""
entity_description: ProtectSwitchEntityDescription
_state_attrs = ("_attr_available", "_attr_is_on")
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
super()._async_update_device_from_protect(device)
self._attr_is_on = self.entity_description.get_ufp_value(self.device) is True
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on."""
await self.entity_description.ufp_set(self.device, True)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
await self.entity_description.ufp_set(self.device, False)
class ProtectNVRSwitch(ProtectNVREntity, SwitchEntity):
class ProtectNVRSwitch(ProtectBaseSwitch, ProtectNVREntity):
"""A UniFi Protect NVR Switch."""
entity_description: ProtectSwitchEntityDescription
_state_attrs = ("_attr_available", "_attr_is_on")
def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None:
super()._async_update_device_from_protect(device)
self._attr_is_on = self.entity_description.get_ufp_value(self.device) is True
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn the device on."""
await self.entity_description.ufp_set(self.device, True)
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn the device off."""
await self.entity_description.ufp_set(self.device, False)
class ProtectPrivacyModeSwitch(RestoreEntity, ProtectSwitch):
"""A UniFi Protect Switch."""