Refactoring test instance type

This commit is contained in:
badele 2015-10-07 19:15:50 +02:00
parent 496e4cf784
commit 46f5ef54a1
2 changed files with 53 additions and 49 deletions

View File

@ -53,34 +53,36 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
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, rfxtrxmod.LightingDevice): if not isinstance(event.device, rfxtrxmod.LightingDevice):
entity_id = slugify(event.device.id_string.lower()) return
# 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: entity_id = slugify(event.device.id_string.lower())
automatic_add = config.get('automatic_add', False) if entity_id not in rfxtrx.RFX_DEVICES:
if automatic_add: automatic_add = config.get('automatic_add', False)
_LOGGER.info( if automatic_add:
"Automatic add %s rfxtrx.light (Class: %s Sub: %s)", _LOGGER.info(
entity_id, "Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
event.device.__class__.__name__, entity_id,
event.device.subtype 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) pkt_id = "".join("{0:02x}".format(x) for x in event.data)
new_light = RfxtrxLight(entity_name, event, False) entity_name = "%s : %s" % (entity_id, pkt_id)
rfxtrx.RFX_DEVICES[entity_id] = new_light new_light = RfxtrxLight(entity_name, event, False)
add_devices_callback([new_light]) rfxtrx.RFX_DEVICES[entity_id] = new_light
add_devices_callback([new_light])
# Check if entity exists (previous automatic added) # Check if entity exists (previous automatic added)
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':
if event.values['Command'] == 'On': if event.values['Command'] == 'On':
rfxtrx.RFX_DEVICES[entity_id].turn_on() rfxtrx.RFX_DEVICES[entity_id].turn_on()
else: else:
rfxtrx.RFX_DEVICES[entity_id].turn_off() rfxtrx.RFX_DEVICES[entity_id].turn_off()
# Subscribe to main rfxtrx events
if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS: if light_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(light_update) rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(light_update)

View File

@ -55,33 +55,35 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
def switch_update(event): def switch_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, LightingDevice):
entity_id = slugify(event.device.id_string.lower()) return
# 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: entity_id = slugify(event.device.id_string.lower())
automatic_add = config.get('automatic_add', False) if entity_id not in rfxtrx.RFX_DEVICES:
if automatic_add: automatic_add = config.get('automatic_add', False)
_LOGGER.info( if automatic_add:
"Automatic add %s rfxtrx.switch (Class: %s Sub: %s)", _LOGGER.info(
entity_id, "Automatic add %s rfxtrx.switch (Class: %s Sub: %s)",
event.device.__class__.__name__, entity_id,
event.device.subtype 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) pkt_id = "".join("{0:02x}".format(x) for x in event.data)
new_switch = RfxtrxSwitch(entity_name, event, False) entity_name = "%s : %s" % (entity_id, pkt_id)
rfxtrx.RFX_DEVICES[entity_id] = new_switch new_switch = RfxtrxSwitch(entity_name, event, False)
add_devices_callback([new_switch]) rfxtrx.RFX_DEVICES[entity_id] = new_switch
add_devices_callback([new_switch])
# Check if entity exists (previous automatic added) # Check if entity exists (previous automatic added)
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':
if event.values['Command'] == 'On': if event.values['Command'] == 'On':
rfxtrx.RFX_DEVICES[entity_id].turn_on() rfxtrx.RFX_DEVICES[entity_id].turn_on()
else: else:
rfxtrx.RFX_DEVICES[entity_id].turn_off() rfxtrx.RFX_DEVICES[entity_id].turn_off()
# Subscribe to main rfxtrx events
if switch_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS: if switch_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(switch_update) rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(switch_update)