core/homeassistant/components/switch/hikvisioncam.py

91 lines
2.7 KiB
Python
Raw Normal View History

"""
Support turning on/off motion detection on Hikvision cameras.
2015-10-20 20:19:04 +00:00
For more details about this platform, please refer to the documentation at
2015-11-09 12:12:18 +00:00
https://home-assistant.io/components/switch.hikvision/
"""
import logging
2016-02-19 05:27:50 +00:00
from homeassistant.const import (
CONF_HOST, CONF_PASSWORD, CONF_USERNAME, STATE_OFF, STATE_ON)
2015-11-29 21:49:05 +00:00
from homeassistant.helpers.entity import ToggleEntity
_LOGGING = logging.getLogger(__name__)
REQUIREMENTS = ['hikvision==0.4']
# pylint: disable=too-many-arguments
# pylint: disable=too-many-instance-attributes
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-03-08 12:35:39 +00:00
"""Setup Hikvision camera."""
2015-11-17 08:18:42 +00:00
import hikvision.api
from hikvision.error import HikvisionError, MissingParamError
host = config.get(CONF_HOST, None)
port = config.get('port', "80")
name = config.get('name', "Hikvision Camera Motion Detection")
username = config.get(CONF_USERNAME, "admin")
password = config.get(CONF_PASSWORD, "12345")
try:
hikvision_cam = hikvision.api.CreateDevice(
2015-06-02 16:14:12 +00:00
host, port=port, username=username,
password=password, is_https=False)
except MissingParamError as param_err:
_LOGGING.error("Missing required param: %s", param_err)
return False
except HikvisionError as conn_err:
_LOGGING.error("Unable to connect: %s", conn_err)
return False
add_devices_callback([
HikvisionMotionSwitch(name, hikvision_cam)
])
class HikvisionMotionSwitch(ToggleEntity):
2016-03-08 12:35:39 +00:00
"""Representation of a switch to toggle on/off motion detection."""
def __init__(self, name, hikvision_cam):
2016-03-08 12:35:39 +00:00
"""Initialize the switch."""
self._name = name
self._hikvision_cam = hikvision_cam
self._state = STATE_OFF
@property
def should_poll(self):
2016-03-08 12:35:39 +00:00
"""Poll for status regularly."""
return True
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the device if any."""
return self._name
@property
def state(self):
2016-03-08 12:35:39 +00:00
"""Return the state of the device if any."""
return self._state
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
return self._state == STATE_ON
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device on."""
_LOGGING.info("Turning on Motion Detection ")
self._hikvision_cam.enable_motion_detection()
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the device off."""
_LOGGING.info("Turning off Motion Detection ")
self._hikvision_cam.disable_motion_detection()
def update(self):
2016-03-08 12:35:39 +00:00
"""Update Motion Detection state."""
2015-06-02 16:17:36 +00:00
enabled = self._hikvision_cam.is_motion_detection_enabled()
_LOGGING.info('enabled: %s', enabled)
self._state = STATE_ON if enabled else STATE_OFF