Extracting zoneminder to a new library (#16527)

* Migrating out the zoneminder platform (and camera.zoneminder) to a new library

* Clean up the global variable ZM usage

* Modify camera.zoneminder to use the new Monitor class implementation

* Refactor camera.zoneminder after latest refactor in zm-py

* Implementing changes to switch.zoneminder to use zm-py native methods

* Complete migrating over sensor.zoneminder to the zm-py library

* Tweaking ZoneMinder components from code review

* Linting fixes for the zoneminder components

* Directly assign value when turning on/off in switch.zoneminder
This commit is contained in:
Rohan Kapoor 2018-09-14 23:44:48 -07:00 committed by Martin Hjelmare
parent 8ce2d701c2
commit 1ca09ea36f
5 changed files with 76 additions and 242 deletions

View file

@ -4,91 +4,47 @@ Support for ZoneMinder camera streaming.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/camera.zoneminder/
"""
import asyncio
import logging
from urllib.parse import urljoin, urlencode
from homeassistant.const import CONF_NAME
from homeassistant.components.camera.mjpeg import (
CONF_MJPEG_URL, CONF_STILL_IMAGE_URL, MjpegCamera)
from homeassistant.components import zoneminder
from homeassistant.components.zoneminder import DOMAIN as ZONEMINDER_DOMAIN
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['zoneminder']
DOMAIN = 'zoneminder'
# From ZoneMinder's web/includes/config.php.in
ZM_STATE_ALARM = "2"
def _get_image_url(hass, monitor, mode):
zm_data = hass.data[DOMAIN]
query = urlencode({
'mode': mode,
'buffer': monitor['StreamReplayBuffer'],
'monitor': monitor['Id'],
})
url = '{zms_url}?{query}'.format(
zms_url=urljoin(zm_data['server_origin'], zm_data['path_zms']),
query=query,
)
_LOGGER.debug('Monitor %s %s URL (without auth): %s',
monitor['Id'], mode, url)
if not zm_data['username']:
return url
url += '&user={:s}'.format(zm_data['username'])
if not zm_data['password']:
return url
return url + '&pass={:s}'.format(zm_data['password'])
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the ZoneMinder cameras."""
cameras = []
monitors = zoneminder.get_state('api/monitors.json')
zm_client = hass.data[ZONEMINDER_DOMAIN]
monitors = zm_client.get_monitors()
if not monitors:
_LOGGER.warning("Could not fetch monitors from ZoneMinder")
return
for i in monitors['monitors']:
monitor = i['Monitor']
if monitor['Function'] == 'None':
_LOGGER.info("Skipping camera %s", monitor['Id'])
continue
_LOGGER.info("Initializing camera %s", monitor['Id'])
device_info = {
CONF_NAME: monitor['Name'],
CONF_MJPEG_URL: _get_image_url(hass, monitor, 'jpeg'),
CONF_STILL_IMAGE_URL: _get_image_url(hass, monitor, 'single')
}
cameras.append(ZoneMinderCamera(hass, device_info, monitor))
if not cameras:
_LOGGER.warning("No active cameras found")
return
async_add_entities(cameras)
cameras = []
for monitor in monitors:
_LOGGER.info("Initializing camera %s", monitor.id)
cameras.append(ZoneMinderCamera(hass, monitor))
add_entities(cameras)
class ZoneMinderCamera(MjpegCamera):
"""Representation of a ZoneMinder Monitor Stream."""
def __init__(self, hass, device_info, monitor):
def __init__(self, hass, monitor):
"""Initialize as a subclass of MjpegCamera."""
device_info = {
CONF_NAME: monitor.name,
CONF_MJPEG_URL: monitor.mjpeg_image_url,
CONF_STILL_IMAGE_URL: monitor.still_image_url
}
super().__init__(hass, device_info)
self._monitor_id = int(monitor['Id'])
self._is_recording = None
self._monitor = monitor
@property
def should_poll(self):
@ -97,17 +53,8 @@ class ZoneMinderCamera(MjpegCamera):
def update(self):
"""Update our recording state from the ZM API."""
_LOGGER.debug("Updating camera state for monitor %i", self._monitor_id)
status_response = zoneminder.get_state(
'api/monitors/alarm/id:%i/command:status.json' % self._monitor_id
)
if not status_response:
_LOGGER.warning("Could not get status for monitor %i",
self._monitor_id)
return
self._is_recording = status_response.get('status') == ZM_STATE_ALARM
_LOGGER.debug("Updating camera state for monitor %i", self._monitor.id)
self._is_recording = self._monitor.is_recording
@property
def is_recording(self):

View file

@ -8,12 +8,11 @@ import logging
import voluptuous as vol
import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import STATE_UNKNOWN
from homeassistant.components.zoneminder import DOMAIN as ZONEMINDER_DOMAIN
from homeassistant.const import CONF_MONITORED_CONDITIONS
from homeassistant.helpers.entity import Entity
from homeassistant.components import zoneminder
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
@ -43,20 +42,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the ZoneMinder sensor platform."""
include_archived = config.get(CONF_INCLUDE_ARCHIVED)
sensors = []
zm_client = hass.data[ZONEMINDER_DOMAIN]
monitors = zm_client.get_monitors()
if not monitors:
_LOGGER.warning('Could not fetch any monitors from ZoneMinder')
monitors = zoneminder.get_state('api/monitors.json')
for i in monitors['monitors']:
sensors.append(
ZMSensorMonitors(int(i['Monitor']['Id']), i['Monitor']['Name'])
)
sensors = []
for monitor in monitors:
sensors.append(ZMSensorMonitors(monitor))
for sensor in config[CONF_MONITORED_CONDITIONS]:
sensors.append(
ZMSensorEvents(int(i['Monitor']['Id']),
i['Monitor']['Name'],
include_archived, sensor)
)
sensors.append(ZMSensorEvents(monitor, include_archived, sensor))
add_entities(sensors)
@ -64,16 +60,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class ZMSensorMonitors(Entity):
"""Get the status of each ZoneMinder monitor."""
def __init__(self, monitor_id, monitor_name):
def __init__(self, monitor):
"""Initialize monitor sensor."""
self._monitor_id = monitor_id
self._monitor_name = monitor_name
self._state = None
self._monitor = monitor
self._state = monitor.function.value
@property
def name(self):
"""Return the name of the sensor."""
return '{} Status'.format(self._monitor_name)
return '{} Status'.format(self._monitor.name)
@property
def state(self):
@ -82,32 +77,28 @@ class ZMSensorMonitors(Entity):
def update(self):
"""Update the sensor."""
monitor = zoneminder.get_state(
'api/monitors/{}.json'.format(self._monitor_id)
)
if monitor['monitor']['Monitor']['Function'] is None:
self._state = STATE_UNKNOWN
state = self._monitor.function
if not state:
self._state = None
else:
self._state = monitor['monitor']['Monitor']['Function']
self._state = state.value
class ZMSensorEvents(Entity):
"""Get the number of events for each monitor."""
def __init__(self, monitor_id, monitor_name, include_archived,
sensor_type):
def __init__(self, monitor, include_archived, sensor_type):
"""Initialize event sensor."""
self._monitor_id = monitor_id
self._monitor_name = monitor_name
from zoneminder.monitor import TimePeriod
self._monitor = monitor
self._include_archived = include_archived
self._type = sensor_type
self._name = SENSOR_TYPES[sensor_type][0]
self.time_period = TimePeriod.get_time_period(sensor_type)
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return '{} {}'.format(self._monitor_name, self._name)
return '{} {}'.format(self._monitor.name, self.time_period.title)
@property
def unit_of_measurement(self):
@ -121,22 +112,5 @@ class ZMSensorEvents(Entity):
def update(self):
"""Update the sensor."""
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/consoleEvents/{}{}.json'.format(date_filter,
archived_filter)
)
try:
self._state = event['results'][str(self._monitor_id)]
except (TypeError, KeyError):
self._state = '0'
self._state = self._monitor.get_events(
self.time_period, self._include_archived)

View file

@ -9,8 +9,8 @@ import logging
import voluptuous as vol
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
from homeassistant.components.zoneminder import DOMAIN as ZONEMINDER_DOMAIN
from homeassistant.const import (CONF_COMMAND_ON, CONF_COMMAND_OFF)
from homeassistant.components import zoneminder
import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
@ -25,22 +25,20 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the ZoneMinder switch platform."""
on_state = config.get(CONF_COMMAND_ON)
off_state = config.get(CONF_COMMAND_OFF)
from zoneminder.monitor import MonitorState
on_state = MonitorState(config.get(CONF_COMMAND_ON))
off_state = MonitorState(config.get(CONF_COMMAND_OFF))
zm_client = hass.data[ZONEMINDER_DOMAIN]
monitors = zm_client.get_monitors()
if not monitors:
_LOGGER.warning('Could not fetch monitors from ZoneMinder')
return
switches = []
monitors = zoneminder.get_state('api/monitors.json')
for i in monitors['monitors']:
switches.append(
ZMSwitchMonitors(
int(i['Monitor']['Id']),
i['Monitor']['Name'],
on_state,
off_state
)
)
for monitor in monitors:
switches.append(ZMSwitchMonitors(monitor, on_state, off_state))
add_entities(switches)
@ -49,10 +47,9 @@ class ZMSwitchMonitors(SwitchDevice):
icon = 'mdi:record-rec'
def __init__(self, monitor_id, monitor_name, on_state, off_state):
def __init__(self, monitor, on_state, off_state):
"""Initialize the switch."""
self._monitor_id = monitor_id
self._monitor_name = monitor_name
self._monitor = monitor
self._on_state = on_state
self._off_state = off_state
self._state = None
@ -60,15 +57,11 @@ class ZMSwitchMonitors(SwitchDevice):
@property
def name(self):
"""Return the name of the switch."""
return "%s State" % self._monitor_name
return '{}\'s State'.format(self._monitor.name)
def update(self):
"""Update the switch value."""
monitor = zoneminder.get_state(
'api/monitors/%i.json' % self._monitor_id
)
current_state = monitor['monitor']['Monitor']['Function']
self._state = True if current_state == self._on_state else False
self._state = self._monitor.function == self._on_state
@property
def is_on(self):
@ -77,14 +70,8 @@ class ZMSwitchMonitors(SwitchDevice):
def turn_on(self, **kwargs):
"""Turn the entity on."""
zoneminder.change_state(
'api/monitors/%i.json' % self._monitor_id,
{'Monitor[Function]': self._on_state}
)
self._monitor.function = self._on_state
def turn_off(self, **kwargs):
"""Turn the entity off."""
zoneminder.change_state(
'api/monitors/%i.json' % self._monitor_id,
{'Monitor[Function]': self._off_state}
)
self._monitor.function = self._off_state

View file

@ -5,9 +5,7 @@ For more details about this component, please refer to the documentation at
https://home-assistant.io/components/zoneminder/
"""
import logging
from urllib.parse import urljoin
import requests
import voluptuous as vol
from homeassistant.const import (
@ -17,6 +15,8 @@ import homeassistant.helpers.config_validation as cv
_LOGGER = logging.getLogger(__name__)
REQUIREMENTS = ['zm-py==0.0.1']
CONF_PATH_ZMS = 'path_zms'
DEFAULT_PATH = '/zm/'
@ -26,10 +26,6 @@ DEFAULT_TIMEOUT = 10
DEFAULT_VERIFY_SSL = True
DOMAIN = 'zoneminder'
LOGIN_RETRIES = 2
ZM = {}
CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
@ -45,8 +41,7 @@ CONFIG_SCHEMA = vol.Schema({
def setup(hass, config):
"""Set up the ZoneMinder component."""
global ZM
ZM = {}
from zoneminder.zm import ZoneMinder
conf = config[DOMAIN]
if conf[CONF_SSL]:
@ -55,83 +50,11 @@ def setup(hass, config):
schema = 'http'
server_origin = '{}://{}'.format(schema, conf[CONF_HOST])
url = urljoin(server_origin, conf[CONF_PATH])
username = conf.get(CONF_USERNAME, None)
password = conf.get(CONF_PASSWORD, None)
hass.data[DOMAIN] = ZoneMinder(server_origin,
conf.get(CONF_USERNAME),
conf.get(CONF_PASSWORD),
conf.get(CONF_PATH),
conf.get(CONF_PATH_ZMS),
conf.get(CONF_VERIFY_SSL))
ssl_verification = conf.get(CONF_VERIFY_SSL)
ZM['server_origin'] = server_origin
ZM['url'] = url
ZM['username'] = username
ZM['password'] = password
ZM['path_zms'] = conf.get(CONF_PATH_ZMS)
ZM['ssl_verification'] = ssl_verification
hass.data[DOMAIN] = ZM
return login()
def login():
"""Login to the ZoneMinder API."""
_LOGGER.debug("Attempting to login to ZoneMinder")
login_post = {'view': 'console', 'action': 'login'}
if ZM['username']:
login_post['username'] = ZM['username']
if ZM['password']:
login_post['password'] = ZM['password']
req = requests.post(ZM['url'] + '/index.php', data=login_post,
verify=ZM['ssl_verification'], timeout=DEFAULT_TIMEOUT)
ZM['cookies'] = req.cookies
# Login calls returns a 200 response on both failure and success.
# The only way to tell if you logged in correctly is to issue an api call.
req = requests.get(
ZM['url'] + 'api/host/getVersion.json', cookies=ZM['cookies'],
timeout=DEFAULT_TIMEOUT, verify=ZM['ssl_verification'])
if not req.ok:
_LOGGER.error("Connection error logging into ZoneMinder")
return False
return True
def _zm_request(method, api_url, data=None):
"""Perform a Zoneminder request."""
# Since the API uses sessions that expire, sometimes we need to re-auth
# if the call fails.
for _ in range(LOGIN_RETRIES):
req = requests.request(
method, urljoin(ZM['url'], api_url), data=data,
cookies=ZM['cookies'], timeout=DEFAULT_TIMEOUT,
verify=ZM['ssl_verification'])
if not req.ok:
login()
else:
break
else:
_LOGGER.error("Unable to get API response from ZoneMinder")
try:
return req.json()
except ValueError:
_LOGGER.exception(
"JSON decode exception caught while attempting to decode: %s",
req.text)
def get_state(api_url):
"""Get a state from the ZoneMinder API service."""
return _zm_request('get', api_url)
def change_state(api_url, post_data):
"""Update a state using the Zoneminder API."""
return _zm_request('post', api_url, data=post_data)
return hass.data[DOMAIN].login()

View file

@ -1541,3 +1541,6 @@ zigpy-xbee==0.1.1
# homeassistant.components.zha
zigpy==0.2.0
# homeassistant.components.zoneminder
zm-py==0.0.1