From ee7022dc67151175b338511fb84c71e347278575 Mon Sep 17 00:00:00 2001
From: Christopher Bailey <cbailey@mort.is>
Date: Sun, 4 Dec 2022 22:06:55 -0500
Subject: [PATCH] Update UniFi Protect ring entity to use event entity (#83270)

---
 .../components/unifiprotect/binary_sensor.py  | 27 ++++++++++---------
 .../components/unifiprotect/sensor.py         | 10 +++----
 .../unifiprotect/test_binary_sensor.py        | 12 ++++-----
 tests/components/unifiprotect/test_sensor.py  |  8 +++---
 4 files changed, 29 insertions(+), 28 deletions(-)

diff --git a/homeassistant/components/unifiprotect/binary_sensor.py b/homeassistant/components/unifiprotect/binary_sensor.py
index bf6e89c3caf..bf53dc8d206 100644
--- a/homeassistant/components/unifiprotect/binary_sensor.py
+++ b/homeassistant/components/unifiprotect/binary_sensor.py
@@ -67,14 +67,6 @@ MOUNT_DEVICE_CLASS_MAP = {
 
 
 CAMERA_SENSORS: tuple[ProtectBinaryEntityDescription, ...] = (
-    ProtectBinaryEntityDescription(
-        key="doorbell",
-        name="Doorbell",
-        device_class=BinarySensorDeviceClass.OCCUPANCY,
-        icon="mdi:doorbell-video",
-        ufp_required_field="feature_flags.has_chime",
-        ufp_value="is_ringing",
-    ),
     ProtectBinaryEntityDescription(
         key="dark",
         name="Is Dark",
@@ -339,7 +331,16 @@ SENSE_SENSORS: tuple[ProtectBinaryEntityDescription, ...] = (
     ),
 )
 
-MOTION_SENSORS: tuple[ProtectBinaryEventEntityDescription, ...] = (
+EVENT_SENSORS: tuple[ProtectBinaryEventEntityDescription, ...] = (
+    ProtectBinaryEventEntityDescription(
+        key="doorbell",
+        name="Doorbell",
+        device_class=BinarySensorDeviceClass.OCCUPANCY,
+        icon="mdi:doorbell-video",
+        ufp_required_field="feature_flags.has_chime",
+        ufp_value="is_ringing",
+        ufp_event_obj="last_ring_event",
+    ),
     ProtectBinaryEventEntityDescription(
         key="motion",
         name="Motion",
@@ -485,7 +486,7 @@ async def async_setup_entry(
             ufp_device=device,
         )
         if device.is_adopted and isinstance(device, Camera):
-            entities += _async_motion_entities(data, ufp_device=device)
+            entities += _async_event_entities(data, ufp_device=device)
         async_add_entities(entities)
 
     entry.async_on_unload(
@@ -501,14 +502,14 @@ async def async_setup_entry(
         lock_descs=DOORLOCK_SENSORS,
         viewer_descs=VIEWER_SENSORS,
     )
-    entities += _async_motion_entities(data)
+    entities += _async_event_entities(data)
     entities += _async_nvr_entities(data)
 
     async_add_entities(entities)
 
 
 @callback
-def _async_motion_entities(
+def _async_event_entities(
     data: ProtectData,
     ufp_device: ProtectAdoptableDeviceModel | None = None,
 ) -> list[ProtectDeviceEntity]:
@@ -517,7 +518,7 @@ def _async_motion_entities(
         data.get_by_types({ModelType.CAMERA}) if ufp_device is None else [ufp_device]
     )
     for device in devices:
-        for description in MOTION_SENSORS:
+        for description in EVENT_SENSORS:
             if not description.has_required(device):
                 continue
             entities.append(ProtectEventBinarySensor(data, device, description))
diff --git a/homeassistant/components/unifiprotect/sensor.py b/homeassistant/components/unifiprotect/sensor.py
index d608907fa83..fa08892e2d3 100644
--- a/homeassistant/components/unifiprotect/sensor.py
+++ b/homeassistant/components/unifiprotect/sensor.py
@@ -521,7 +521,7 @@ NVR_DISABLED_SENSORS: tuple[ProtectSensorEntityDescription, ...] = (
     ),
 )
 
-MOTION_SENSORS: tuple[ProtectSensorEventEntityDescription, ...] = (
+EVENT_SENSORS: tuple[ProtectSensorEventEntityDescription, ...] = (
     ProtectSensorEventEntityDescription(
         key="detected_object",
         name="Detected Object",
@@ -641,7 +641,7 @@ async def async_setup_entry(
             ufp_device=device,
         )
         if device.is_adopted_by_us and isinstance(device, Camera):
-            entities += _async_motion_entities(data, ufp_device=device)
+            entities += _async_event_entities(data, ufp_device=device)
         async_add_entities(entities)
 
     entry.async_on_unload(
@@ -659,14 +659,14 @@ async def async_setup_entry(
         chime_descs=CHIME_SENSORS,
         viewer_descs=VIEWER_SENSORS,
     )
-    entities += _async_motion_entities(data)
+    entities += _async_event_entities(data)
     entities += _async_nvr_entities(data)
 
     async_add_entities(entities)
 
 
 @callback
-def _async_motion_entities(
+def _async_event_entities(
     data: ProtectData,
     ufp_device: Camera | None = None,
 ) -> list[ProtectDeviceEntity]:
@@ -687,7 +687,7 @@ def _async_motion_entities(
         if not device.feature_flags.has_smart_detect:
             continue
 
-        for event_desc in MOTION_SENSORS:
+        for event_desc in EVENT_SENSORS:
             if not event_desc.has_required(device):
                 continue
 
diff --git a/tests/components/unifiprotect/test_binary_sensor.py b/tests/components/unifiprotect/test_binary_sensor.py
index b2935552855..152628c75f9 100644
--- a/tests/components/unifiprotect/test_binary_sensor.py
+++ b/tests/components/unifiprotect/test_binary_sensor.py
@@ -11,8 +11,8 @@ from pyunifiprotect.data.nvr import EventMetadata
 from homeassistant.components.binary_sensor import BinarySensorDeviceClass
 from homeassistant.components.unifiprotect.binary_sensor import (
     CAMERA_SENSORS,
+    EVENT_SENSORS,
     LIGHT_SENSORS,
-    MOTION_SENSORS,
     SENSE_SENSORS,
 )
 from homeassistant.components.unifiprotect.const import (
@@ -124,7 +124,7 @@ async def test_binary_sensor_setup_camera_all(
 
     entity_registry = er.async_get(hass)
 
-    description = CAMERA_SENSORS[0]
+    description = EVENT_SENSORS[0]
     unique_id, entity_id = ids_from_device_description(
         Platform.BINARY_SENSOR, doorbell, description
     )
@@ -139,7 +139,7 @@ async def test_binary_sensor_setup_camera_all(
     assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION
 
     # Is Dark
-    description = CAMERA_SENSORS[1]
+    description = CAMERA_SENSORS[0]
     unique_id, entity_id = ids_from_device_description(
         Platform.BINARY_SENSOR, doorbell, description
     )
@@ -154,7 +154,7 @@ async def test_binary_sensor_setup_camera_all(
     assert state.attributes[ATTR_ATTRIBUTION] == DEFAULT_ATTRIBUTION
 
     # Motion
-    description = MOTION_SENSORS[0]
+    description = EVENT_SENSORS[1]
     unique_id, entity_id = ids_from_device_description(
         Platform.BINARY_SENSOR, doorbell, description
     )
@@ -179,7 +179,7 @@ async def test_binary_sensor_setup_camera_none(
     assert_entity_counts(hass, Platform.BINARY_SENSOR, 2, 2)
 
     entity_registry = er.async_get(hass)
-    description = CAMERA_SENSORS[1]
+    description = CAMERA_SENSORS[0]
 
     unique_id, entity_id = ids_from_device_description(
         Platform.BINARY_SENSOR, camera, description
@@ -265,7 +265,7 @@ async def test_binary_sensor_update_motion(
     assert_entity_counts(hass, Platform.BINARY_SENSOR, 13, 13)
 
     _, entity_id = ids_from_device_description(
-        Platform.BINARY_SENSOR, doorbell, MOTION_SENSORS[0]
+        Platform.BINARY_SENSOR, doorbell, EVENT_SENSORS[1]
     )
 
     event = Event(
diff --git a/tests/components/unifiprotect/test_sensor.py b/tests/components/unifiprotect/test_sensor.py
index e67195df5e9..f5779e78b1c 100644
--- a/tests/components/unifiprotect/test_sensor.py
+++ b/tests/components/unifiprotect/test_sensor.py
@@ -23,7 +23,7 @@ from homeassistant.components.unifiprotect.sensor import (
     ALL_DEVICES_SENSORS,
     CAMERA_DISABLED_SENSORS,
     CAMERA_SENSORS,
-    MOTION_SENSORS,
+    EVENT_SENSORS,
     MOTION_TRIP_SENSORS,
     NVR_DISABLED_SENSORS,
     NVR_SENSORS,
@@ -399,7 +399,7 @@ async def test_sensor_setup_camera(
 
     # Detected Object
     unique_id, entity_id = ids_from_device_description(
-        Platform.SENSOR, doorbell, MOTION_SENSORS[0]
+        Platform.SENSOR, doorbell, EVENT_SENSORS[0]
     )
 
     entity = entity_registry.async_get(entity_id)
@@ -455,7 +455,7 @@ async def test_sensor_update_motion(
     assert_entity_counts(hass, Platform.SENSOR, 25, 12)
 
     _, entity_id = ids_from_device_description(
-        Platform.SENSOR, doorbell, MOTION_SENSORS[0]
+        Platform.SENSOR, doorbell, EVENT_SENSORS[0]
     )
 
     await enable_entity(hass, ufp.entry.entry_id, entity_id)
@@ -582,7 +582,7 @@ async def test_camera_update_licenseplate(
     assert_entity_counts(hass, Platform.SENSOR, 24, 13)
 
     _, entity_id = ids_from_device_description(
-        Platform.SENSOR, camera, MOTION_SENSORS[1]
+        Platform.SENSOR, camera, EVENT_SENSORS[1]
     )
 
     event_metadata = EventMetadata(