1 """Support for ZoneMinder sensors."""
3 from __future__
import annotations
7 import voluptuous
as vol
8 from zoneminder.monitor
import Monitor, TimePeriod
9 from zoneminder.zm
import ZoneMinder
12 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
14 SensorEntityDescription,
23 from .
import DOMAIN
as ZONEMINDER_DOMAIN
25 _LOGGER = logging.getLogger(__name__)
27 CONF_INCLUDE_ARCHIVED =
"include_archived"
29 DEFAULT_INCLUDE_ARCHIVED =
False
31 SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
38 name=
"Events Last Hour",
42 name=
"Events Last Day",
46 name=
"Events Last Week",
50 name=
"Events Last Month",
54 SENSOR_KEYS: list[str] = [desc.key
for desc
in SENSOR_TYPES]
56 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
59 CONF_INCLUDE_ARCHIVED, default=DEFAULT_INCLUDE_ARCHIVED
61 vol.Optional(CONF_MONITORED_CONDITIONS, default=[
"all"]): vol.All(
62 cv.ensure_list, [vol.In(SENSOR_KEYS)]
71 add_entities: AddEntitiesCallback,
72 discovery_info: DiscoveryInfoType |
None =
None,
74 """Set up the ZoneMinder sensor platform."""
75 include_archived = config[CONF_INCLUDE_ARCHIVED]
76 monitored_conditions = config[CONF_MONITORED_CONDITIONS]
78 sensors: list[SensorEntity] = []
80 for zm_client
in hass.data[ZONEMINDER_DOMAIN].values():
81 if not (monitors := zm_client.get_monitors()):
83 "Sensor could not fetch any monitors from ZoneMinder"
86 for monitor
in monitors:
92 for description
in SENSOR_TYPES
93 if description.key
in monitored_conditions
102 """Get the status of each ZoneMinder monitor."""
105 """Initialize monitor sensor."""
111 """Update the sensor."""
112 if not (state := self.
_monitor_monitor.function):
120 """Get the number of events for each monitor."""
122 _attr_native_unit_of_measurement =
"Events"
127 include_archived: bool,
128 description: SensorEntityDescription,
130 """Initialize event sensor."""
135 self.
time_periodtime_period = TimePeriod.get_time_period(description.key)
136 self.
_attr_name_attr_name = f
"{monitor.name} {self.time_period.title}"
139 """Update the sensor."""
146 """Get the ZoneMinder run state."""
148 _attr_name =
"Run State"
151 """Initialize run state sensor."""
156 """Update the sensor."""
None __init__(self, Monitor monitor, bool include_archived, SensorEntityDescription description)
None __init__(self, Monitor monitor)
None __init__(self, ZoneMinder client)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)