Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Support for ZoneMinder sensors."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import voluptuous as vol
8 from zoneminder.monitor import Monitor, TimePeriod
9 from zoneminder.zm import ZoneMinder
10 
12  PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
13  SensorEntity,
14  SensorEntityDescription,
15 )
16 from homeassistant.const import CONF_MONITORED_CONDITIONS
17 from homeassistant.core import HomeAssistant
18 from homeassistant.exceptions import PlatformNotReady
20 from homeassistant.helpers.entity_platform import AddEntitiesCallback
21 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
22 
23 from . import DOMAIN as ZONEMINDER_DOMAIN
24 
25 _LOGGER = logging.getLogger(__name__)
26 
27 CONF_INCLUDE_ARCHIVED = "include_archived"
28 
29 DEFAULT_INCLUDE_ARCHIVED = False
30 
31 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
33  key="all",
34  name="Events",
35  ),
37  key="hour",
38  name="Events Last Hour",
39  ),
41  key="day",
42  name="Events Last Day",
43  ),
45  key="week",
46  name="Events Last Week",
47  ),
49  key="month",
50  name="Events Last Month",
51  ),
52 )
53 
54 SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
55 
56 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
57  {
58  vol.Optional(
59  CONF_INCLUDE_ARCHIVED, default=DEFAULT_INCLUDE_ARCHIVED
60  ): cv.boolean,
61  vol.Optional(CONF_MONITORED_CONDITIONS, default=["all"]): vol.All(
62  cv.ensure_list, [vol.In(SENSOR_KEYS)]
63  ),
64  }
65 )
66 
67 
69  hass: HomeAssistant,
70  config: ConfigType,
71  add_entities: AddEntitiesCallback,
72  discovery_info: DiscoveryInfoType | None = None,
73 ) -> None:
74  """Set up the ZoneMinder sensor platform."""
75  include_archived = config[CONF_INCLUDE_ARCHIVED]
76  monitored_conditions = config[CONF_MONITORED_CONDITIONS]
77 
78  sensors: list[SensorEntity] = []
79  zm_client: ZoneMinder
80  for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
81  if not (monitors := zm_client.get_monitors()):
82  raise PlatformNotReady(
83  "Sensor could not fetch any monitors from ZoneMinder"
84  )
85 
86  for monitor in monitors:
87  sensors.append(ZMSensorMonitors(monitor))
88 
89  sensors.extend(
90  [
91  ZMSensorEvents(monitor, include_archived, description)
92  for description in SENSOR_TYPES
93  if description.key in monitored_conditions
94  ]
95  )
96 
97  sensors.append(ZMSensorRunState(zm_client))
98  add_entities(sensors)
99 
100 
102  """Get the status of each ZoneMinder monitor."""
103 
104  def __init__(self, monitor: Monitor) -> None:
105  """Initialize monitor sensor."""
106  self._monitor_monitor = monitor
107  self._attr_available_attr_available = False
108  self._attr_name_attr_name = f"{self._monitor.name} Status"
109 
110  def update(self) -> None:
111  """Update the sensor."""
112  if not (state := self._monitor_monitor.function):
113  self._attr_native_value_attr_native_value = None
114  else:
115  self._attr_native_value_attr_native_value = state.value
116  self._attr_available_attr_available = self._monitor_monitor.is_available
117 
118 
120  """Get the number of events for each monitor."""
121 
122  _attr_native_unit_of_measurement = "Events"
123 
124  def __init__(
125  self,
126  monitor: Monitor,
127  include_archived: bool,
128  description: SensorEntityDescription,
129  ) -> None:
130  """Initialize event sensor."""
131  self.entity_descriptionentity_description = description
132 
133  self._monitor_monitor = monitor
134  self._include_archived_include_archived = include_archived
135  self.time_periodtime_period = TimePeriod.get_time_period(description.key)
136  self._attr_name_attr_name = f"{monitor.name} {self.time_period.title}"
137 
138  def update(self) -> None:
139  """Update the sensor."""
140  self._attr_native_value_attr_native_value = self._monitor_monitor.get_events(
141  self.time_periodtime_period, self._include_archived_include_archived
142  )
143 
144 
146  """Get the ZoneMinder run state."""
147 
148  _attr_name = "Run State"
149 
150  def __init__(self, client: ZoneMinder) -> None:
151  """Initialize run state sensor."""
152  self._attr_available_attr_available = False
153  self._client_client = client
154 
155  def update(self) -> None:
156  """Update the sensor."""
157  self._attr_native_value_attr_native_value = self._client_client.get_active_state()
158  self._attr_available_attr_available = self._client_client.is_available
None __init__(self, Monitor monitor, bool include_archived, SensorEntityDescription description)
Definition: sensor.py:129
def add_entities(account, async_add_entities, tracked)
Definition: sensor.py:40
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: sensor.py:73