Add a light & switch rfxtrx sender capability

pull/483/head
badele 2015-10-02 22:39:30 +02:00
parent cc47e39006
commit db509ccf18
4 changed files with 78 additions and 24 deletions

View File

@ -24,7 +24,7 @@ light:
""" """
import logging import logging
import homeassistant.components.rfxtrx as rfxtrx import homeassistant.components.rfxtrx as rfxtrx
from RFXtrx import LightingDevice import RFXtrx as rfxtrxmod
from homeassistant.components.light import Light from homeassistant.components.light import Light
from homeassistant.util import slugify from homeassistant.util import slugify
@ -38,22 +38,23 @@ _LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_devices_callback, discovery_info=None): def setup_platform(hass, config, add_devices_callback, discovery_info=None):
""" Setup the RFXtrx platform. """ """ Setup the RFXtrx platform. """
# Add light from config file # Add light from config file
lights = [] lights = []
devices = config.get('devices') devices = config.get('devices', None)
for entity_id, entity_info in devices.items(): if devices:
if entity_id not in rfxtrx.RFX_DEVICES: for entity_id, entity_info in devices.items():
_LOGGER.info("Add %s rfxtrx.light", entity_info['name']) if entity_id not in rfxtrx.RFX_DEVICES:
new_light = RfxtrxLight(entity_info['name'], None, False) _LOGGER.info("Add %s rfxtrx.light", entity_info['name'])
rfxtrx.RFX_DEVICES[entity_id] = new_light rfxobject = rfxtrx.getRFXObject(entity_info['packetid'])
lights.append(new_light) new_light = RfxtrxLight(entity_info['name'], rfxobject, False)
rfxtrx.RFX_DEVICES[entity_id] = new_light
lights.append(new_light)
add_devices_callback(lights) add_devices_callback(lights)
def light_update(event): def light_update(event):
""" Callback for sensor updates from the RFXtrx gateway. """ """ 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()) entity_id = slugify(event.device.id_string.lower())
# Add entity if not exist and the automatic_add is True # 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.__class__.__name__,
event.device.subtype 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 rfxtrx.RFX_DEVICES[entity_id] = new_light
add_devices_callback([new_light]) add_devices_callback([new_light])
@ -107,7 +110,8 @@ class RfxtrxLight(Light):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
""" Turn the device on. """ """ 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._state = True
self.update_ha_state() self.update_ha_state()
@ -115,7 +119,8 @@ class RfxtrxLight(Light):
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
""" Turn the device off. """ """ 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._state = False
self.update_ha_state() self.update_ha_state()

View File

@ -4,6 +4,24 @@ homeassistant.components.rfxtrx
Connects Home Assistant to a RFXtrx device. 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 import logging
DEPENDENCIES = [] DEPENDENCIES = []
@ -12,6 +30,7 @@ REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/' +
DOMAIN = "rfxtrx" DOMAIN = "rfxtrx"
CONF_DEVICE = 'device' CONF_DEVICE = 'device'
CONF_DEBUG = 'debug'
RECEIVED_EVT_SUBSCRIBERS = [] RECEIVED_EVT_SUBSCRIBERS = []
RFX_DEVICES = {} RFX_DEVICES = {}
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -23,6 +42,7 @@ def setup(hass, config):
# Declare the Handle event # Declare the Handle event
def handle_receive(event): def handle_receive(event):
""" Callback all subscribers for RFXtrx gateway. """ """ Callback all subscribers for RFXtrx gateway. """
for subscriber in RECEIVED_EVT_SUBSCRIBERS: for subscriber in RECEIVED_EVT_SUBSCRIBERS:
subscriber(event) subscriber(event)
@ -35,7 +55,30 @@ def setup(hass, config):
# Init the rfxtrx module # Init the rfxtrx module
global RFXOBJECT global RFXOBJECT
device = config[DOMAIN][CONF_DEVICE] 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 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

View File

@ -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 # Add entity if not exist and the automatic_add is True
if entity_id not in rfxtrx.RFX_DEVICES: 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: if automatic_add:
_LOGGER.info("Automatic add %s rfxtrx.light", entity_id) _LOGGER.info("Automatic add %s rfxtrx.light", entity_id)
new_sensor = RfxtrxSensor(event) new_sensor = RfxtrxSensor(event)

View File

@ -42,12 +42,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
# Add switch from config file # Add switch from config file
switchs = [] switchs = []
devices = config.get('devices') devices = config.get('devices')
for entity_id, entity_info in devices.items(): if devices:
if entity_id not in rfxtrx.RFX_DEVICES: for entity_id, entity_info in devices.items():
_LOGGER.info("Add %s rfxtrx.switch", entity_info['name']) if entity_id not in rfxtrx.RFX_DEVICES:
new_switch = RfxtrxSwitch(entity_info['name'], None, False) _LOGGER.info("Add %s rfxtrx.switch", entity_info['name'])
rfxtrx.RFX_DEVICES[entity_id] = new_switch rfxobject = rfxtrx.getRFXObject(entity_info['packetid'])
switchs.append(new_switch) new_switch = RfxtrxSwitch(entity_info['name'], rfxobject, False)
rfxtrx.RFX_DEVICES[entity_id] = new_switch
switchs.append(new_switch)
add_devices_callback(switchs) 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.__class__.__name__,
event.device.subtype 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 rfxtrx.RFX_DEVICES[entity_id] = new_switch
add_devices_callback([new_switch]) add_devices_callback([new_switch])
@ -106,14 +110,16 @@ class RfxtrxSwitch(SwitchDevice):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
""" Turn the device on. """ """ 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._state = True
self.update_ha_state() self.update_ha_state()
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
""" Turn the device off. """ """ 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._state = False
self.update_ha_state() self.update_ha_state()