1 """Platform providing event entities for UniFi Protect."""
3 from __future__
import annotations
7 from uiprotect.data
import Camera, EventType, ProtectAdoptableDeviceModel
12 EventEntityDescription,
19 EVENT_TYPE_DOORBELL_RING,
20 EVENT_TYPE_FINGERPRINT_IDENTIFIED,
21 EVENT_TYPE_FINGERPRINT_NOT_IDENTIFIED,
22 EVENT_TYPE_NFC_SCANNED,
24 from .data
import ProtectData, ProtectDeviceType, UFPConfigEntry
25 from .entity
import EventEntityMixin, ProtectDeviceEntity, ProtectEventMixin
28 @dataclasses.dataclass(frozen=True, kw_only=True)
30 """Describes UniFi Protect event entity."""
32 entity_class: type[ProtectDeviceEntity]
36 """A UniFi Protect event entity."""
38 entity_description: ProtectEventEntityDescription
44 prev_event = self.
_event_event
47 if event := description.get_event_obj(device):
54 and event.type
is EventType.RING
56 self.
_trigger_event_trigger_event(EVENT_TYPE_DOORBELL_RING, {ATTR_EVENT_ID: event.id})
61 """A UniFi Protect NFC event entity."""
63 entity_description: ProtectEventEntityDescription
69 prev_event = self.
_event_event
72 if event := description.get_event_obj(device):
79 and event.type
is EventType.NFC_CARD_SCANNED
81 event_data = {ATTR_EVENT_ID: event.id}
82 if event.metadata
and event.metadata.nfc
and event.metadata.nfc.nfc_id:
83 event_data[
"nfc_id"] = event.metadata.nfc.nfc_id
85 self.
_trigger_event_trigger_event(EVENT_TYPE_NFC_SCANNED, event_data)
90 EventEntityMixin, ProtectDeviceEntity, EventEntity
92 """A UniFi Protect fingerprint event entity."""
94 entity_description: ProtectEventEntityDescription
100 prev_event = self.
_event_event
103 if event := description.get_event_obj(device):
110 and event.type
is EventType.FINGERPRINT_IDENTIFIED
112 event_data = {ATTR_EVENT_ID: event.id}
115 and event.metadata.fingerprint
116 and event.metadata.fingerprint.ulp_id
118 event_data[
"ulp_id"] = event.metadata.fingerprint.ulp_id
119 event_identified = EVENT_TYPE_FINGERPRINT_IDENTIFIED
121 event_data[
"ulp_id"] =
""
122 event_identified = EVENT_TYPE_FINGERPRINT_NOT_IDENTIFIED
128 EVENT_DESCRIPTIONS: tuple[ProtectEventEntityDescription, ...] = (
131 translation_key=
"doorbell",
132 device_class=EventDeviceClass.DOORBELL,
133 icon=
"mdi:doorbell-video",
134 ufp_required_field=
"feature_flags.is_doorbell",
135 ufp_event_obj=
"last_ring_event",
136 event_types=[EVENT_TYPE_DOORBELL_RING],
137 entity_class=ProtectDeviceRingEventEntity,
141 translation_key=
"nfc",
142 device_class=EventDeviceClass.DOORBELL,
144 ufp_required_field=
"feature_flags.support_nfc",
145 ufp_event_obj=
"last_nfc_card_scanned_event",
146 event_types=[EVENT_TYPE_NFC_SCANNED],
147 entity_class=ProtectDeviceNFCEventEntity,
151 translation_key=
"fingerprint",
152 device_class=EventDeviceClass.DOORBELL,
153 icon=
"mdi:fingerprint",
154 ufp_required_field=
"feature_flags.has_fingerprint_sensor",
155 ufp_event_obj=
"last_fingerprint_identified_event",
157 EVENT_TYPE_FINGERPRINT_IDENTIFIED,
158 EVENT_TYPE_FINGERPRINT_NOT_IDENTIFIED,
160 entity_class=ProtectDeviceFingerprintEventEntity,
168 ufp_device: ProtectAdoptableDeviceModel |
None =
None,
169 ) -> list[ProtectDeviceEntity]:
171 description.entity_class(data, device, description)
172 for device
in (data.get_cameras()
if ufp_device
is None else [ufp_device])
173 for description
in EVENT_DESCRIPTIONS
174 if description.has_required(device)
180 entry: UFPConfigEntry,
181 async_add_entities: AddEntitiesCallback,
183 """Set up event entities for UniFi Protect integration."""
184 data = entry.runtime_data
187 def _add_new_device(device: ProtectAdoptableDeviceModel) ->
None:
188 if device.is_adopted
and isinstance(device, Camera):
191 data.async_subscribe_adopt(_add_new_device)
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
bool _event_already_ended(self, Event|None prev_event, datetime|None prev_event_end)
None _async_update_device_from_protect(self, ProtectDeviceType device)
None _async_update_device_from_protect(self, ProtectDeviceType device)
None _async_update_device_from_protect(self, ProtectDeviceType device)
None async_write_ha_state(self)
list[ProtectDeviceEntity] _async_event_entities(ProtectData data, ProtectAdoptableDeviceModel|None ufp_device=None)
None async_setup_entry(HomeAssistant hass, UFPConfigEntry entry, AddEntitiesCallback async_add_entities)