refactor rfxtrx code

pull/1385/head
Daniel 2016-02-23 18:01:53 +01:00
parent d6a14a1767
commit 23db6e753f
4 changed files with 93 additions and 78 deletions

View File

@ -26,12 +26,10 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
import RFXtrx as rfxtrxmod import RFXtrx as rfxtrxmod
lights = [] lights = []
devices = config.get('devices', None)
signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS) signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS)
if devices: for device_id, entity_info in config.get('devices', {}).items():
for entity_id, entity_info in devices.items(): if device_id not in rfxtrx.RFX_DEVICES:
if entity_id not in rfxtrx.RFX_DEVICES:
_LOGGER.info("Add %s rfxtrx.light", entity_info[ATTR_NAME]) _LOGGER.info("Add %s rfxtrx.light", entity_info[ATTR_NAME])
# Check if i must fire event # Check if i must fire event
@ -42,7 +40,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
new_light = RfxtrxLight( new_light = RfxtrxLight(
entity_info[ATTR_NAME], rfxobject, datas, entity_info[ATTR_NAME], rfxobject, datas,
signal_repetitions) signal_repetitions)
rfxtrx.RFX_DEVICES[entity_id] = new_light rfxtrx.RFX_DEVICES[device_id] = new_light
lights.append(new_light) lights.append(new_light)
add_devices_callback(lights) add_devices_callback(lights)
@ -54,33 +52,33 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
return return
# Add entity if not exist and the automatic_add is True # Add entity if not exist and the automatic_add is True
entity_id = slugify(event.device.id_string.lower()) device_id = slugify(event.device.id_string.lower())
if entity_id not in rfxtrx.RFX_DEVICES: if device_id not in rfxtrx.RFX_DEVICES:
automatic_add = config.get('automatic_add', False) automatic_add = config.get('automatic_add', False)
if not automatic_add: if not automatic_add:
return return
_LOGGER.info( _LOGGER.info(
"Automatic add %s rfxtrx.light (Class: %s Sub: %s)", "Automatic add %s rfxtrx.light (Class: %s Sub: %s)",
entity_id, device_id,
event.device.__class__.__name__, event.device.__class__.__name__,
event.device.subtype event.device.subtype
) )
pkt_id = "".join("{0:02x}".format(x) for x in event.data) 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} datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
signal_repetitions = config.get('signal_repetitions', signal_repetitions = config.get('signal_repetitions',
SIGNAL_REPETITIONS) SIGNAL_REPETITIONS)
new_light = RfxtrxLight(entity_name, event, datas, new_light = RfxtrxLight(entity_name, event, datas,
signal_repetitions) signal_repetitions)
rfxtrx.RFX_DEVICES[entity_id] = new_light rfxtrx.RFX_DEVICES[device_id] = new_light
add_devices_callback([new_light]) add_devices_callback([new_light])
# Check if entity exists or previously added automatically # Check if entity exists or previously added automatically
if entity_id in rfxtrx.RFX_DEVICES: if device_id in rfxtrx.RFX_DEVICES:
_LOGGER.debug( _LOGGER.debug(
"EntityID: %s light_update. Command: %s", "EntityID: %s light_update. Command: %s",
entity_id, device_id,
event.values['Command'] event.values['Command']
) )
@ -90,27 +88,27 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
# Update the rfxtrx device state # Update the rfxtrx device state
is_on = event.values['Command'] == 'On' is_on = event.values['Command'] == 'On'
# pylint: disable=protected-access # pylint: disable=protected-access
rfxtrx.RFX_DEVICES[entity_id]._state = is_on rfxtrx.RFX_DEVICES[device_id]._state = is_on
rfxtrx.RFX_DEVICES[entity_id].update_ha_state() rfxtrx.RFX_DEVICES[device_id].update_ha_state()
elif event.values['Command'] == 'Set level': elif event.values['Command'] == 'Set level':
# pylint: disable=protected-access # pylint: disable=protected-access
rfxtrx.RFX_DEVICES[entity_id]._brightness = \ rfxtrx.RFX_DEVICES[device_id]._brightness = \
(event.values['Dim level'] * 255 // 100) (event.values['Dim level'] * 255 // 100)
# Update the rfxtrx device state # Update the rfxtrx device state
is_on = rfxtrx.RFX_DEVICES[entity_id]._brightness > 0 is_on = rfxtrx.RFX_DEVICES[device_id]._brightness > 0
rfxtrx.RFX_DEVICES[entity_id]._state = is_on rfxtrx.RFX_DEVICES[device_id]._state = is_on
rfxtrx.RFX_DEVICES[entity_id].update_ha_state() rfxtrx.RFX_DEVICES[device_id].update_ha_state()
else: else:
return return
# Fire event # Fire event
if rfxtrx.RFX_DEVICES[entity_id].should_fire_event: if rfxtrx.RFX_DEVICES[device_id].should_fire_event:
rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire( rfxtrx.RFX_DEVICES[device_id].hass.bus.fire(
EVENT_BUTTON_PRESSED, { EVENT_BUTTON_PRESSED, {
ATTR_ENTITY_ID: ATTR_ENTITY_ID:
rfxtrx.RFX_DEVICES[entity_id].entity_id, rfxtrx.RFX_DEVICES[device_id].device_id,
ATTR_STATE: event.values['Command'].lower() ATTR_STATE: event.values['Command'].lower()
} }
) )

View File

@ -21,6 +21,7 @@ ATTR_STATE = 'state'
ATTR_NAME = 'name' ATTR_NAME = 'name'
ATTR_PACKETID = 'packetid' ATTR_PACKETID = 'packetid'
ATTR_FIREEVENT = 'fire_event' ATTR_FIREEVENT = 'fire_event'
ATTR_DATA_TYPE = 'data_type'
EVENT_BUTTON_PRESSED = 'button_pressed' EVENT_BUTTON_PRESSED = 'button_pressed'

View File

@ -11,6 +11,8 @@ import homeassistant.components.rfxtrx as rfxtrx
from homeassistant.const import TEMP_CELCIUS from homeassistant.const import TEMP_CELCIUS
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.util import slugify from homeassistant.util import slugify
from homeassistant.components.rfxtrx import (
ATTR_PACKETID, ATTR_NAME, ATTR_DATA_TYPE)
DEPENDENCIES = ['rfxtrx'] DEPENDENCIES = ['rfxtrx']
@ -29,25 +31,41 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
"""Setup the RFXtrx platform.""" """Setup the RFXtrx platform."""
from RFXtrx import SensorEvent from RFXtrx import SensorEvent
sensors = []
for device_id, entity_info in config.get('devices', {}).items():
if device_id not in rfxtrx.RFX_DEVICES:
_LOGGER.info("Add %s rfxtrx.sensor", entity_info[ATTR_NAME])
event = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID])
new_sensor = RfxtrxSensor(event, entity_info[ATTR_NAME],
entity_info.get(ATTR_DATA_TYPE, None))
rfxtrx.RFX_DEVICES[device_id] = new_sensor
sensors.append(new_sensor)
add_devices_callback(sensors)
def sensor_update(event): def sensor_update(event):
"""Callback for sensor updates from the RFXtrx gateway.""" """Callback for sensor updates from the RFXtrx gateway."""
if isinstance(event, SensorEvent): if not isinstance(event, SensorEvent):
entity_id = slugify(event.device.id_string.lower()) return
device_id = "sensor_" + slugify(event.device.id_string.lower())
if device_id in rfxtrx.RFX_DEVICES:
rfxtrx.RFX_DEVICES[device_id].event = event
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: if config.get('automatic_add', True):
automatic_add = config.get('automatic_add', True) pkt_id = "".join("{0:02x}".format(x) for x in event.data)
if automatic_add: entity_name = "%s : %s" % (device_id, pkt_id)
_LOGGER.info("Automatic add %s rfxtrx.sensor", entity_id) _LOGGER.info(
new_sensor = RfxtrxSensor(event) "Automatic add rfxtrx.sensor: (%s : %s)",
rfxtrx.RFX_DEVICES[entity_id] = new_sensor device_id,
pkt_id)
new_sensor = RfxtrxSensor(event, entity_name)
rfxtrx.RFX_DEVICES[device_id] = new_sensor
add_devices_callback([new_sensor]) add_devices_callback([new_sensor])
else:
_LOGGER.debug(
"EntityID: %s sensor_update",
entity_id,
)
rfxtrx.RFX_DEVICES[entity_id].event = event
if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS: if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS:
rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(sensor_update) rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(sensor_update)
@ -56,21 +74,21 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
class RfxtrxSensor(Entity): class RfxtrxSensor(Entity):
"""Represents a RFXtrx sensor.""" """Represents a RFXtrx sensor."""
def __init__(self, event): def __init__(self, event, name, data_type=None):
self.event = event self.event = event
self._unit_of_measurement = None self._unit_of_measurement = None
self._data_type = None self._data_type = None
self._name = name
if data_type:
self._data_type = data_type
self._unit_of_measurement = DATA_TYPES[data_type]
return
for data_type in DATA_TYPES: for data_type in DATA_TYPES:
if data_type in self.event.values: if data_type in self.event.values:
self._unit_of_measurement = DATA_TYPES[data_type] self._unit_of_measurement = DATA_TYPES[data_type]
self._data_type = data_type self._data_type = data_type
break break
id_string = int(event.device.id_string.replace(":", ""), 16)
self._name = "{} {} ({})".format(self._data_type,
self.event.device.type_string,
id_string)
def __str__(self): def __str__(self):
"""Returns the name.""" """Returns the name."""
return self._name return self._name

View File

@ -27,11 +27,9 @@ 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')
signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS) signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS)
if devices: for device_id, entity_info in config.get('devices', {}).items():
for entity_id, entity_info in devices.items(): if device_id not in rfxtrx.RFX_DEVICES:
if entity_id not in rfxtrx.RFX_DEVICES:
_LOGGER.info("Add %s rfxtrx.switch", entity_info[ATTR_NAME]) _LOGGER.info("Add %s rfxtrx.switch", entity_info[ATTR_NAME])
# Check if i must fire event # Check if i must fire event
@ -42,7 +40,7 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
newswitch = RfxtrxSwitch( newswitch = RfxtrxSwitch(
entity_info[ATTR_NAME], rfxobject, datas, entity_info[ATTR_NAME], rfxobject, datas,
signal_repetitions) signal_repetitions)
rfxtrx.RFX_DEVICES[entity_id] = newswitch rfxtrx.RFX_DEVICES[device_id] = newswitch
switchs.append(newswitch) switchs.append(newswitch)
add_devices_callback(switchs) add_devices_callback(switchs)
@ -54,33 +52,33 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
return return
# Add entity if not exist and the automatic_add is True # Add entity if not exist and the automatic_add is True
entity_id = slugify(event.device.id_string.lower()) device_id = slugify(event.device.id_string.lower())
if entity_id not in rfxtrx.RFX_DEVICES: if device_id not in rfxtrx.RFX_DEVICES:
automatic_add = config.get('automatic_add', False) automatic_add = config.get('automatic_add', False)
if not automatic_add: if not automatic_add:
return return
_LOGGER.info( _LOGGER.info(
"Automatic add %s rfxtrx.switch (Class: %s Sub: %s)", "Automatic add %s rfxtrx.switch (Class: %s Sub: %s)",
entity_id, device_id,
event.device.__class__.__name__, event.device.__class__.__name__,
event.device.subtype event.device.subtype
) )
pkt_id = "".join("{0:02x}".format(x) for x in event.data) 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} datas = {ATTR_STATE: False, ATTR_FIREEVENT: False}
signal_repetitions = config.get('signal_repetitions', signal_repetitions = config.get('signal_repetitions',
SIGNAL_REPETITIONS) SIGNAL_REPETITIONS)
new_switch = RfxtrxSwitch(entity_name, event, datas, new_switch = RfxtrxSwitch(entity_name, event, datas,
signal_repetitions) signal_repetitions)
rfxtrx.RFX_DEVICES[entity_id] = new_switch rfxtrx.RFX_DEVICES[device_id] = new_switch
add_devices_callback([new_switch]) add_devices_callback([new_switch])
# Check if entity exists or previously added automatically # Check if entity exists or previously added automatically
if entity_id in rfxtrx.RFX_DEVICES: if device_id in rfxtrx.RFX_DEVICES:
_LOGGER.debug( _LOGGER.debug(
"EntityID: %s switch_update. Command: %s", "EntityID: %s switch_update. Command: %s",
entity_id, device_id,
event.values['Command'] event.values['Command']
) )
if event.values['Command'] == 'On'\ 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 # Update the rfxtrx device state
is_on = event.values['Command'] == 'On' is_on = event.values['Command'] == 'On'
# pylint: disable=protected-access # pylint: disable=protected-access
rfxtrx.RFX_DEVICES[entity_id]._state = is_on rfxtrx.RFX_DEVICES[device_id]._state = is_on
rfxtrx.RFX_DEVICES[entity_id].update_ha_state() rfxtrx.RFX_DEVICES[device_id].update_ha_state()
# Fire event # Fire event
if rfxtrx.RFX_DEVICES[entity_id].should_fire_event: if rfxtrx.RFX_DEVICES[device_id].should_fire_event:
rfxtrx.RFX_DEVICES[entity_id].hass.bus.fire( rfxtrx.RFX_DEVICES[device_id].hass.bus.fire(
EVENT_BUTTON_PRESSED, { EVENT_BUTTON_PRESSED, {
ATTR_ENTITY_ID: ATTR_ENTITY_ID:
rfxtrx.RFX_DEVICES[entity_id].entity_id, rfxtrx.RFX_DEVICES[device_id].device_id,
ATTR_STATE: event.values['Command'].lower() ATTR_STATE: event.values['Command'].lower()
} }
) )