mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Use EntityDescription - zoneminder (#55922)
This commit is contained in:
parent
7195b8222b
commit
5429a67877
@ -1,10 +1,16 @@
|
|||||||
"""Support for ZoneMinder sensors."""
|
"""Support for ZoneMinder sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
from zoneminder.monitor import TimePeriod
|
from zoneminder.monitor import TimePeriod
|
||||||
|
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
PLATFORM_SCHEMA,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
@ -16,13 +22,30 @@ CONF_INCLUDE_ARCHIVED = "include_archived"
|
|||||||
|
|
||||||
DEFAULT_INCLUDE_ARCHIVED = False
|
DEFAULT_INCLUDE_ARCHIVED = False
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"all": ["Events"],
|
SensorEntityDescription(
|
||||||
"hour": ["Events Last Hour"],
|
key="all",
|
||||||
"day": ["Events Last Day"],
|
name="Events",
|
||||||
"week": ["Events Last Week"],
|
),
|
||||||
"month": ["Events Last Month"],
|
SensorEntityDescription(
|
||||||
}
|
key="hour",
|
||||||
|
name="Events Last Hour",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="day",
|
||||||
|
name="Events Last Day",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="week",
|
||||||
|
name="Events Last Week",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="month",
|
||||||
|
name="Events Last Month",
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
SENSOR_KEYS: list[str] = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{
|
{
|
||||||
@ -30,7 +53,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
CONF_INCLUDE_ARCHIVED, default=DEFAULT_INCLUDE_ARCHIVED
|
CONF_INCLUDE_ARCHIVED, default=DEFAULT_INCLUDE_ARCHIVED
|
||||||
): cv.boolean,
|
): cv.boolean,
|
||||||
vol.Optional(CONF_MONITORED_CONDITIONS, default=["all"]): vol.All(
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=["all"]): vol.All(
|
||||||
cv.ensure_list, [vol.In(list(SENSOR_TYPES))]
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
||||||
),
|
),
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
@ -38,7 +61,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the ZoneMinder sensor platform."""
|
"""Set up the ZoneMinder sensor platform."""
|
||||||
include_archived = config.get(CONF_INCLUDE_ARCHIVED)
|
include_archived = config[CONF_INCLUDE_ARCHIVED]
|
||||||
|
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
||||||
|
|
||||||
sensors = []
|
sensors = []
|
||||||
for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
|
for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
|
||||||
@ -49,8 +73,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
for monitor in monitors:
|
for monitor in monitors:
|
||||||
sensors.append(ZMSensorMonitors(monitor))
|
sensors.append(ZMSensorMonitors(monitor))
|
||||||
|
|
||||||
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
sensors.extend(
|
||||||
sensors.append(ZMSensorEvents(monitor, include_archived, sensor))
|
[
|
||||||
|
ZMSensorEvents(monitor, include_archived, description)
|
||||||
|
for description in SENSOR_TYPES
|
||||||
|
if description.key in monitored_conditions
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
sensors.append(ZMSensorRunState(zm_client))
|
sensors.append(ZMSensorRunState(zm_client))
|
||||||
add_entities(sensors)
|
add_entities(sensors)
|
||||||
@ -93,32 +122,26 @@ class ZMSensorMonitors(SensorEntity):
|
|||||||
class ZMSensorEvents(SensorEntity):
|
class ZMSensorEvents(SensorEntity):
|
||||||
"""Get the number of events for each monitor."""
|
"""Get the number of events for each monitor."""
|
||||||
|
|
||||||
def __init__(self, monitor, include_archived, sensor_type):
|
_attr_native_unit_of_measurement = "Events"
|
||||||
|
|
||||||
|
def __init__(self, monitor, include_archived, description: SensorEntityDescription):
|
||||||
"""Initialize event sensor."""
|
"""Initialize event sensor."""
|
||||||
|
self.entity_description = description
|
||||||
|
|
||||||
self._monitor = monitor
|
self._monitor = monitor
|
||||||
self._include_archived = include_archived
|
self._include_archived = include_archived
|
||||||
self.time_period = TimePeriod.get_time_period(sensor_type)
|
self.time_period = TimePeriod.get_time_period(description.key)
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return f"{self._monitor.name} {self.time_period.title}"
|
return f"{self._monitor.name} {self.time_period.title}"
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement of this entity, if any."""
|
|
||||||
return "Events"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
self._state = self._monitor.get_events(self.time_period, self._include_archived)
|
self._attr_native_value = self._monitor.get_events(
|
||||||
|
self.time_period, self._include_archived
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ZMSensorRunState(SensorEntity):
|
class ZMSensorRunState(SensorEntity):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user