mirror of
https://github.com/home-assistant/core.git
synced 2026-04-17 23:53:49 +01:00
97 lines
2.8 KiB
Python
97 lines
2.8 KiB
Python
"""Event platform for the UniFi Access integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from homeassistant.components.event import (
|
|
EventDeviceClass,
|
|
EventEntity,
|
|
EventEntityDescription,
|
|
)
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
|
|
from .coordinator import DoorEvent, UnifiAccessConfigEntry, UnifiAccessCoordinator
|
|
from .entity import UnifiAccessEntity
|
|
|
|
PARALLEL_UPDATES = 0
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class UnifiAccessEventEntityDescription(EventEntityDescription):
|
|
"""Describes a UniFi Access event entity."""
|
|
|
|
category: str
|
|
|
|
|
|
DOORBELL_EVENT_DESCRIPTION = UnifiAccessEventEntityDescription(
|
|
key="doorbell",
|
|
translation_key="doorbell",
|
|
device_class=EventDeviceClass.DOORBELL,
|
|
event_types=["ring"],
|
|
category="doorbell",
|
|
)
|
|
|
|
ACCESS_EVENT_DESCRIPTION = UnifiAccessEventEntityDescription(
|
|
key="access",
|
|
translation_key="access",
|
|
event_types=["access_granted", "access_denied"],
|
|
category="access",
|
|
)
|
|
|
|
EVENT_DESCRIPTIONS: list[UnifiAccessEventEntityDescription] = [
|
|
DOORBELL_EVENT_DESCRIPTION,
|
|
ACCESS_EVENT_DESCRIPTION,
|
|
]
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: UnifiAccessConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Set up UniFi Access event entities."""
|
|
coordinator = entry.runtime_data
|
|
async_add_entities(
|
|
UnifiAccessEventEntity(coordinator, door_id, description)
|
|
for door_id in coordinator.data
|
|
for description in EVENT_DESCRIPTIONS
|
|
)
|
|
|
|
|
|
class UnifiAccessEventEntity(UnifiAccessEntity, EventEntity):
|
|
"""Representation of a UniFi Access event entity."""
|
|
|
|
entity_description: UnifiAccessEventEntityDescription
|
|
|
|
def __init__(
|
|
self,
|
|
coordinator: UnifiAccessCoordinator,
|
|
door_id: str,
|
|
description: UnifiAccessEventEntityDescription,
|
|
) -> None:
|
|
"""Initialize the event entity."""
|
|
door = coordinator.data[door_id]
|
|
super().__init__(coordinator, door, description.key)
|
|
self.entity_description = description
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
"""Subscribe to door events when added to hass."""
|
|
await super().async_added_to_hass()
|
|
self.async_on_remove(
|
|
self.coordinator.async_subscribe_door_events(self._async_handle_event)
|
|
)
|
|
|
|
@callback
|
|
def _async_handle_event(self, event: DoorEvent) -> None:
|
|
"""Handle incoming event from coordinator."""
|
|
if (
|
|
event.door_id != self._door_id
|
|
or event.category != self.entity_description.category
|
|
or event.event_type not in self.event_types
|
|
):
|
|
return
|
|
self._trigger_event(event.event_type, event.event_data)
|
|
self.async_write_ha_state()
|