2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ZoneMinder sensors."""
|
2021-09-08 06:48:55 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-10-15 03:56:40 +00:00
|
|
|
import logging
|
|
|
|
|
2016-12-02 06:17:38 +00:00
|
|
|
import voluptuous as vol
|
2020-10-07 14:28:49 +00:00
|
|
|
from zoneminder.monitor import TimePeriod
|
2016-12-02 06:17:38 +00:00
|
|
|
|
2021-09-08 06:48:55 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2018-01-09 20:58:26 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
2022-01-05 16:21:46 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-05 16:21:46 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
from . import DOMAIN as ZONEMINDER_DOMAIN
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2016-10-15 03:56:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2016-12-02 06:17:38 +00:00
|
|
|
CONF_INCLUDE_ARCHIVED = "include_archived"
|
|
|
|
|
|
|
|
DEFAULT_INCLUDE_ARCHIVED = False
|
|
|
|
|
2021-09-08 06:48:55 +00:00
|
|
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="all",
|
|
|
|
name="Events",
|
|
|
|
),
|
|
|
|
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]
|
2018-01-09 20:58:26 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_INCLUDE_ARCHIVED, default=DEFAULT_INCLUDE_ARCHIVED
|
|
|
|
): cv.boolean,
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=["all"]): vol.All(
|
2021-09-08 06:48:55 +00:00
|
|
|
cv.ensure_list, [vol.In(SENSOR_KEYS)]
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-12-02 06:17:38 +00:00
|
|
|
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2022-01-05 16:21:46 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2020-10-07 14:28:49 +00:00
|
|
|
"""Set up the ZoneMinder sensor platform."""
|
2021-09-08 06:48:55 +00:00
|
|
|
include_archived = config[CONF_INCLUDE_ARCHIVED]
|
|
|
|
monitored_conditions = config[CONF_MONITORED_CONDITIONS]
|
2016-12-02 06:17:38 +00:00
|
|
|
|
2022-01-05 16:21:46 +00:00
|
|
|
sensors: list[SensorEntity] = []
|
2020-10-07 14:28:49 +00:00
|
|
|
for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
|
2021-10-31 17:35:27 +00:00
|
|
|
if not (monitors := zm_client.get_monitors()):
|
2020-10-07 14:28:49 +00:00
|
|
|
_LOGGER.warning("Could not fetch any monitors from ZoneMinder")
|
2019-01-16 09:15:13 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
for monitor in monitors:
|
|
|
|
sensors.append(ZMSensorMonitors(monitor))
|
2018-01-09 20:58:26 +00:00
|
|
|
|
2021-09-08 06:48:55 +00:00
|
|
|
sensors.extend(
|
|
|
|
[
|
|
|
|
ZMSensorEvents(monitor, include_archived, description)
|
|
|
|
for description in SENSOR_TYPES
|
|
|
|
if description.key in monitored_conditions
|
|
|
|
]
|
|
|
|
)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
sensors.append(ZMSensorRunState(zm_client))
|
|
|
|
add_entities(sensors)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
class ZMSensorMonitors(SensorEntity):
|
2016-10-29 20:10:42 +00:00
|
|
|
"""Get the status of each ZoneMinder monitor."""
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
def __init__(self, monitor):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Initialize monitor sensor."""
|
2018-09-15 06:44:48 +00:00
|
|
|
self._monitor = monitor
|
2018-12-14 07:10:54 +00:00
|
|
|
self._state = None
|
|
|
|
self._is_available = None
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 19:15:31 +00:00
|
|
|
return f"{self._monitor.name} Status"
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self):
|
2016-10-15 03:56:40 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
2018-12-14 07:10:54 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if Monitor is available."""
|
|
|
|
return self._is_available
|
|
|
|
|
2016-10-15 03:56:40 +00:00
|
|
|
def update(self):
|
|
|
|
"""Update the sensor."""
|
2021-10-17 18:02:42 +00:00
|
|
|
if not (state := self._monitor.function):
|
2018-09-15 06:44:48 +00:00
|
|
|
self._state = None
|
2016-10-15 03:56:40 +00:00
|
|
|
else:
|
2018-09-15 06:44:48 +00:00
|
|
|
self._state = state.value
|
2018-12-14 07:10:54 +00:00
|
|
|
self._is_available = self._monitor.is_available
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
class ZMSensorEvents(SensorEntity):
|
2016-10-15 03:56:40 +00:00
|
|
|
"""Get the number of events for each monitor."""
|
|
|
|
|
2021-09-08 06:48:55 +00:00
|
|
|
_attr_native_unit_of_measurement = "Events"
|
|
|
|
|
|
|
|
def __init__(self, monitor, include_archived, description: SensorEntityDescription):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Initialize event sensor."""
|
2021-09-08 06:48:55 +00:00
|
|
|
self.entity_description = description
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-09-15 06:44:48 +00:00
|
|
|
self._monitor = monitor
|
2016-12-02 06:17:38 +00:00
|
|
|
self._include_archived = include_archived
|
2021-09-08 06:48:55 +00:00
|
|
|
self.time_period = TimePeriod.get_time_period(description.key)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-09-03 19:15:31 +00:00
|
|
|
return f"{self._monitor.name} {self.time_period.title}"
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the sensor."""
|
2021-09-08 06:48:55 +00:00
|
|
|
self._attr_native_value = self._monitor.get_events(
|
|
|
|
self.time_period, self._include_archived
|
|
|
|
)
|
2018-10-11 07:38:31 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:50:29 +00:00
|
|
|
class ZMSensorRunState(SensorEntity):
|
2018-10-11 07:38:31 +00:00
|
|
|
"""Get the ZoneMinder run state."""
|
|
|
|
|
2020-10-07 14:28:49 +00:00
|
|
|
def __init__(self, client):
|
2018-10-11 07:38:31 +00:00
|
|
|
"""Initialize run state sensor."""
|
|
|
|
self._state = None
|
2018-12-14 07:10:54 +00:00
|
|
|
self._is_available = None
|
2018-10-11 07:38:31 +00:00
|
|
|
self._client = client
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "Run State"
|
2018-10-11 07:38:31 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-11 19:17:16 +00:00
|
|
|
def native_value(self):
|
2018-10-11 07:38:31 +00:00
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
2018-12-14 07:10:54 +00:00
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return True if ZoneMinder is available."""
|
|
|
|
return self._is_available
|
|
|
|
|
2018-10-11 07:38:31 +00:00
|
|
|
def update(self):
|
|
|
|
"""Update the sensor."""
|
|
|
|
self._state = self._client.get_active_state()
|
2018-12-14 07:10:54 +00:00
|
|
|
self._is_available = self._client.is_available
|