Skip to content

Commit

Permalink
Extracting zoneminder to a new library (home-assistant#16527)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rohankapoorcom authored and MartinHjelmare committed Sep 15, 2018
1 parent 8ce2d70 commit 1ca09ea
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 242 deletions.
91 changes: 19 additions & 72 deletions homeassistant/components/camera/zoneminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,91 +4,47 @@
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):
Expand All @@ -97,17 +53,8 @@ def should_poll(self):

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):
Expand Down
76 changes: 25 additions & 51 deletions homeassistant/components/sensor/zoneminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

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__)

Expand Down Expand Up @@ -43,37 +42,33 @@ 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)


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):
Expand All @@ -82,32 +77,28 @@ def state(self):

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):
Expand All @@ -121,22 +112,5 @@ def state(self):

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)
49 changes: 18 additions & 31 deletions homeassistant/components/switch/zoneminder.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
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__)
Expand All @@ -25,22 +25,20 @@

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))

switches = []
zm_client = hass.data[ZONEMINDER_DOMAIN]

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
)
)
monitors = zm_client.get_monitors()
if not monitors:
_LOGGER.warning('Could not fetch monitors from ZoneMinder')
return

switches = []
for monitor in monitors:
switches.append(ZMSwitchMonitors(monitor, on_state, off_state))
add_entities(switches)


Expand All @@ -49,26 +47,21 @@ 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

@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):
Expand All @@ -77,14 +70,8 @@ def is_on(self):

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
Loading

0 comments on commit 1ca09ea

Please sign in to comment.