Home Assistant Unofficial Reference 2024.12.1
event.py
Go to the documentation of this file.
1 """Component providing support for ring events."""
2 
3 from dataclasses import dataclass
4 from typing import Generic
5 
6 from ring_doorbell import RingCapability, RingEvent as RingAlert
7 from ring_doorbell.const import KIND_DING, KIND_INTERCOM_UNLOCK, KIND_MOTION
8 
10  EventDeviceClass,
11  EventEntity,
12  EventEntityDescription,
13 )
14 from homeassistant.core import HomeAssistant, callback
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import RingConfigEntry
18 from .coordinator import RingListenCoordinator
19 from .entity import RingBaseEntity, RingDeviceT
20 
21 
22 @dataclass(frozen=True, kw_only=True)
24  """Base class for event entity description."""
25 
26  capability: RingCapability
27 
28 
29 EVENT_DESCRIPTIONS: tuple[RingEventEntityDescription, ...] = (
31  key=KIND_DING,
32  translation_key=KIND_DING,
33  device_class=EventDeviceClass.DOORBELL,
34  event_types=[KIND_DING],
35  capability=RingCapability.DING,
36  ),
38  key=KIND_MOTION,
39  translation_key=KIND_MOTION,
40  device_class=EventDeviceClass.MOTION,
41  event_types=[KIND_MOTION],
42  capability=RingCapability.MOTION_DETECTION,
43  ),
45  key=KIND_INTERCOM_UNLOCK,
46  translation_key=KIND_INTERCOM_UNLOCK,
47  device_class=EventDeviceClass.BUTTON,
48  event_types=[KIND_INTERCOM_UNLOCK],
49  capability=RingCapability.OPEN,
50  ),
51 )
52 
53 
55  hass: HomeAssistant,
56  entry: RingConfigEntry,
57  async_add_entities: AddEntitiesCallback,
58 ) -> None:
59  """Set up events for a Ring device."""
60  ring_data = entry.runtime_data
61  listen_coordinator = ring_data.listen_coordinator
62 
64  RingEvent(device, listen_coordinator, description)
65  for description in EVENT_DESCRIPTIONS
66  for device in ring_data.devices.all_devices
67  if device.has_capability(description.capability)
68  )
69 
70 
71 class RingEvent(RingBaseEntity[RingListenCoordinator, RingDeviceT], EventEntity):
72  """An event implementation for Ring device."""
73 
74  entity_description: RingEventEntityDescription[RingDeviceT]
75 
76  def __init__(
77  self,
78  device: RingDeviceT,
79  coordinator: RingListenCoordinator,
80  description: RingEventEntityDescription[RingDeviceT],
81  ) -> None:
82  """Initialize a event entity for Ring device."""
83  super().__init__(device, coordinator)
84  self.entity_descriptionentity_description = description
85  self._attr_unique_id_attr_unique_id = f"{device.id}-{description.key}"
86 
87  @callback
88  def _async_handle_event(self, event: str) -> None:
89  """Handle the event."""
90  self._trigger_event_trigger_event(event)
91 
92  def _get_coordinator_alert(self) -> RingAlert | None:
93  return self.coordinator.alerts.get(
94  (self._device_device.device_api_id, self.entity_descriptionentity_description.key)
95  )
96 
97  @callback
98  def _handle_coordinator_update(self) -> None:
99  if (alert := self._get_coordinator_alert_get_coordinator_alert()) and not alert.is_update:
100  self._async_handle_event_async_handle_event(alert.kind)
102 
103  @property
104  def available(self) -> bool:
105  """Return if entity is available."""
106  return self.coordinator.event_listener.started
107 
108  async def async_update(self) -> None:
109  """All updates are passive."""
None _trigger_event(self, str event_type, dict[str, Any]|None event_attributes=None)
Definition: __init__.py:148
RingAlert|None _get_coordinator_alert(self)
Definition: event.py:92
None __init__(self, RingDeviceT device, RingListenCoordinator coordinator, RingEventEntityDescription[RingDeviceT] description)
Definition: event.py:81
None _async_handle_event(self, str event)
Definition: event.py:88
None async_setup_entry(HomeAssistant hass, RingConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: event.py:58