From db509ccf181a2488815ace2c663811dff938d12c Mon Sep 17 00:00:00 2001 From: badele Date: Fri, 2 Oct 2015 22:39:30 +0200 Subject: [PATCH] Add a light & switch rfxtrx sender capability --- homeassistant/components/light/rfxtrx.py | 31 +++++++++------- homeassistant/components/rfxtrx.py | 45 ++++++++++++++++++++++- homeassistant/components/sensor/rfxtrx.py | 2 +- homeassistant/components/switch/rfxtrx.py | 24 +++++++----- 4 files changed, 78 insertions(+), 24 deletions(-) diff --git a/homeassistant/components/light/rfxtrx.py b/homeassistant/components/light/rfxtrx.py index 190aae55da6..d4008682c4c 100644 --- a/homeassistant/components/light/rfxtrx.py +++ b/homeassistant/components/light/rfxtrx.py @@ -24,7 +24,7 @@ light: """ import logging import homeassistant.components.rfxtrx as rfxtrx -from RFXtrx import LightingDevice +import RFXtrx as rfxtrxmod from homeassistant.components.light import Light from homeassistant.util import slugify @@ -38,22 +38,23 @@ _LOGGER = logging.getLogger(__name__) def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Setup the RFXtrx platform. """ - # Add light from config file lights = [] - devices = config.get('devices') - for entity_id, entity_info in devices.items(): - if entity_id not in rfxtrx.RFX_DEVICES: - _LOGGER.info("Add %s rfxtrx.light", entity_info['name']) - new_light = RfxtrxLight(entity_info['name'], None, False) - rfxtrx.RFX_DEVICES[entity_id] = new_light - lights.append(new_light) + devices = config.get('devices', None) + if devices: + for entity_id, entity_info in devices.items(): + if entity_id not in rfxtrx.RFX_DEVICES: + _LOGGER.info("Add %s rfxtrx.light", entity_info['name']) + rfxobject = rfxtrx.getRFXObject(entity_info['packetid']) + new_light = RfxtrxLight(entity_info['name'], rfxobject, False) + rfxtrx.RFX_DEVICES[entity_id] = new_light + lights.append(new_light) add_devices_callback(lights) def light_update(event): """ Callback for sensor updates from the RFXtrx gateway. """ - if isinstance(event.device, LightingDevice): + if isinstance(event.device, rfxtrxmod.LightingDevice): entity_id = slugify(event.device.id_string.lower()) # Add entity if not exist and the automatic_add is True @@ -65,7 +66,9 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): event.device.__class__.__name__, event.device.subtype ) - new_light = RfxtrxLight(entity_id, event, False) + packet_id = "".join("{0:02x}".format(x) for x in event.data) + entity_name = "%(entity_id)s : %(packet_id)s" % locals() + new_light = RfxtrxLight(entity_name, event, False) rfxtrx.RFX_DEVICES[entity_id] = new_light add_devices_callback([new_light]) @@ -107,7 +110,8 @@ class RfxtrxLight(Light): def turn_on(self, **kwargs): """ Turn the device on. """ - self._event.device.send_on(rfxtrx.RFXOBJECT.transport) + if self._event: + self._event.device.send_on(rfxtrx.RFXOBJECT.transport) self._state = True self.update_ha_state() @@ -115,7 +119,8 @@ class RfxtrxLight(Light): def turn_off(self, **kwargs): """ Turn the device off. """ - self._event.device.send_off(rfxtrx.RFXOBJECT.transport) + if self._event: + self._event.device.send_off(rfxtrx.RFXOBJECT.transport) self._state = False self.update_ha_state() diff --git a/homeassistant/components/rfxtrx.py b/homeassistant/components/rfxtrx.py index afd74c907d1..d4eb379b04f 100644 --- a/homeassistant/components/rfxtrx.py +++ b/homeassistant/components/rfxtrx.py @@ -4,6 +4,24 @@ homeassistant.components.rfxtrx Connects Home Assistant to a RFXtrx device. """ +""" +homeassistant.components.rfxtrx +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Connects Home Assistant to a RFXtrx device. + +Configuration: + +To use Rfxtrx device you will need to add the following to your +configuration.yaml file. + +rfxtrx: + device: /dev/serial/by-id/usb-RFXCOM_RFXtrx433_A1YVC1P0-if00-port0 + +*Optional* + + debug: True + +""" import logging DEPENDENCIES = [] @@ -12,6 +30,7 @@ REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/' + DOMAIN = "rfxtrx" CONF_DEVICE = 'device' +CONF_DEBUG = 'debug' RECEIVED_EVT_SUBSCRIBERS = [] RFX_DEVICES = {} _LOGGER = logging.getLogger(__name__) @@ -23,6 +42,7 @@ def setup(hass, config): # Declare the Handle event def handle_receive(event): """ Callback all subscribers for RFXtrx gateway. """ + for subscriber in RECEIVED_EVT_SUBSCRIBERS: subscriber(event) @@ -35,7 +55,30 @@ def setup(hass, config): # Init the rfxtrx module global RFXOBJECT + device = config[DOMAIN][CONF_DEVICE] - RFXOBJECT = rfxtrxmod.Core(device, handle_receive) + try: + debug = config[DOMAIN][CONF_DEBUG] + except KeyError: + debug = False + + RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug) return True + +def getRFXObject(packetid): + """ return the RFXObject with the packetid""" + binarypacket = bytearray.fromhex(packetid) + + pkt = rfxtrxmod.lowlevel.parse(binarypacket) + if pkt is not None: + if isinstance(pkt, rfxtrxmod.lowlevel.SensorPacket): + obj = rfxtrxmod.SensorEvent(pkt) + elif isinstance(pkt, rfxtrxmod.lowlevel.Status): + obj = rfxtrxmod.StatusEvent(pkt) + else: + obj = rfxtrxmod.ControlEvent(pkt) + + return obj + + return None diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py index 625fd50de84..d0514c93a51 100644 --- a/homeassistant/components/sensor/rfxtrx.py +++ b/homeassistant/components/sensor/rfxtrx.py @@ -50,7 +50,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): # Add entity if not exist and the automatic_add is True if entity_id not in rfxtrx.RFX_DEVICES: - automatic_add = config.get('automatic_add', False) + automatic_add = config.get('automatic_add', True) if automatic_add: _LOGGER.info("Automatic add %s rfxtrx.light", entity_id) new_sensor = RfxtrxSensor(event) diff --git a/homeassistant/components/switch/rfxtrx.py b/homeassistant/components/switch/rfxtrx.py index cdc7f64f360..d13e298f1b7 100644 --- a/homeassistant/components/switch/rfxtrx.py +++ b/homeassistant/components/switch/rfxtrx.py @@ -42,12 +42,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): # Add switch from config file switchs = [] devices = config.get('devices') - for entity_id, entity_info in devices.items(): - if entity_id not in rfxtrx.RFX_DEVICES: - _LOGGER.info("Add %s rfxtrx.switch", entity_info['name']) - new_switch = RfxtrxSwitch(entity_info['name'], None, False) - rfxtrx.RFX_DEVICES[entity_id] = new_switch - switchs.append(new_switch) + if devices: + for entity_id, entity_info in devices.items(): + if entity_id not in rfxtrx.RFX_DEVICES: + _LOGGER.info("Add %s rfxtrx.switch", entity_info['name']) + rfxobject = rfxtrx.getRFXObject(entity_info['packetid']) + new_switch = RfxtrxSwitch(entity_info['name'], rfxobject, False) + rfxtrx.RFX_DEVICES[entity_id] = new_switch + switchs.append(new_switch) add_devices_callback(switchs) @@ -65,7 +67,9 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): event.device.__class__.__name__, event.device.subtype ) - new_switch = RfxtrxSwitch(entity_id, event, False) + packet_id = "".join("{0:02x}".format(x) for x in event.data) + entity_name = "%(entity_id)s : %(packet_id)s" % locals() + new_switch = RfxtrxSwitch(entity_name, event, False) rfxtrx.RFX_DEVICES[entity_id] = new_switch add_devices_callback([new_switch]) @@ -106,14 +110,16 @@ class RfxtrxSwitch(SwitchDevice): def turn_on(self, **kwargs): """ Turn the device on. """ - self._event.device.send_on(rfxtrx.RFXOBJECT.transport) + if self._event: + self._event.device.send_on(rfxtrx.RFXOBJECT.transport) self._state = True self.update_ha_state() def turn_off(self, **kwargs): """ Turn the device off. """ - self._event.device.send_off(rfxtrx.RFXOBJECT.transport) + if self._event: + self._event.device.send_off(rfxtrx.RFXOBJECT.transport) self._state = False self.update_ha_state()