mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
ZoneMinder event sensor updates (#11369)
* Switch the ZoneMinder event sensor over to use the consoleEvents API, and add monitored_conditions for 'hour', 'day', 'week', 'month' and 'all'. 'all' is enabled by default to provide backward compatibility with the old behaviour. * - Switch to new string formatting - Remove redundant validator * De-lint
This commit is contained in:
parent
85858885c8
commit
13042d5557
@ -10,6 +10,7 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.components.zoneminder as zoneminder
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
@ -22,9 +23,19 @@ CONF_INCLUDE_ARCHIVED = "include_archived"
|
||||
|
||||
DEFAULT_INCLUDE_ARCHIVED = False
|
||||
|
||||
SENSOR_TYPES = {
|
||||
'all': ['Events'],
|
||||
'hour': ['Events Last Hour'],
|
||||
'day': ['Events Last Day'],
|
||||
'week': ['Events Last Week'],
|
||||
'month': ['Events Last Month'],
|
||||
}
|
||||
|
||||
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))]),
|
||||
})
|
||||
|
||||
|
||||
@ -39,9 +50,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
sensors.append(
|
||||
ZMSensorMonitors(int(i['Monitor']['Id']), i['Monitor']['Name'])
|
||||
)
|
||||
|
||||
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
||||
sensors.append(
|
||||
ZMSensorEvents(int(i['Monitor']['Id']), i['Monitor']['Name'],
|
||||
include_archived)
|
||||
ZMSensorEvents(int(i['Monitor']['Id']),
|
||||
i['Monitor']['Name'],
|
||||
include_archived, sensor)
|
||||
)
|
||||
|
||||
add_devices(sensors)
|
||||
@ -69,7 +83,7 @@ class ZMSensorMonitors(Entity):
|
||||
def update(self):
|
||||
"""Update the sensor."""
|
||||
monitor = zoneminder.get_state(
|
||||
'api/monitors/%i.json' % self._monitor_id
|
||||
'api/monitors/{}.json'.format(self._monitor_id)
|
||||
)
|
||||
if monitor['monitor']['Monitor']['Function'] is None:
|
||||
self._state = STATE_UNKNOWN
|
||||
@ -80,17 +94,20 @@ class ZMSensorMonitors(Entity):
|
||||
class ZMSensorEvents(Entity):
|
||||
"""Get the number of events for each monitor."""
|
||||
|
||||
def __init__(self, monitor_id, monitor_name, include_archived):
|
||||
def __init__(self, monitor_id, monitor_name, include_archived,
|
||||
sensor_type):
|
||||
"""Initialize event sensor."""
|
||||
self._monitor_id = monitor_id
|
||||
self._monitor_name = monitor_name
|
||||
self._include_archived = include_archived
|
||||
self._type = sensor_type
|
||||
self._name = SENSOR_TYPES[sensor_type][0]
|
||||
self._state = None
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return '{} Events'.format(self._monitor_name)
|
||||
return '{} {}'.format(self._monitor_name, self._name)
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
@ -104,13 +121,22 @@ class ZMSensorEvents(Entity):
|
||||
|
||||
def update(self):
|
||||
"""Update the sensor."""
|
||||
archived_filter = '/Archived:0'
|
||||
date_filter = '1%20{}'.format(self._type)
|
||||
if self._type == 'all':
|
||||
# The consoleEvents API uses DATE_SUB, so give it
|
||||
# something large
|
||||
date_filter = '100%20year'
|
||||
|
||||
archived_filter = '/Archived=:0'
|
||||
if self._include_archived:
|
||||
archived_filter = ''
|
||||
|
||||
event = zoneminder.get_state(
|
||||
'api/events/index/MonitorId:%i%s.json' % (self._monitor_id,
|
||||
'api/events/consoleEvents/{}{}.json'.format(date_filter,
|
||||
archived_filter)
|
||||
)
|
||||
|
||||
self._state = event['pagination']['count']
|
||||
try:
|
||||
self._state = event['results'][str(self._monitor_id)]
|
||||
except (TypeError, KeyError):
|
||||
self._state = '0'
|
||||
|
Loading…
x
Reference in New Issue
Block a user