mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
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:
parent
8ce2d701c2
commit
1ca09ea36f
@ -4,91 +4,47 @@ Support for ZoneMinder camera streaming.
|
|||||||
For more details about this platform, please refer to the documentation at
|
For more details about this platform, please refer to the documentation at
|
||||||
https://home-assistant.io/components/camera.zoneminder/
|
https://home-assistant.io/components/camera.zoneminder/
|
||||||
"""
|
"""
|
||||||
import asyncio
|
|
||||||
import logging
|
import logging
|
||||||
from urllib.parse import urljoin, urlencode
|
|
||||||
|
|
||||||
from homeassistant.const import CONF_NAME
|
from homeassistant.const import CONF_NAME
|
||||||
from homeassistant.components.camera.mjpeg import (
|
from homeassistant.components.camera.mjpeg import (
|
||||||
CONF_MJPEG_URL, CONF_STILL_IMAGE_URL, MjpegCamera)
|
CONF_MJPEG_URL, CONF_STILL_IMAGE_URL, MjpegCamera)
|
||||||
|
from homeassistant.components.zoneminder import DOMAIN as ZONEMINDER_DOMAIN
|
||||||
from homeassistant.components import zoneminder
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEPENDENCIES = ['zoneminder']
|
DEPENDENCIES = ['zoneminder']
|
||||||
DOMAIN = 'zoneminder'
|
|
||||||
|
|
||||||
# From ZoneMinder's web/includes/config.php.in
|
|
||||||
ZM_STATE_ALARM = "2"
|
|
||||||
|
|
||||||
|
|
||||||
def _get_image_url(hass, monitor, mode):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
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):
|
|
||||||
"""Set up the ZoneMinder cameras."""
|
"""Set up the ZoneMinder cameras."""
|
||||||
cameras = []
|
zm_client = hass.data[ZONEMINDER_DOMAIN]
|
||||||
monitors = zoneminder.get_state('api/monitors.json')
|
|
||||||
|
monitors = zm_client.get_monitors()
|
||||||
if not monitors:
|
if not monitors:
|
||||||
_LOGGER.warning("Could not fetch monitors from ZoneMinder")
|
_LOGGER.warning("Could not fetch monitors from ZoneMinder")
|
||||||
return
|
return
|
||||||
|
|
||||||
for i in monitors['monitors']:
|
cameras = []
|
||||||
monitor = i['Monitor']
|
for monitor in monitors:
|
||||||
|
_LOGGER.info("Initializing camera %s", monitor.id)
|
||||||
if monitor['Function'] == 'None':
|
cameras.append(ZoneMinderCamera(hass, monitor))
|
||||||
_LOGGER.info("Skipping camera %s", monitor['Id'])
|
add_entities(cameras)
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
class ZoneMinderCamera(MjpegCamera):
|
class ZoneMinderCamera(MjpegCamera):
|
||||||
"""Representation of a ZoneMinder Monitor Stream."""
|
"""Representation of a ZoneMinder Monitor Stream."""
|
||||||
|
|
||||||
def __init__(self, hass, device_info, monitor):
|
def __init__(self, hass, monitor):
|
||||||
"""Initialize as a subclass of MjpegCamera."""
|
"""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)
|
super().__init__(hass, device_info)
|
||||||
self._monitor_id = int(monitor['Id'])
|
|
||||||
self._is_recording = None
|
self._is_recording = None
|
||||||
|
self._monitor = monitor
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def should_poll(self):
|
def should_poll(self):
|
||||||
@ -97,17 +53,8 @@ class ZoneMinderCamera(MjpegCamera):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update our recording state from the ZM API."""
|
"""Update our recording state from the ZM API."""
|
||||||
_LOGGER.debug("Updating camera state for monitor %i", self._monitor_id)
|
_LOGGER.debug("Updating camera state for monitor %i", self._monitor.id)
|
||||||
status_response = zoneminder.get_state(
|
self._is_recording = self._monitor.is_recording
|
||||||
'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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_recording(self):
|
def is_recording(self):
|
||||||
|
@ -8,12 +8,11 @@ import logging
|
|||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
|
import homeassistant.helpers.config_validation as cv
|
||||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
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.const import CONF_MONITORED_CONDITIONS
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
from homeassistant.components import zoneminder
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -43,20 +42,17 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
"""Set up the ZoneMinder sensor platform."""
|
"""Set up the ZoneMinder sensor platform."""
|
||||||
include_archived = config.get(CONF_INCLUDE_ARCHIVED)
|
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')
|
sensors = []
|
||||||
for i in monitors['monitors']:
|
for monitor in monitors:
|
||||||
sensors.append(
|
sensors.append(ZMSensorMonitors(monitor))
|
||||||
ZMSensorMonitors(int(i['Monitor']['Id']), i['Monitor']['Name'])
|
|
||||||
)
|
|
||||||
|
|
||||||
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
for sensor in config[CONF_MONITORED_CONDITIONS]:
|
||||||
sensors.append(
|
sensors.append(ZMSensorEvents(monitor, include_archived, sensor))
|
||||||
ZMSensorEvents(int(i['Monitor']['Id']),
|
|
||||||
i['Monitor']['Name'],
|
|
||||||
include_archived, sensor)
|
|
||||||
)
|
|
||||||
|
|
||||||
add_entities(sensors)
|
add_entities(sensors)
|
||||||
|
|
||||||
@ -64,16 +60,15 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
|
|||||||
class ZMSensorMonitors(Entity):
|
class ZMSensorMonitors(Entity):
|
||||||
"""Get the status of each ZoneMinder monitor."""
|
"""Get the status of each ZoneMinder monitor."""
|
||||||
|
|
||||||
def __init__(self, monitor_id, monitor_name):
|
def __init__(self, monitor):
|
||||||
"""Initialize monitor sensor."""
|
"""Initialize monitor sensor."""
|
||||||
self._monitor_id = monitor_id
|
self._monitor = monitor
|
||||||
self._monitor_name = monitor_name
|
self._state = monitor.function.value
|
||||||
self._state = None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return '{} Status'.format(self._monitor_name)
|
return '{} Status'.format(self._monitor.name)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
@ -82,32 +77,28 @@ class ZMSensorMonitors(Entity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
monitor = zoneminder.get_state(
|
state = self._monitor.function
|
||||||
'api/monitors/{}.json'.format(self._monitor_id)
|
if not state:
|
||||||
)
|
self._state = None
|
||||||
if monitor['monitor']['Monitor']['Function'] is None:
|
|
||||||
self._state = STATE_UNKNOWN
|
|
||||||
else:
|
else:
|
||||||
self._state = monitor['monitor']['Monitor']['Function']
|
self._state = state.value
|
||||||
|
|
||||||
|
|
||||||
class ZMSensorEvents(Entity):
|
class ZMSensorEvents(Entity):
|
||||||
"""Get the number of events for each monitor."""
|
"""Get the number of events for each monitor."""
|
||||||
|
|
||||||
def __init__(self, monitor_id, monitor_name, include_archived,
|
def __init__(self, monitor, include_archived, sensor_type):
|
||||||
sensor_type):
|
|
||||||
"""Initialize event sensor."""
|
"""Initialize event sensor."""
|
||||||
self._monitor_id = monitor_id
|
from zoneminder.monitor import TimePeriod
|
||||||
self._monitor_name = monitor_name
|
self._monitor = monitor
|
||||||
self._include_archived = include_archived
|
self._include_archived = include_archived
|
||||||
self._type = sensor_type
|
self.time_period = TimePeriod.get_time_period(sensor_type)
|
||||||
self._name = SENSOR_TYPES[sensor_type][0]
|
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return '{} {}'.format(self._monitor_name, self._name)
|
return '{} {}'.format(self._monitor.name, self.time_period.title)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
@ -121,22 +112,5 @@ class ZMSensorEvents(Entity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
date_filter = '1%20{}'.format(self._type)
|
self._state = self._monitor.get_events(
|
||||||
if self._type == 'all':
|
self.time_period, self._include_archived)
|
||||||
# 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'
|
|
||||||
|
@ -9,8 +9,8 @@ import logging
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
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.const import (CONF_COMMAND_ON, CONF_COMMAND_OFF)
|
||||||
from homeassistant.components import zoneminder
|
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
@ -25,22 +25,20 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|||||||
|
|
||||||
def setup_platform(hass, config, add_entities, discovery_info=None):
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
||||||
"""Set up the ZoneMinder switch platform."""
|
"""Set up the ZoneMinder switch platform."""
|
||||||
on_state = config.get(CONF_COMMAND_ON)
|
from zoneminder.monitor import MonitorState
|
||||||
off_state = config.get(CONF_COMMAND_OFF)
|
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 = []
|
switches = []
|
||||||
|
for monitor in monitors:
|
||||||
monitors = zoneminder.get_state('api/monitors.json')
|
switches.append(ZMSwitchMonitors(monitor, on_state, off_state))
|
||||||
for i in monitors['monitors']:
|
|
||||||
switches.append(
|
|
||||||
ZMSwitchMonitors(
|
|
||||||
int(i['Monitor']['Id']),
|
|
||||||
i['Monitor']['Name'],
|
|
||||||
on_state,
|
|
||||||
off_state
|
|
||||||
)
|
|
||||||
)
|
|
||||||
|
|
||||||
add_entities(switches)
|
add_entities(switches)
|
||||||
|
|
||||||
|
|
||||||
@ -49,10 +47,9 @@ class ZMSwitchMonitors(SwitchDevice):
|
|||||||
|
|
||||||
icon = 'mdi:record-rec'
|
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."""
|
"""Initialize the switch."""
|
||||||
self._monitor_id = monitor_id
|
self._monitor = monitor
|
||||||
self._monitor_name = monitor_name
|
|
||||||
self._on_state = on_state
|
self._on_state = on_state
|
||||||
self._off_state = off_state
|
self._off_state = off_state
|
||||||
self._state = None
|
self._state = None
|
||||||
@ -60,15 +57,11 @@ class ZMSwitchMonitors(SwitchDevice):
|
|||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the switch."""
|
"""Return the name of the switch."""
|
||||||
return "%s State" % self._monitor_name
|
return '{}\'s State'.format(self._monitor.name)
|
||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Update the switch value."""
|
"""Update the switch value."""
|
||||||
monitor = zoneminder.get_state(
|
self._state = self._monitor.function == self._on_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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self):
|
def is_on(self):
|
||||||
@ -77,14 +70,8 @@ class ZMSwitchMonitors(SwitchDevice):
|
|||||||
|
|
||||||
def turn_on(self, **kwargs):
|
def turn_on(self, **kwargs):
|
||||||
"""Turn the entity on."""
|
"""Turn the entity on."""
|
||||||
zoneminder.change_state(
|
self._monitor.function = self._on_state
|
||||||
'api/monitors/%i.json' % self._monitor_id,
|
|
||||||
{'Monitor[Function]': self._on_state}
|
|
||||||
)
|
|
||||||
|
|
||||||
def turn_off(self, **kwargs):
|
def turn_off(self, **kwargs):
|
||||||
"""Turn the entity off."""
|
"""Turn the entity off."""
|
||||||
zoneminder.change_state(
|
self._monitor.function = self._off_state
|
||||||
'api/monitors/%i.json' % self._monitor_id,
|
|
||||||
{'Monitor[Function]': self._off_state}
|
|
||||||
)
|
|
||||||
|
@ -5,9 +5,7 @@ For more details about this component, please refer to the documentation at
|
|||||||
https://home-assistant.io/components/zoneminder/
|
https://home-assistant.io/components/zoneminder/
|
||||||
"""
|
"""
|
||||||
import logging
|
import logging
|
||||||
from urllib.parse import urljoin
|
|
||||||
|
|
||||||
import requests
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
@ -17,6 +15,8 @@ import homeassistant.helpers.config_validation as cv
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
REQUIREMENTS = ['zm-py==0.0.1']
|
||||||
|
|
||||||
CONF_PATH_ZMS = 'path_zms'
|
CONF_PATH_ZMS = 'path_zms'
|
||||||
|
|
||||||
DEFAULT_PATH = '/zm/'
|
DEFAULT_PATH = '/zm/'
|
||||||
@ -26,10 +26,6 @@ DEFAULT_TIMEOUT = 10
|
|||||||
DEFAULT_VERIFY_SSL = True
|
DEFAULT_VERIFY_SSL = True
|
||||||
DOMAIN = 'zoneminder'
|
DOMAIN = 'zoneminder'
|
||||||
|
|
||||||
LOGIN_RETRIES = 2
|
|
||||||
|
|
||||||
ZM = {}
|
|
||||||
|
|
||||||
CONFIG_SCHEMA = vol.Schema({
|
CONFIG_SCHEMA = vol.Schema({
|
||||||
DOMAIN: vol.Schema({
|
DOMAIN: vol.Schema({
|
||||||
vol.Required(CONF_HOST): cv.string,
|
vol.Required(CONF_HOST): cv.string,
|
||||||
@ -45,8 +41,7 @@ CONFIG_SCHEMA = vol.Schema({
|
|||||||
|
|
||||||
def setup(hass, config):
|
def setup(hass, config):
|
||||||
"""Set up the ZoneMinder component."""
|
"""Set up the ZoneMinder component."""
|
||||||
global ZM
|
from zoneminder.zm import ZoneMinder
|
||||||
ZM = {}
|
|
||||||
|
|
||||||
conf = config[DOMAIN]
|
conf = config[DOMAIN]
|
||||||
if conf[CONF_SSL]:
|
if conf[CONF_SSL]:
|
||||||
@ -55,83 +50,11 @@ def setup(hass, config):
|
|||||||
schema = 'http'
|
schema = 'http'
|
||||||
|
|
||||||
server_origin = '{}://{}'.format(schema, conf[CONF_HOST])
|
server_origin = '{}://{}'.format(schema, conf[CONF_HOST])
|
||||||
url = urljoin(server_origin, conf[CONF_PATH])
|
hass.data[DOMAIN] = ZoneMinder(server_origin,
|
||||||
username = conf.get(CONF_USERNAME, None)
|
conf.get(CONF_USERNAME),
|
||||||
password = conf.get(CONF_PASSWORD, None)
|
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)
|
return hass.data[DOMAIN].login()
|
||||||
|
|
||||||
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)
|
|
||||||
|
@ -1541,3 +1541,6 @@ zigpy-xbee==0.1.1
|
|||||||
|
|
||||||
# homeassistant.components.zha
|
# homeassistant.components.zha
|
||||||
zigpy==0.2.0
|
zigpy==0.2.0
|
||||||
|
|
||||||
|
# homeassistant.components.zoneminder
|
||||||
|
zm-py==0.0.1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user