Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unifiprotect Add user information retrieval for NFC and fingerprint events #132604

Merged
merged 13 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 52 additions & 10 deletions homeassistant/components/unifiprotect/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

import dataclasses

from uiprotect.data import Camera, EventType, ProtectAdoptableDeviceModel

from homeassistant.components.event import (
EventDeviceClass,
EventEntity,
Expand All @@ -14,17 +12,43 @@
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import Bootstrap
from .const import (
ATTR_EVENT_ID,
EVENT_TYPE_DOORBELL_RING,
EVENT_TYPE_FINGERPRINT_IDENTIFIED,
EVENT_TYPE_FINGERPRINT_NOT_IDENTIFIED,
EVENT_TYPE_NFC_SCANNED,
KEYRINGS_KEY_TYPE_ID_NFC,
KEYRINGS_ULP_ID,
KEYRINGS_USER_FULL_NAME,
KEYRINGS_USER_STATUS,
)
from .data import (
Camera,
EventType,
ProtectAdoptableDeviceModel,
ProtectData,
ProtectDeviceType,
UFPConfigEntry,
)
from .data import ProtectData, ProtectDeviceType, UFPConfigEntry
from .entity import EventEntityMixin, ProtectDeviceEntity, ProtectEventMixin


def _add_ulp_user_infos(
bootstrap: Bootstrap, event_data: dict[str, str], ulp_id: str
) -> None:
"""Add ULP user information to the event data."""
if ulp_usr := bootstrap.ulp_users.by_ulp_id(ulp_id):
event_data.update(
{
KEYRINGS_ULP_ID: ulp_usr.ulp_id,
KEYRINGS_USER_FULL_NAME: ulp_usr.full_name,
KEYRINGS_USER_STATUS: ulp_usr.status,
}
)


@dataclasses.dataclass(frozen=True, kw_only=True)
class ProtectEventEntityDescription(ProtectEventMixin, EventEntityDescription):
"""Describes UniFi Protect event entity."""
Expand Down Expand Up @@ -78,9 +102,22 @@ def _async_update_device_from_protect(self, device: ProtectDeviceType) -> None:
and not self._event_already_ended(prev_event, prev_event_end)
and event.type is EventType.NFC_CARD_SCANNED
):
event_data = {ATTR_EVENT_ID: event.id}
event_data = {
ATTR_EVENT_ID: event.id,
KEYRINGS_USER_FULL_NAME: "",
KEYRINGS_ULP_ID: "",
KEYRINGS_USER_STATUS: "",
KEYRINGS_KEY_TYPE_ID_NFC: "",
}

if event.metadata and event.metadata.nfc and event.metadata.nfc.nfc_id:
event_data["nfc_id"] = event.metadata.nfc.nfc_id
nfc_id = event.metadata.nfc.nfc_id
event_data[KEYRINGS_KEY_TYPE_ID_NFC] = nfc_id
keyring = self.data.api.bootstrap.keyrings.by_registry_id(nfc_id)
if keyring and keyring.ulp_user:
_add_ulp_user_infos(
self.data.api.bootstrap, event_data, keyring.ulp_user
)

self._trigger_event(EVENT_TYPE_NFC_SCANNED, event_data)
self.async_write_ha_state()
Expand Down Expand Up @@ -109,17 +146,22 @@ def _async_update_device_from_protect(self, device: ProtectDeviceType) -> None:
and not self._event_already_ended(prev_event, prev_event_end)
and event.type is EventType.FINGERPRINT_IDENTIFIED
):
event_data = {ATTR_EVENT_ID: event.id}
event_data = {
ATTR_EVENT_ID: event.id,
KEYRINGS_USER_FULL_NAME: "",
KEYRINGS_ULP_ID: "",
}
event_identified = EVENT_TYPE_FINGERPRINT_NOT_IDENTIFIED
if (
event.metadata
and event.metadata.fingerprint
and event.metadata.fingerprint.ulp_id
):
event_data["ulp_id"] = event.metadata.fingerprint.ulp_id
event_identified = EVENT_TYPE_FINGERPRINT_IDENTIFIED
else:
event_data["ulp_id"] = ""
event_identified = EVENT_TYPE_FINGERPRINT_NOT_IDENTIFIED
ulp_id = event.metadata.fingerprint.ulp_id
if ulp_id:
event_data[KEYRINGS_ULP_ID] = ulp_id
_add_ulp_user_infos(self.data.api.bootstrap, event_data, ulp_id)

self._trigger_event(event_identified, event_data)
self.async_write_ha_state()
Expand Down
Loading
Loading