refactor rfxtrx code
This commit is contained in:
parent
d6a14a1767
commit
23db6e753f
4 changed files with 93 additions and 78 deletions
|
@ -27,23 +27,21 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
|
||||
# Add switch from config file
|
||||
switchs = []
|
||||
devices = config.get('devices')
|
||||
signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS)
|
||||
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[ATTR_NAME])
|
||||
for device_id, entity_info in config.get('devices', {}).items():
|
||||
if device_id not in rfxtrx.RFX_DEVICES:
|
||||
_LOGGER.info("Add %s rfxtrx.switch", entity_info[ATTR_NAME])
|
||||
|
||||
# Check if i must fire event
|
||||
fire_event = entity_info.get(ATTR_FIREEVENT, False)
|
||||
datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}
|
||||
# Check if i must fire event
|
||||
fire_event = entity_info.get(ATTR_FIREEVENT, False)
|
||||
datas = {ATTR_STATE: False, ATTR_FIREEVENT: fire_event}
|
||||
|
||||
rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
|
||||
newswitch = RfxtrxSwitch(
|
||||
entity_info[ATTR_NAME], rfxobject, datas,
|
||||
signal_repetitions)
|
||||
rfxtrx.RFX_DEVICES[entity_id] = newswitch
|
||||
switchs.append(newswitch)
|
||||
rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
|
||||
newswitch = RfxtrxSwitch(
|
||||
entity_info[ATTR_NAME], rfxobject, datas,
|
||||
signal_repetitions)
|
||||
rfxtrx.RFX_DEVICES[device_id] = newswitch
|
||||
switchs.append(newswitch)
|
||||
|
||||
add_devices_callback(switchs)
|
||||
|
||||
|
@ -54,33 +52,33 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
return
|
||||
|
||||
# Add entity if not exist and the automatic_add is True
|
||||
entity_id = slugify(event.device.id_string.lower())
|
||||
if entity_id not in rfxtrx.RFX_DEVICES:
|
||||
device_id = slugify(event.device.id_string.lower())
|
||||
if device_id not in rfxtrx.RFX_DEVICES:
|
||||
automatic_add = config.get('automatic_add', False)
|
||||
if not automatic_add:
|
||||
return
|
||||
|
||||
_LOGGER.info(
|
||||
"Automatic add %s rfxtrx.switch (Class: %s Sub: %s)",
|
||||
entity_id,
|
||||
device_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)
|
||||
entity_name = "%s : %s" % (device_id, pkt_id)
|
||||
datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
|
||||
signal_repetitions = config.get('signal_repetitions',
|
||||
SIGNAL_REPETITIONS)
|
||||
new_switch = RfxtrxSwitch(entity_name, event, datas,
|
||||
signal_repetitions)
|
||||
rfxtrx.RFX_DEVICES[entity_id] = new_switch
|
||||
rfxtrx.RFX_DEVICES[device_id] = new_switch
|
||||
add_devices_callback([new_switch])
|
||||
|
||||
# Check if entity exists or previously added automatically
|
||||
if entity_id in rfxtrx.RFX_DEVICES:
|
||||
if device_id in rfxtrx.RFX_DEVICES:
|
||||
_LOGGER.debug(
|
||||
"EntityID: %s switch_update. Command: %s",
|
||||
entity_id,
|
||||
device_id,
|
||||
event.values['Command']
|
||||
)
|
||||
if event.values['Command'] == 'On'\
|
||||
|
@ -89,15 +87,15 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
|||
# Update the rfxtrx device state
|
||||
is_on = event.values['Command'] == 'On'
|
||||
# pylint: disable=protected-access
|
||||
rfxtrx.RFX_DEVICES[entity_id]._state = is_on
|
||||
rfxtrx.RFX_DEVICES[entity_id].update_ha_state()
|
||||
rfxtrx.RFX_DEVICES[device_id]._state = is_on
|
||||
rfxtrx.RFX_DEVICES[device_id].update_ha_state()
|
||||
|
||||
# Fire event
|
||||
if rfxtrx.RFX_DEVICES[entity_id].should_fire_event:
|
||||
rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire(
|
||||
if rfxtrx.RFX_DEVICES[device_id].should_fire_event:
|
||||
rfxtrx.RFX_DEVICES[device_id].hass.bus.fire(
|
||||
EVENT_BUTTON_PRESSED, {
|
||||
ATTR_ENTITY_ID:
|
||||
rfxtrx.RFX_DEVICES[entity_id].entity_id,
|
||||
rfxtrx.RFX_DEVICES[device_id].device_id,
|
||||
ATTR_STATE: event.values['Command'].lower()
|
||||
}
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue