mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Zoneminder SSL fix (#16157)
* Update zoneminder.py Added a verify_ssl parameter for zoneminder * PEP8 fixup * PEP8 indenting fix * Fix lint issue * Remove whitespace
This commit is contained in:
parent
617802653f
commit
f929c38e98
@ -11,16 +11,19 @@ import requests
|
|||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
CONF_PATH, CONF_HOST, CONF_SSL, CONF_PASSWORD, CONF_USERNAME)
|
CONF_HOST, CONF_PASSWORD, CONF_PATH, CONF_SSL, CONF_USERNAME,
|
||||||
|
CONF_VERIFY_SSL)
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
CONF_PATH_ZMS = 'path_zms'
|
CONF_PATH_ZMS = 'path_zms'
|
||||||
|
|
||||||
DEFAULT_PATH = '/zm/'
|
DEFAULT_PATH = '/zm/'
|
||||||
DEFAULT_PATH_ZMS = '/zm/cgi-bin/nph-zms'
|
DEFAULT_PATH_ZMS = '/zm/cgi-bin/nph-zms'
|
||||||
DEFAULT_SSL = False
|
DEFAULT_SSL = False
|
||||||
DEFAULT_TIMEOUT = 10
|
DEFAULT_TIMEOUT = 10
|
||||||
|
DEFAULT_VERIFY_SSL = True
|
||||||
DOMAIN = 'zoneminder'
|
DOMAIN = 'zoneminder'
|
||||||
|
|
||||||
LOGIN_RETRIES = 2
|
LOGIN_RETRIES = 2
|
||||||
@ -30,12 +33,12 @@ 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,
|
||||||
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
vol.Optional(CONF_PASSWORD): cv.string,
|
||||||
vol.Optional(CONF_PATH, default=DEFAULT_PATH): cv.string,
|
vol.Optional(CONF_PATH, default=DEFAULT_PATH): cv.string,
|
||||||
# This should match PATH_ZMS in ZoneMinder settings.
|
|
||||||
vol.Optional(CONF_PATH_ZMS, default=DEFAULT_PATH_ZMS): cv.string,
|
vol.Optional(CONF_PATH_ZMS, default=DEFAULT_PATH_ZMS): cv.string,
|
||||||
|
vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
|
||||||
vol.Optional(CONF_USERNAME): cv.string,
|
vol.Optional(CONF_USERNAME): cv.string,
|
||||||
vol.Optional(CONF_PASSWORD): cv.string
|
vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
|
||||||
})
|
})
|
||||||
}, extra=vol.ALLOW_EXTRA)
|
}, extra=vol.ALLOW_EXTRA)
|
||||||
|
|
||||||
@ -56,11 +59,14 @@ def setup(hass, config):
|
|||||||
username = conf.get(CONF_USERNAME, None)
|
username = conf.get(CONF_USERNAME, None)
|
||||||
password = conf.get(CONF_PASSWORD, None)
|
password = conf.get(CONF_PASSWORD, None)
|
||||||
|
|
||||||
|
ssl_verification = conf.get(CONF_VERIFY_SSL)
|
||||||
|
|
||||||
ZM['server_origin'] = server_origin
|
ZM['server_origin'] = server_origin
|
||||||
ZM['url'] = url
|
ZM['url'] = url
|
||||||
ZM['username'] = username
|
ZM['username'] = username
|
||||||
ZM['password'] = password
|
ZM['password'] = password
|
||||||
ZM['path_zms'] = conf.get(CONF_PATH_ZMS)
|
ZM['path_zms'] = conf.get(CONF_PATH_ZMS)
|
||||||
|
ZM['ssl_verification'] = ssl_verification
|
||||||
|
|
||||||
hass.data[DOMAIN] = ZM
|
hass.data[DOMAIN] = ZM
|
||||||
|
|
||||||
@ -77,14 +83,16 @@ def login():
|
|||||||
if ZM['password']:
|
if ZM['password']:
|
||||||
login_post['password'] = ZM['password']
|
login_post['password'] = ZM['password']
|
||||||
|
|
||||||
req = requests.post(ZM['url'] + '/index.php', data=login_post)
|
req = requests.post(ZM['url'] + '/index.php', data=login_post,
|
||||||
|
verify=ZM['ssl_verification'], timeout=DEFAULT_TIMEOUT)
|
||||||
|
|
||||||
ZM['cookies'] = req.cookies
|
ZM['cookies'] = req.cookies
|
||||||
|
|
||||||
# Login calls returns a 200 response 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.
|
# The only way to tell if you logged in correctly is to issue an api call.
|
||||||
req = requests.get(
|
req = requests.get(
|
||||||
ZM['url'] + 'api/host/getVersion.json', cookies=ZM['cookies'],
|
ZM['url'] + 'api/host/getVersion.json', cookies=ZM['cookies'],
|
||||||
timeout=DEFAULT_TIMEOUT)
|
timeout=DEFAULT_TIMEOUT, verify=ZM['ssl_verification'])
|
||||||
|
|
||||||
if not req.ok:
|
if not req.ok:
|
||||||
_LOGGER.error("Connection error logging into ZoneMinder")
|
_LOGGER.error("Connection error logging into ZoneMinder")
|
||||||
@ -100,7 +108,8 @@ def _zm_request(method, api_url, data=None):
|
|||||||
for _ in range(LOGIN_RETRIES):
|
for _ in range(LOGIN_RETRIES):
|
||||||
req = requests.request(
|
req = requests.request(
|
||||||
method, urljoin(ZM['url'], api_url), data=data,
|
method, urljoin(ZM['url'], api_url), data=data,
|
||||||
cookies=ZM['cookies'], timeout=DEFAULT_TIMEOUT)
|
cookies=ZM['cookies'], timeout=DEFAULT_TIMEOUT,
|
||||||
|
verify=ZM['ssl_verification'])
|
||||||
|
|
||||||
if not req.ok:
|
if not req.ok:
|
||||||
login()
|
login()
|
||||||
@ -113,8 +122,9 @@ def _zm_request(method, api_url, data=None):
|
|||||||
try:
|
try:
|
||||||
return req.json()
|
return req.json()
|
||||||
except ValueError:
|
except ValueError:
|
||||||
_LOGGER.exception('JSON decode exception caught while attempting to '
|
_LOGGER.exception(
|
||||||
'decode "%s"', req.text)
|
"JSON decode exception caught while attempting to decode: %s",
|
||||||
|
req.text)
|
||||||
|
|
||||||
|
|
||||||
def get_state(api_url):
|
def get_state(api_url):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user