2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ZoneMinder sensors."""
|
2016-10-15 03:56:40 +00:00
|
|
|
import logging
|
2020-09-17 05:58:51 +00:00
|
|
|
from typing import Callable, List, Optional
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2016-12-02 06:17:38 +00:00
|
|
|
import voluptuous as vol
|
2020-09-17 05:58:51 +00:00
|
|
|
from zoneminder.monitor import Monitor, TimePeriod
|
|
|
|
from zoneminder.zm import ZoneMinder
|
2016-12-02 06:17:38 +00:00
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN, PLATFORM_SCHEMA
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2018-01-09 20:58:26 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
2020-09-17 05:58:51 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2019-03-21 05:56:46 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2016-10-15 03:56:40 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
from .common import get_client_from_data, get_platform_configs
|
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
|
|
|
|
|
2018-01-09 20:58:26 +00:00
|
|
|
SENSOR_TYPES = {
|
2019-07-31 19:25:30 +00:00
|
|
|
"all": ["Events"],
|
|
|
|
"hour": ["Events Last Hour"],
|
|
|
|
"day": ["Events Last Day"],
|
|
|
|
"week": ["Events Last Week"],
|
|
|
|
"month": ["Events Last Month"],
|
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(
|
|
|
|
cv.ensure_list, [vol.In(list(SENSOR_TYPES))]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2016-12-02 06:17:38 +00:00
|
|
|
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: Callable[[List[Entity], Optional[bool]], None],
|
|
|
|
) -> None:
|
|
|
|
"""Set up the sensor config entry."""
|
|
|
|
zm_client = get_client_from_data(hass, config_entry.unique_id)
|
|
|
|
monitors = await hass.async_add_job(zm_client.get_monitors)
|
|
|
|
|
|
|
|
if not monitors:
|
|
|
|
_LOGGER.warning("Did not fetch any monitors from ZoneMinder")
|
2016-12-02 06:17:38 +00:00
|
|
|
|
2018-09-15 06:44:48 +00:00
|
|
|
sensors = []
|
2020-09-17 05:58:51 +00:00
|
|
|
for monitor in monitors:
|
|
|
|
sensors.append(ZMSensorMonitors(monitor, config_entry))
|
2019-01-16 09:15:13 +00:00
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
for config in get_platform_configs(hass, SENSOR_DOMAIN):
|
|
|
|
include_archived = config.get(CONF_INCLUDE_ARCHIVED)
|
2018-01-09 20:58:26 +00:00
|
|
|
|
2019-01-16 09:15:13 +00:00
|
|
|
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
2020-09-17 05:58:51 +00:00
|
|
|
sensors.append(
|
|
|
|
ZMSensorEvents(monitor, include_archived, sensor, config_entry)
|
|
|
|
)
|
|
|
|
|
|
|
|
sensors.append(ZMSensorRunState(zm_client, config_entry))
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
async_add_entities(sensors, True)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZMSensorMonitors(Entity):
|
2016-10-29 20:10:42 +00:00
|
|
|
"""Get the status of each ZoneMinder monitor."""
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
def __init__(self, monitor: Monitor, config_entry: ConfigEntry):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Initialize monitor sensor."""
|
2018-09-15 06:44:48 +00:00
|
|
|
self._monitor = monitor
|
2020-09-17 05:58:51 +00:00
|
|
|
self._config_entry = config_entry
|
2018-12-14 07:10:54 +00:00
|
|
|
self._state = None
|
|
|
|
self._is_available = None
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> Optional[str]:
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return f"{self._config_entry.unique_id}_{self._monitor.id}_status"
|
|
|
|
|
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
|
|
|
|
def state(self):
|
|
|
|
"""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."""
|
2018-09-15 06:44:48 +00:00
|
|
|
state = self._monitor.function
|
|
|
|
if not state:
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class ZMSensorEvents(Entity):
|
|
|
|
"""Get the number of events for each monitor."""
|
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
monitor: Monitor,
|
|
|
|
include_archived: bool,
|
|
|
|
sensor_type: str,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Initialize event sensor."""
|
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
|
2018-09-15 06:44:48 +00:00
|
|
|
self.time_period = TimePeriod.get_time_period(sensor_type)
|
2020-09-17 05:58:51 +00:00
|
|
|
self._config_entry = config_entry
|
2016-10-15 03:56:40 +00:00
|
|
|
self._state = None
|
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self) -> Optional[str]:
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return f"{self._config_entry.unique_id}_{self._monitor.id}_{self.time_period.value}_{self._include_archived}_events"
|
|
|
|
|
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
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "Events"
|
2016-10-15 03:56:40 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the sensor."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self._monitor.get_events(self.time_period, self._include_archived)
|
2018-10-11 07:38:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ZMSensorRunState(Entity):
|
|
|
|
"""Get the ZoneMinder run state."""
|
|
|
|
|
2020-09-17 05:58:51 +00:00
|
|
|
def __init__(self, client: ZoneMinder, config_entry: ConfigEntry):
|
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
|
2020-09-17 05:58:51 +00:00
|
|
|
self._config_entry = config_entry
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> Optional[str]:
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return f"{self._config_entry.unique_id}_runstate"
|
2018-10-11 07:38:31 +00:00
|
|
|
|
|
|
|
@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
|
|
|
|
def state(self):
|
|
|
|
"""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
|