diff --git a/homeassistant/components/unifiprotect/sensor.py b/homeassistant/components/unifiprotect/sensor.py index a69e9d48293..95b01710b9b 100644 --- a/homeassistant/components/unifiprotect/sensor.py +++ b/homeassistant/components/unifiprotect/sensor.py @@ -520,7 +520,7 @@ NVR_DISABLED_SENSORS: tuple[ProtectSensorEntityDescription, ...] = ( ), ) -EVENT_SENSORS: tuple[ProtectSensorEventEntityDescription, ...] = ( +LICENSE_PLATE_EVENT_SENSORS: tuple[ProtectSensorEventEntityDescription, ...] = ( ProtectSensorEventEntityDescription( key="smart_obj_licenseplate", name="License Plate Detected", @@ -678,11 +678,11 @@ def _async_event_entities( if not device.feature_flags.has_smart_detect: continue - for event_desc in EVENT_SENSORS: + for event_desc in LICENSE_PLATE_EVENT_SENSORS: if not event_desc.has_required(device): continue - entities.append(ProtectEventSensor(data, device, event_desc)) + entities.append(ProtectLicensePlateEventSensor(data, device, event_desc)) _LOGGER.debug( "Adding sensor entity %s for %s", description.name, @@ -750,35 +750,6 @@ class ProtectEventSensor(EventEntityMixin, SensorEntity): entity_description: ProtectSensorEventEntityDescription - @callback - def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None: - # do not call ProtectDeviceSensor method since we want event to get value here - EventEntityMixin._async_update_device_from_protect(self, device) # noqa: SLF001 - event = self._event - entity_description = self.entity_description - is_on = entity_description.get_is_on(self.device, self._event) - is_license_plate = ( - entity_description.ufp_event_obj == "last_license_plate_detect_event" - ) - if ( - not is_on - or event is None - or ( - is_license_plate - and (event.metadata is None or event.metadata.license_plate is None) - ) - ): - self._attr_native_value = OBJECT_TYPE_NONE - self._event = None - self._attr_extra_state_attributes = {} - return - - if is_license_plate: - # type verified above - self._attr_native_value = event.metadata.license_plate.name # type: ignore[union-attr] - else: - self._attr_native_value = event.smart_detect_types[0].value - @callback def _async_get_state_attrs(self) -> tuple[Any, ...]: """Retrieve data that goes into the current state of the entity. @@ -792,3 +763,24 @@ class ProtectEventSensor(EventEntityMixin, SensorEntity): self._attr_native_value, self._attr_extra_state_attributes, ) + + +class ProtectLicensePlateEventSensor(ProtectEventSensor): + """A UniFi Protect license plate sensor.""" + + @callback + def _async_update_device_from_protect(self, device: ProtectModelWithId) -> None: + super()._async_update_device_from_protect(device) + event = self._event + entity_description = self.entity_description + if ( + event is None + or (event.metadata is None or event.metadata.license_plate is None) + or not entity_description.get_is_on(self.device, event) + ): + self._attr_native_value = OBJECT_TYPE_NONE + self._event = None + self._attr_extra_state_attributes = {} + return + + self._attr_native_value = event.metadata.license_plate.name diff --git a/tests/components/unifiprotect/test_sensor.py b/tests/components/unifiprotect/test_sensor.py index 1ba3641ba36..72915936a70 100644 --- a/tests/components/unifiprotect/test_sensor.py +++ b/tests/components/unifiprotect/test_sensor.py @@ -14,7 +14,7 @@ from homeassistant.components.unifiprotect.sensor import ( ALL_DEVICES_SENSORS, CAMERA_DISABLED_SENSORS, CAMERA_SENSORS, - EVENT_SENSORS, + LICENSE_PLATE_EVENT_SENSORS, MOTION_TRIP_SENSORS, NVR_DISABLED_SENSORS, NVR_SENSORS, @@ -514,7 +514,7 @@ async def test_camera_update_licenseplate( assert_entity_counts(hass, Platform.SENSOR, 23, 13) _, entity_id = ids_from_device_description( - Platform.SENSOR, camera, EVENT_SENSORS[0] + Platform.SENSOR, camera, LICENSE_PLATE_EVENT_SENSORS[0] ) event_metadata = EventMetadata(