Refactoring the rfxtrx components

This commit is contained in:
badele 2015-10-07 19:57:40 +02:00
parent 46f5ef54a1
commit a5dae78155
3 changed files with 42 additions and 35 deletions

View File

@ -31,7 +31,6 @@ from homeassistant.util import slugify
DEPENDENCIES = ['rfxtrx'] DEPENDENCIES = ['rfxtrx']
DOMAIN = "rfxtrx"
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -60,20 +59,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
entity_id = slugify(event.device.id_string.lower()) entity_id = slugify(event.device.id_string.lower())
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', False)
if automatic_add: if not automatic_add:
_LOGGER.info( return
"Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
entity_id,
event.device.__class__.__name__,
event.device.subtype
)
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, pkt_id)
new_light = RfxtrxLight(entity_name, event, False)
rfxtrx.RFX_DEVICES[entity_id] = new_light
add_devices_callback([new_light])
# Check if entity exists (previous automatic added) _LOGGER.info(
"Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
entity_id,
event.device.__class__.__name__,
event.device.subtype
)
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, pkt_id)
new_light = RfxtrxLight(entity_name, event, False)
rfxtrx.RFX_DEVICES[entity_id] = new_light
add_devices_callback([new_light])
# Check if entity exists or previously added automatically
if entity_id in rfxtrx.RFX_DEVICES: if entity_id in rfxtrx.RFX_DEVICES:
if event.values['Command'] == 'On'\ if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off': or event.values['Command'] == 'Off':
@ -112,7 +113,7 @@ class RfxtrxLight(Light):
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
""" Turn the device on. """ """ Turn the device on. """
if self._event: if hasattr(self, '_event') and self._event:
self._event.device.send_on(rfxtrx.RFXOBJECT.transport) self._event.device.send_on(rfxtrx.RFXOBJECT.transport)
self._state = True self._state = True
@ -121,7 +122,7 @@ class RfxtrxLight(Light):
def turn_off(self, **kwargs): def turn_off(self, **kwargs):
""" Turn the device off. """ """ Turn the device off. """
if self._event: if hasattr(self, '_event') and self._event:
self._event.device.send_off(rfxtrx.RFXOBJECT.transport) self._event.device.send_off(rfxtrx.RFXOBJECT.transport)
self._state = False self._state = False

View File

@ -20,7 +20,8 @@ import logging
from homeassistant.util import slugify from homeassistant.util import slugify
DEPENDENCIES = [] DEPENDENCIES = []
REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/0.2.zip#RFXtrx==0.2'] REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/0.2.zip' +
'#RFXtrx==0.2']
DOMAIN = "rfxtrx" DOMAIN = "rfxtrx"
CONF_DEVICE = 'device' CONF_DEVICE = 'device'
@ -59,11 +60,15 @@ def setup(hass, config):
# Init the rfxtrx module # Init the rfxtrx module
global RFXOBJECT global RFXOBJECT
if CONF_DEVICE not in config[DOMAIN]:
_LOGGER.exception(
"can found device parameter in %s YAML configuration section",
DOMAIN
)
return False
device = config[DOMAIN][CONF_DEVICE] device = config[DOMAIN][CONF_DEVICE]
try: debug = config[DOMAIN].get(CONF_DEBUG, False)
debug = config[DOMAIN][CONF_DEBUG]
except KeyError:
debug = False
RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug) RFXOBJECT = rfxtrxmod.Core(device, handle_receive, debug=debug)

View File

@ -31,7 +31,6 @@ from homeassistant.util import slugify
DEPENDENCIES = ['rfxtrx'] DEPENDENCIES = ['rfxtrx']
DOMAIN = "rfxtrx"
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -61,20 +60,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
entity_id = slugify(event.device.id_string.lower()) entity_id = slugify(event.device.id_string.lower())
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', False)
if automatic_add: if not automatic_add:
_LOGGER.info( return
"Automatic add %s rfxtrx.switch (Class: %s Sub: %s)",
entity_id,
event.device.__class__.__name__,
event.device.subtype
)
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, pkt_id)
new_switch = RfxtrxSwitch(entity_name, event, False)
rfxtrx.RFX_DEVICES[entity_id] = new_switch
add_devices_callback([new_switch])
# Check if entity exists (previous automatic added) _LOGGER.info(
"Automatic add %s rfxtrx.switch (Class: %s Sub: %s)",
entity_id,
event.device.__class__.__name__,
event.device.subtype
)
pkt_id = "".join("{0:02x}".format(x) for x in event.data)
entity_name = "%s : %s" % (entity_id, pkt_id)
new_switch = RfxtrxSwitch(entity_name, event, False)
rfxtrx.RFX_DEVICES[entity_id] = new_switch
add_devices_callback([new_switch])
# Check if entity exists or previously added automatically
if entity_id in rfxtrx.RFX_DEVICES: if entity_id in rfxtrx.RFX_DEVICES:
if event.values['Command'] == 'On'\ if event.values['Command'] == 'On'\
or event.values['Command'] == 'Off': or event.values['Command'] == 'Off':