Created
March 25, 2020 13:08
-
-
Save wallacemreis/1b8e3320bc929faf94703cf5d16bd51c to your computer and use it in GitHub Desktop.
Zoneminder HA PTZ
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Support for ZoneMinder.""" | |
import logging | |
import requests | |
import voluptuous as vol | |
from zoneminder.zm import ZoneMinder | |
from homeassistant.const import ( | |
ATTR_ID, | |
ATTR_NAME, | |
CONF_HOST, | |
CONF_PASSWORD, | |
CONF_PATH, | |
CONF_SSL, | |
CONF_USERNAME, | |
CONF_VERIFY_SSL, | |
) | |
import homeassistant.helpers.config_validation as cv | |
from homeassistant.helpers.discovery import async_load_platform | |
_LOGGER = logging.getLogger(__name__) | |
CONF_PATH_ZMS = "path_zms" | |
DEFAULT_PATH = "/zm/" | |
DEFAULT_PATH_ZMS = "/zm/cgi-bin/nph-zms" | |
DEFAULT_SSL = False | |
DEFAULT_TIMEOUT = 10 | |
DEFAULT_VERIFY_SSL = True | |
DOMAIN = "zoneminder" | |
HOST_CONFIG_SCHEMA = vol.Schema( | |
{ | |
vol.Required(CONF_HOST): cv.string, | |
vol.Optional(CONF_PASSWORD): cv.string, | |
vol.Optional(CONF_PATH, default=DEFAULT_PATH): 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_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean, | |
} | |
) | |
CONFIG_SCHEMA = vol.Schema( | |
{DOMAIN: vol.All(cv.ensure_list, [HOST_CONFIG_SCHEMA])}, extra=vol.ALLOW_EXTRA | |
) | |
SERVICE_SET_RUN_STATE = "set_run_state" | |
SET_RUN_STATE_SCHEMA = vol.Schema( | |
{vol.Required(ATTR_ID): cv.string, vol.Required(ATTR_NAME): cv.string} | |
) | |
ATTR_CAMERA_ID = 1 | |
ATTR_MOVE_DIRECTION = "moveConRight" | |
SERVICE_PTZ_MOVE_CAMERA = "ptz_move_camera" | |
PTZ_MOVE_CAMERA_SCHEMA = vol.Schema( | |
{vol.Required(ATTR_CAMERA_ID): cv.string, vol.Required(ATTR_MOVE_DIRECTION): cv.string} | |
) | |
def setup(hass, config): | |
"""Set up the ZoneMinder component.""" | |
hass.data[DOMAIN] = {} | |
success = True | |
for conf in config[DOMAIN]: | |
if conf[CONF_SSL]: | |
schema = "https" | |
else: | |
schema = "http" | |
host_name = conf[CONF_HOST] | |
server_origin = f"{schema}://{host_name}" | |
zm_client = 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), | |
) | |
hass.data[DOMAIN][host_name] = zm_client | |
success = zm_client.login() and success | |
def set_active_state(call): | |
"""Set the ZoneMinder run state to the given state name.""" | |
zm_id = call.data[ATTR_ID] | |
state_name = call.data[ATTR_NAME] | |
if zm_id not in hass.data[DOMAIN]: | |
_LOGGER.error("Invalid ZoneMinder host provided: %s", zm_id) | |
if not hass.data[DOMAIN][zm_id].set_active_state(state_name): | |
_LOGGER.error( | |
"Unable to change ZoneMinder state. Host: %s, state: %s", zm_id, state_name, | |
) | |
hass.services.register( | |
DOMAIN, SERVICE_SET_RUN_STATE, set_active_state, schema=SET_RUN_STATE_SCHEMA | |
) | |
def ptz_move_camera(call): | |
camera_id = call.data["camera_id"] | |
move_direction = call.data["move_direction"] | |
params = { | |
"view": "request", | |
"request": "control", | |
"id": camera_id, | |
"control": move_direction, | |
"xge": 43, | |
"token": zm_client._auth_token, | |
} | |
requests.request( | |
method="POST", url="{}{}".format(zm_client._server_url, "index.php"), params=params, | |
) | |
hass.services.register(DOMAIN, SERVICE_PTZ_MOVE_CAMERA, ptz_move_camera) | |
hass.async_create_task(async_load_platform(hass, "binary_sensor", DOMAIN, {}, config)) | |
return success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set_run_state: | |
description: Set the ZoneMinder run state | |
fields: | |
name: | |
description: The string name of the ZoneMinder run state to set as active. | |
example: 'Home' | |
ptz_move_camera: | |
description: Move the ZoneMinder Camera | |
fields: | |
camera_id: | |
description: The string id of the ZoneMinder camera. | |
example: '1' | |
move_direction: | |
description: The string name of the ZoneMinder direction. | |
example: 'moveConRight' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment