Update Hikvision Binary Sensors to latest library, remove pyDispatcher (#6231)

* Update pyHik version, remove pyDispatcher in favor of callbacks

* Fix naming

* Fix lint blank line

* Move stream thread start to HOMEASSISTANT_START event

* Bump library version to cleanup shutdown

* Fix requirements
This commit is contained in:
John Mihalic 2017-03-03 09:11:30 -05:00 committed by Pascal Vizeli
parent 568c549353
commit 35fcc299c0
2 changed files with 19 additions and 27 deletions

View File

@ -15,9 +15,10 @@ from homeassistant.components.binary_sensor import (
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_PORT, CONF_NAME, CONF_USERNAME, CONF_PASSWORD, CONF_HOST, CONF_PORT, CONF_NAME, CONF_USERNAME, CONF_PASSWORD,
CONF_SSL, EVENT_HOMEASSISTANT_STOP, ATTR_LAST_TRIP_TIME, CONF_CUSTOMIZE) CONF_SSL, EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_START,
ATTR_LAST_TRIP_TIME, CONF_CUSTOMIZE)
REQUIREMENTS = ['pyhik==0.0.7', 'pydispatcher==2.0.5'] REQUIREMENTS = ['pyhik==0.1.0']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
CONF_IGNORED = 'ignored' CONF_IGNORED = 'ignored'
@ -119,30 +120,32 @@ class HikvisionData(object):
self._password = password self._password = password
# Establish camera # Establish camera
self._cam = HikCamera(self._url, self._port, self.camdata = HikCamera(self._url, self._port,
self._username, self._password) self._username, self._password)
if self._name is None: if self._name is None:
self._name = self._cam.get_name self._name = self.camdata.get_name
# Start event stream
self._cam.start_stream()
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, self.stop_hik) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, self.stop_hik)
hass.bus.listen_once(EVENT_HOMEASSISTANT_START, self.start_hik)
def stop_hik(self, event): def stop_hik(self, event):
"""Shutdown Hikvision subscriptions and subscription thread on exit.""" """Shutdown Hikvision subscriptions and subscription thread on exit."""
self._cam.disconnect() self.camdata.disconnect()
def start_hik(self, event):
"""Start Hikvision event stream thread."""
self.camdata.start_stream()
@property @property
def sensors(self): def sensors(self):
"""Return list of available sensors and their states.""" """Return list of available sensors and their states."""
return self._cam.current_event_states return self.camdata.current_event_states
@property @property
def cam_id(self): def cam_id(self):
"""Return camera id.""" """Return camera id."""
return self._cam.get_id return self.camdata.get_id
@property @property
def name(self): def name(self):
@ -155,8 +158,6 @@ class HikvisionBinarySensor(BinarySensorDevice):
def __init__(self, hass, sensor, cam, delay): def __init__(self, hass, sensor, cam, delay):
"""Initialize the binary_sensor.""" """Initialize the binary_sensor."""
from pydispatch import dispatcher
self._hass = hass self._hass = hass
self._cam = cam self._cam = cam
self._name = self._cam.name + ' ' + sensor self._name = self._cam.name + ' ' + sensor
@ -170,12 +171,8 @@ class HikvisionBinarySensor(BinarySensorDevice):
self._timer = None self._timer = None
# Form signal for dispatcher # Register callback function with pyHik
signal = 'ValueChanged.{}'.format(self._cam.cam_id) self._cam.camdata.add_update_callback(self._update_callback, self._id)
dispatcher.connect(self._update_callback,
signal=signal,
sender=self._sensor)
def _sensor_state(self): def _sensor_state(self):
"""Extract sensor state.""" """Extract sensor state."""
@ -225,13 +222,9 @@ class HikvisionBinarySensor(BinarySensorDevice):
return attr return attr
def _update_callback(self, signal, sender): def _update_callback(self, msg):
"""Update the sensor's state, if needed.""" """Update the sensor's state, if needed."""
_LOGGER.debug('Dispatcher callback, signal: %s, sender: %s', _LOGGER.debug('Callback signal from: %s', msg)
signal, sender)
if sender is not self._sensor:
return
if self._delay > 0 and not self.is_on: if self._delay > 0 and not self.is_on:
# Set timer to wait until updating the state # Set timer to wait until updating the state

View File

@ -473,7 +473,6 @@ pycmus==0.1.0
# pycups==1.9.73 # pycups==1.9.73
# homeassistant.components.zwave # homeassistant.components.zwave
# homeassistant.components.binary_sensor.hikvision
pydispatcher==2.0.5 pydispatcher==2.0.5
# homeassistant.components.sensor.ebox # homeassistant.components.sensor.ebox
@ -498,7 +497,7 @@ pygatt==3.0.0
pyharmony==1.0.12 pyharmony==1.0.12
# homeassistant.components.binary_sensor.hikvision # homeassistant.components.binary_sensor.hikvision
pyhik==0.0.7 pyhik==0.1.0
# homeassistant.components.homematic # homeassistant.components.homematic
pyhomematic==0.1.22 pyhomematic==0.1.22