mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Maintenance zoneminder (#4102)
* Add timeout to requests, fix typos, and defaults * Clean-up
This commit is contained in:
parent
9ea1101aba
commit
942d630762
@ -1,13 +1,14 @@
|
||||
"""
|
||||
Support for Zoneminder Sensors.
|
||||
Support for ZoneMinder Sensors.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.zoneminder/
|
||||
"""
|
||||
import logging
|
||||
|
||||
import homeassistant.components.zoneminder as zoneminder
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.components.zoneminder as zoneminder
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
@ -15,7 +16,7 @@ DEPENDENCIES = ['zoneminder']
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup Zoneminder platform."""
|
||||
"""Set up the ZoneMinder sensor platform."""
|
||||
sensors = []
|
||||
|
||||
monitors = zoneminder.get_state('api/monitors.json')
|
||||
@ -31,7 +32,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
|
||||
class ZMSensorMonitors(Entity):
|
||||
"""Get the status of each monitor."""
|
||||
"""Get the status of each ZoneMinder monitor."""
|
||||
|
||||
def __init__(self, monitor_id, monitor_name):
|
||||
"""Initiate monitor sensor."""
|
||||
@ -42,7 +43,7 @@ class ZMSensorMonitors(Entity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return "%s Status" % self._monitor_name
|
||||
return '{} Status'.format(self._monitor_name)
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
@ -55,7 +56,7 @@ class ZMSensorMonitors(Entity):
|
||||
'api/monitors/%i.json' % self._monitor_id
|
||||
)
|
||||
if monitor['monitor']['Monitor']['Function'] is None:
|
||||
self._state = "None"
|
||||
self._state = STATE_UNKNOWN
|
||||
else:
|
||||
self._state = monitor['monitor']['Monitor']['Function']
|
||||
|
||||
@ -72,7 +73,7 @@ class ZMSensorEvents(Entity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return "%s Events" % self._monitor_name
|
||||
return '{} Events'.format(self._monitor_name)
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
|
@ -1,5 +1,5 @@
|
||||
"""
|
||||
Support for Zoneminder switches.
|
||||
Support for ZoneMinder switches.
|
||||
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/switch.zoneminder/
|
||||
@ -10,23 +10,21 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.components.switch import (SwitchDevice, PLATFORM_SCHEMA)
|
||||
from homeassistant.const import (CONF_COMMAND_ON, CONF_COMMAND_OFF)
|
||||
import homeassistant.components.zoneminder as zoneminder
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
import homeassistant.components.zoneminder as zoneminder
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEPENDENCIES = ['zoneminder']
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_COMMAND_ON): cv.string,
|
||||
vol.Required(CONF_COMMAND_OFF): cv.string,
|
||||
})
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DEPENDENCIES = ['zoneminder']
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
"""Setup the Zoneminder switch."""
|
||||
"""Set up the ZoneMinder switch platform."""
|
||||
on_state = config.get(CONF_COMMAND_ON)
|
||||
off_state = config.get(CONF_COMMAND_OFF)
|
||||
|
||||
@ -47,7 +45,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
|
||||
|
||||
class ZMSwitchMonitors(SwitchDevice):
|
||||
"""Representation of an zoneminder switch."""
|
||||
"""Representation of a ZoneMinder switch."""
|
||||
|
||||
icon = 'mdi:record-rec'
|
||||
|
||||
|
@ -1,10 +1,9 @@
|
||||
"""
|
||||
Support for Zoneminder.
|
||||
Support for ZoneMinder.
|
||||
|
||||
For more details about this component, please refer to the documentation at
|
||||
https://home-assistant.io/components/zoneminder/
|
||||
"""
|
||||
|
||||
import logging
|
||||
import json
|
||||
from urllib.parse import urljoin
|
||||
@ -12,43 +11,44 @@ from urllib.parse import urljoin
|
||||
import requests
|
||||
import voluptuous as vol
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.const import (
|
||||
CONF_PATH, CONF_HOST, CONF_SSL, CONF_PASSWORD, CONF_USERNAME)
|
||||
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
REQUIREMENTS = []
|
||||
|
||||
DEFAULT_PATH = '/zm/'
|
||||
DEFAULT_SSL = False
|
||||
DEFAULT_TIMEOUT = 10
|
||||
DOMAIN = 'zoneminder'
|
||||
|
||||
LOGIN_RETRIES = 2
|
||||
|
||||
ZM = {}
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema({
|
||||
DOMAIN: vol.Schema({
|
||||
vol.Required(CONF_HOST): cv.string,
|
||||
vol.Optional(CONF_SSL, default=False): cv.boolean,
|
||||
vol.Optional(CONF_PATH, default="/zm/"): cv.string,
|
||||
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
||||
vol.Optional(CONF_PATH, default=DEFAULT_PATH): cv.string,
|
||||
vol.Optional(CONF_USERNAME): cv.string,
|
||||
vol.Optional(CONF_PASSWORD): cv.string
|
||||
})
|
||||
}, extra=vol.ALLOW_EXTRA)
|
||||
|
||||
LOGIN_RETRIES = 2
|
||||
ZM = {}
|
||||
|
||||
|
||||
def setup(hass, config):
|
||||
"""Setup the zonminder platform."""
|
||||
"""Set up the ZoneMinder component."""
|
||||
global ZM
|
||||
ZM = {}
|
||||
|
||||
conf = config[DOMAIN]
|
||||
if conf[CONF_SSL]:
|
||||
schema = "https"
|
||||
schema = 'https'
|
||||
else:
|
||||
schema = "http"
|
||||
schema = 'http'
|
||||
|
||||
url = urljoin(schema + "://" + conf[CONF_HOST], conf[CONF_PATH])
|
||||
url = urljoin('{}://{}'.format(schema, conf[CONF_HOST]), conf[CONF_PATH])
|
||||
username = conf.get(CONF_USERNAME, None)
|
||||
password = conf.get(CONF_PASSWORD, None)
|
||||
|
||||
@ -61,8 +61,8 @@ def setup(hass, config):
|
||||
|
||||
# pylint: disable=no-member
|
||||
def login():
|
||||
"""Login to the zoneminder api."""
|
||||
_LOGGER.debug("Attempting to login to zoneminder")
|
||||
"""Login to the ZoneMinder API."""
|
||||
_LOGGER.debug("Attempting to login to ZoneMinder")
|
||||
|
||||
login_post = {'view': 'console', 'action': 'login'}
|
||||
if ZM['username']:
|
||||
@ -73,12 +73,11 @@ def login():
|
||||
req = requests.post(ZM['url'] + '/index.php', data=login_post)
|
||||
ZM['cookies'] = req.cookies
|
||||
|
||||
# Login calls returns a 200 repsonse on both failure and success..
|
||||
# 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']
|
||||
)
|
||||
ZM['url'] + 'api/host/getVersion.json', cookies=ZM['cookies'],
|
||||
timeout=DEFAULT_TIMEOUT)
|
||||
|
||||
if req.status_code != requests.codes.ok:
|
||||
_LOGGER.error("Connection error logging into ZoneMinder")
|
||||
@ -89,18 +88,19 @@ def login():
|
||||
|
||||
# pylint: disable=no-member
|
||||
def get_state(api_url):
|
||||
"""Get a state from the zoneminder API service."""
|
||||
# Since the API uses sessions that expire, sometimes we need
|
||||
# to re-auth if the call fails.
|
||||
"""Get a state from the ZoneMinder API service."""
|
||||
# Since the API uses sessions that expire, sometimes we need to re-auth
|
||||
# if the call fails.
|
||||
for _ in range(LOGIN_RETRIES):
|
||||
req = requests.get(urljoin(ZM['url'], api_url), cookies=ZM['cookies'])
|
||||
req = requests.get(urljoin(ZM['url'], api_url), cookies=ZM['cookies'],
|
||||
timeout=DEFAULT_TIMEOUT)
|
||||
|
||||
if req.status_code != requests.codes.ok:
|
||||
login()
|
||||
else:
|
||||
break
|
||||
else:
|
||||
_LOGGER.exception("Unable to get API response")
|
||||
_LOGGER.exception("Unable to get API response from ZoneMinder")
|
||||
|
||||
return json.loads(req.text)
|
||||
|
||||
@ -110,9 +110,8 @@ def change_state(api_url, post_data):
|
||||
"""Update a state using the Zoneminder API."""
|
||||
for _ in range(LOGIN_RETRIES):
|
||||
req = requests.post(
|
||||
urljoin(ZM['url'], api_url),
|
||||
data=post_data,
|
||||
cookies=ZM['cookies'])
|
||||
urljoin(ZM['url'], api_url), data=post_data, cookies=ZM['cookies'],
|
||||
timeout=DEFAULT_TIMEOUT)
|
||||
|
||||
if req.status_code != requests.codes.ok:
|
||||
login()
|
||||
@ -120,6 +119,6 @@ def change_state(api_url, post_data):
|
||||
break
|
||||
|
||||
else:
|
||||
_LOGGER.exception("Unable to get API response")
|
||||
_LOGGER.exception("Unable to get API response from ZoneMinder")
|
||||
|
||||
return json.loads(req.text)
|
||||
|
Loading…
x
Reference in New Issue
Block a user