Home Assistant Unofficial Reference 2024.12.1
entity.py
Go to the documentation of this file.
1 """Base classes for Axis entities."""
2 
3 from __future__ import annotations
4 
5 from abc import abstractmethod
6 from collections.abc import Callable
7 from dataclasses import dataclass
8 from typing import TYPE_CHECKING
9 
10 from axis.models.event import Event, EventTopic
11 
12 from homeassistant.core import callback
13 from homeassistant.helpers.device_registry import DeviceInfo
14 from homeassistant.helpers.dispatcher import async_dispatcher_connect
15 from homeassistant.helpers.entity import Entity, EntityDescription
16 
17 from .const import DOMAIN as AXIS_DOMAIN
18 
19 if TYPE_CHECKING:
20  from .hub import AxisHub
21 
22 TOPIC_TO_EVENT_TYPE = {
23  EventTopic.DAY_NIGHT_VISION: "DayNight",
24  EventTopic.FENCE_GUARD: "Fence Guard",
25  EventTopic.LIGHT_STATUS: "Light",
26  EventTopic.LOITERING_GUARD: "Loitering Guard",
27  EventTopic.MOTION_DETECTION: "Motion",
28  EventTopic.MOTION_DETECTION_3: "VMD3",
29  EventTopic.MOTION_DETECTION_4: "VMD4",
30  EventTopic.MOTION_GUARD: "Motion Guard",
31  EventTopic.OBJECT_ANALYTICS: "Object Analytics",
32  EventTopic.PIR: "PIR",
33  EventTopic.PORT_INPUT: "Input",
34  EventTopic.PORT_SUPERVISED_INPUT: "Supervised Input",
35  EventTopic.PTZ_IS_MOVING: "is_moving",
36  EventTopic.PTZ_ON_PRESET: "on_preset",
37  EventTopic.RELAY: "Relay",
38  EventTopic.SOUND_TRIGGER_LEVEL: "Sound",
39 }
40 
41 
42 @dataclass(frozen=True, kw_only=True)
44  """Axis event based entity description."""
45 
46  event_topic: tuple[EventTopic, ...] | EventTopic
47  """Event topic that provides state updates."""
48  name_fn: Callable[[AxisHub, Event], str] = lambda hub, event: ""
49  """Function providing the corresponding name to the event ID."""
50  supported_fn: Callable[[AxisHub, Event], bool] = lambda hub, event: True
51  """Function validating if event is supported."""
52 
53 
55  """Base common to all Axis entities."""
56 
57  _attr_has_entity_name = True
58 
59  def __init__(self, hub: AxisHub) -> None:
60  """Initialize the Axis event."""
61  self.hubhub = hub
62 
63  self._attr_device_info_attr_device_info = DeviceInfo(
64  identifiers={(AXIS_DOMAIN, hub.unique_id)},
65  serial_number=hub.unique_id,
66  )
67 
68  async def async_added_to_hass(self) -> None:
69  """Subscribe device events."""
70  self.async_on_removeasync_on_remove(
72  self.hasshass,
73  self.hubhub.signal_reachable,
74  self.async_signal_reachable_callbackasync_signal_reachable_callback,
75  )
76  )
77 
78  @callback
80  """Call when device connection state change."""
81  self._attr_available_attr_available = self.hubhub.available
82  self.async_write_ha_stateasync_write_ha_state()
83 
84 
86  """Base common to all Axis entities from event stream."""
87 
88  entity_description: AxisEventDescription
89 
90  _attr_should_poll = False
91 
92  def __init__(
93  self, hub: AxisHub, description: AxisEventDescription, event: Event
94  ) -> None:
95  """Initialize the Axis event."""
96  super().__init__(hub)
97 
98  self.entity_descriptionentity_description = description
99 
100  self._event_id_event_id = event.id
101  self._event_topic_event_topic = event.topic_base
102 
103  event_type = TOPIC_TO_EVENT_TYPE[event.topic_base]
104  self._attr_name_attr_name = description.name_fn(hub, event) or f"{event_type} {event.id}"
105 
106  self._attr_unique_id_attr_unique_id = f"{hub.unique_id}-{event.topic}-{event.id}"
107 
108  @callback
109  @abstractmethod
110  def async_event_callback(self, event: Event) -> None:
111  """Update the entities state."""
112 
113  async def async_added_to_hass(self) -> None:
114  """Subscribe sensors events."""
115  await super().async_added_to_hass()
116  self.async_on_removeasync_on_remove(
117  self.hubhub.api.event.subscribe(
118  self.async_event_callbackasync_event_callback,
119  id_filter=self._event_id_event_id,
120  topic_filter=self._event_topic_event_topic,
121  )
122  )
None __init__(self, AxisHub hub, AxisEventDescription description, Event event)
Definition: entity.py:94
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
Definition: dispatcher.py:103