Bugfix ZigBee / Move from eventbus to dispatcher (#6333)

* Bugfix ZigBee / Move from eventbus to dispatcher

* fix lint
This commit is contained in:
Pascal Vizeli 2017-03-01 17:57:23 +01:00 committed by Paulus Schoutsen
parent 68713822fd
commit 52b1e13aca
3 changed files with 21 additions and 31 deletions

View file

@ -24,7 +24,8 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
def setup_platform(hass, config, add_devices, discovery_info=None): def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the ZigBee binary sensor platform.""" """Setup the ZigBee binary sensor platform."""
add_devices([ZigBeeBinarySensor(hass, ZigBeeDigitalInConfig(config))]) add_devices(
[ZigBeeBinarySensor(hass, ZigBeeDigitalInConfig(config))], True)
class ZigBeeBinarySensor(ZigBeeDigitalIn, BinarySensorDevice): class ZigBeeBinarySensor(ZigBeeDigitalIn, BinarySensorDevice):

View file

@ -44,7 +44,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
_LOGGER.exception("Unknown ZigBee sensor type: %s", typ) _LOGGER.exception("Unknown ZigBee sensor type: %s", typ)
return return
add_devices([sensor_class(hass, config_class(config))]) add_devices([sensor_class(hass, config_class(config))], True)
class ZigBeeTemperatureSensor(Entity): class ZigBeeTemperatureSensor(Entity):
@ -54,8 +54,6 @@ class ZigBeeTemperatureSensor(Entity):
"""Initialize the sensor.""" """Initialize the sensor."""
self._config = config self._config = config
self._temp = None self._temp = None
# Get initial state
self.schedule_update_ha_state(True)
@property @property
def name(self): def name(self):

View file

@ -4,10 +4,9 @@ Support for ZigBee devices.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/zigbee/ https://home-assistant.io/components/zigbee/
""" """
import asyncio
import logging import logging
import pickle
from binascii import hexlify, unhexlify from binascii import hexlify, unhexlify
from base64 import b64encode, b64decode
import voluptuous as vol import voluptuous as vol
@ -15,6 +14,8 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_STOP, CONF_DEVICE, CONF_NAME, CONF_PIN) EVENT_HOMEASSISTANT_STOP, CONF_DEVICE, CONF_NAME, CONF_PIN)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.dispatcher import (
async_dispatcher_connect, dispatcher_send)
REQUIREMENTS = ['xbee-helper==0.0.7'] REQUIREMENTS = ['xbee-helper==0.0.7']
@ -22,7 +23,7 @@ _LOGGER = logging.getLogger(__name__)
DOMAIN = 'zigbee' DOMAIN = 'zigbee'
EVENT_ZIGBEE_FRAME_RECEIVED = 'zigbee_frame_received' SIGNAL_ZIGBEE_FRAME_RECEIVED = 'zigbee_frame_received'
CONF_ADDRESS = 'address' CONF_ADDRESS = 'address'
CONF_BAUD = 'baud' CONF_BAUD = 'baud'
@ -102,9 +103,7 @@ def setup(hass, config):
Pickles the frame, then encodes it into base64 since it contains Pickles the frame, then encodes it into base64 since it contains
non JSON serializable binary. non JSON serializable binary.
""" """
hass.bus.fire( dispatcher_send(hass, SIGNAL_ZIGBEE_FRAME_RECEIVED, frame)
EVENT_ZIGBEE_FRAME_RECEIVED,
{ATTR_FRAME: b64encode(pickle.dumps(frame)).decode("ascii")})
DEVICE.add_frame_rx_handler(_frame_received) DEVICE.add_frame_rx_handler(_frame_received)
@ -125,16 +124,6 @@ def frame_is_relevant(entity, frame):
return True return True
def subscribe(hass, callback):
"""Subscribe to incoming ZigBee frames."""
def zigbee_frame_subscriber(event):
"""Decode and unpickle the frame from the event bus, and call back."""
frame = pickle.loads(b64decode(event.data[ATTR_FRAME]))
callback(frame)
hass.bus.listen(EVENT_ZIGBEE_FRAME_RECEIVED, zigbee_frame_subscriber)
class ZigBeeConfig(object): class ZigBeeConfig(object):
"""Handle the fetching of configuration from the config file.""" """Handle the fetching of configuration from the config file."""
@ -288,6 +277,9 @@ class ZigBeeDigitalIn(Entity):
self._config = config self._config = config
self._state = False self._state = False
@asyncio.coroutine
def async_added_to_hass(self):
"""Register callbacks."""
def handle_frame(frame): def handle_frame(frame):
"""Handle an incoming frame. """Handle an incoming frame.
@ -302,12 +294,10 @@ class ZigBeeDigitalIn(Entity):
# Doesn't contain information about our pin # Doesn't contain information about our pin
return return
self._state = self._config.state2bool[sample[pin_name]] self._state = self._config.state2bool[sample[pin_name]]
self.update_ha_state() self.schedule_update_ha_state()
subscribe(hass, handle_frame) async_dispatcher_connect(
self.hass, SIGNAL_ZIGBEE_FRAME_RECEIVED, handle_frame)
# Get initial state
self.schedule_update_ha_state(True)
@property @property
def name(self): def name(self):
@ -373,7 +363,7 @@ class ZigBeeDigitalOut(ZigBeeDigitalIn):
return return
self._state = state self._state = state
if not self.should_poll: if not self.should_poll:
self.update_ha_state() self.schedule_update_ha_state()
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Set the digital output to its 'on' state.""" """Set the digital output to its 'on' state."""
@ -410,6 +400,9 @@ class ZigBeeAnalogIn(Entity):
self._config = config self._config = config
self._value = None self._value = None
@asyncio.coroutine
def async_added_to_hass(self):
"""Register callbacks."""
def handle_frame(frame): def handle_frame(frame):
"""Handle an incoming frame. """Handle an incoming frame.
@ -428,12 +421,10 @@ class ZigBeeAnalogIn(Entity):
ADC_PERCENTAGE, ADC_PERCENTAGE,
self._config.max_voltage self._config.max_voltage
) )
self.update_ha_state() self.schedule_update_ha_state()
subscribe(hass, handle_frame) async_dispatcher_connect(
self.hass, SIGNAL_ZIGBEE_FRAME_RECEIVED, handle_frame)
# Get initial state
hass.add_job(self.async_update_ha_state, True)
@property @property
def name(self): def name(self):