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 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,14 +38,15 @@ _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')
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'])
new_light = RfxtrxLight(entity_info['name'], None, False)
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)
@ -53,7 +54,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
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,6 +110,7 @@ class RfxtrxLight(Light):
def turn_on(self, **kwargs):
""" Turn the device on. """
if self._event:
self._event.device.send_on(rfxtrx.RFXOBJECT.transport)
self._state = True
@ -115,6 +119,7 @@ class RfxtrxLight(Light):
def turn_off(self, **kwargs):
""" Turn the device off. """
if self._event:
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
self._state = False

View File

@ -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

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
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)

View File

@ -42,10 +42,12 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
# Add switch from config file
switchs = []
devices = config.get('devices')
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'])
new_switch = RfxtrxSwitch(entity_info['name'], None, False)
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)
@ -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,6 +110,7 @@ class RfxtrxSwitch(SwitchDevice):
def turn_on(self, **kwargs):
""" Turn the device on. """
if self._event:
self._event.device.send_on(rfxtrx.RFXOBJECT.transport)
self._state = True
@ -113,6 +118,7 @@ class RfxtrxSwitch(SwitchDevice):
def turn_off(self, **kwargs):
""" Turn the device off. """
if self._event:
self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
self._state = False