2019-02-13 20:21:14 +00:00
|
|
|
"""Support for ZoneMinder sensors."""
|
2016-10-15 03:56:40 +00:00
|
|
|
import logging
|
|
|
|
|
2016-12-02 06:17:38 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2018-01-09 20:58:26 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
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
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DOMAIN as ZONEMINDER_DOMAIN
|
|
|
|
|
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 = {
|
|
|
|
'all': ['Events'],
|
|
|
|
'hour': ['Events Last Hour'],
|
|
|
|
'day': ['Events Last Day'],
|
|
|
|
'week': ['Events Last Week'],
|
|
|
|
'month': ['Events Last Month'],
|
|
|
|
}
|
|
|
|
|
2016-12-02 06:17:38 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Optional(CONF_INCLUDE_ARCHIVED, default=DEFAULT_INCLUDE_ARCHIVED):
|
|
|
|
cv.boolean,
|
2018-01-09 20:58:26 +00:00
|
|
|
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
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2016-10-29 20:10:42 +00:00
|
|
|
"""Set up the ZoneMinder sensor platform."""
|
2016-12-02 06:17:38 +00:00
|
|
|
include_archived = config.get(CONF_INCLUDE_ARCHIVED)
|
|
|
|
|
2018-09-15 06:44:48 +00:00
|
|
|
sensors = []
|
2019-01-16 09:15:13 +00:00
|
|
|
for zm_client in hass.data[ZONEMINDER_DOMAIN].values():
|
|
|
|
monitors = zm_client.get_monitors()
|
|
|
|
if not monitors:
|
|
|
|
_LOGGER.warning('Could not fetch any monitors from ZoneMinder')
|
|
|
|
|
|
|
|
for monitor in monitors:
|
|
|
|
sensors.append(ZMSensorMonitors(monitor))
|
2018-01-09 20:58:26 +00:00
|
|
|
|
2019-01-16 09:15:13 +00:00
|
|
|
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
|
|
|
sensors.append(
|
|
|
|
ZMSensorEvents(monitor, include_archived, sensor)
|
|
|
|
)
|
2016-10-15 03:56:40 +00:00
|
|
|
|
2019-01-16 09:15:13 +00:00
|
|
|
sensors.append(ZMSensorRunState(zm_client))
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors)
|
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
|
|
|
|
2018-09-15 06:44:48 +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."""
|
2018-09-15 06:44:48 +00:00
|
|
|
return '{} Status'.format(self._monitor.name)
|
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."""
|
|
|
|
|
2018-09-15 06:44:48 +00:00
|
|
|
def __init__(self, monitor, include_archived, sensor_type):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Initialize event sensor."""
|
2018-09-15 06:44:48 +00:00
|
|
|
from zoneminder.monitor import TimePeriod
|
|
|
|
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)
|
2016-10-15 03:56:40 +00:00
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
2018-09-15 06:44:48 +00:00
|
|
|
return '{} {}'.format(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."""
|
|
|
|
return 'Events'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Update the sensor."""
|
2018-09-15 06:44:48 +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."""
|
|
|
|
|
|
|
|
def __init__(self, client):
|
|
|
|
"""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."""
|
|
|
|
return 'Run State'
|
|
|
|
|
|
|
|
@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
|