diff --git a/homeassistant/components/light/rfxtrx.py b/homeassistant/components/light/rfxtrx.py index 60d61c95a42..194676c8054 100644 --- a/homeassistant/components/light/rfxtrx.py +++ b/homeassistant/components/light/rfxtrx.py @@ -26,24 +26,22 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): import RFXtrx as rfxtrxmod lights = [] - devices = config.get('devices', None) 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.light", 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.light", 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]) - new_light = RfxtrxLight( - entity_info[ATTR_NAME], rfxobject, datas, - signal_repetitions) - rfxtrx.RFX_DEVICES[entity_id] = new_light - lights.append(new_light) + rfxobject = rfxtrx.get_rfx_object(entity_info[ATTR_PACKETID]) + new_light = RfxtrxLight( + entity_info[ATTR_NAME], rfxobject, datas, + signal_repetitions) + rfxtrx.RFX_DEVICES[device_id] = new_light + lights.append(new_light) add_devices_callback(lights) @@ -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.light (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_light = RfxtrxLight(entity_name, event, datas, signal_repetitions) - rfxtrx.RFX_DEVICES[entity_id] = new_light + rfxtrx.RFX_DEVICES[device_id] = new_light add_devices_callback([new_light]) # 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 light_update. Command: %s", - entity_id, + device_id, event.values['Command'] ) @@ -90,27 +88,27 @@ 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() elif event.values['Command'] == 'Set level': # pylint: disable=protected-access - rfxtrx.RFX_DEVICES[entity_id]._brightness = \ + rfxtrx.RFX_DEVICES[device_id]._brightness = \ (event.values['Dim level'] * 255 // 100) # Update the rfxtrx device state - is_on = rfxtrx.RFX_DEVICES[entity_id]._brightness > 0 - rfxtrx.RFX_DEVICES[entity_id]._state = is_on - rfxtrx.RFX_DEVICES[entity_id].update_ha_state() + is_on = rfxtrx.RFX_DEVICES[device_id]._brightness > 0 + rfxtrx.RFX_DEVICES[device_id]._state = is_on + rfxtrx.RFX_DEVICES[device_id].update_ha_state() else: return # 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() } ) diff --git a/homeassistant/components/rfxtrx.py b/homeassistant/components/rfxtrx.py index 4789348c69a..4b971530584 100644 --- a/homeassistant/components/rfxtrx.py +++ b/homeassistant/components/rfxtrx.py @@ -21,6 +21,7 @@ ATTR_STATE = 'state' ATTR_NAME = 'name' ATTR_PACKETID = 'packetid' ATTR_FIREEVENT = 'fire_event' +ATTR_DATA_TYPE = 'data_type' EVENT_BUTTON_PRESSED = 'button_pressed' diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py index d424d899bcc..4c1a96ce40d 100644 --- a/homeassistant/components/sensor/rfxtrx.py +++ b/homeassistant/components/sensor/rfxtrx.py @@ -11,6 +11,8 @@ import homeassistant.components.rfxtrx as rfxtrx from homeassistant.const import TEMP_CELCIUS from homeassistant.helpers.entity import Entity from homeassistant.util import slugify +from homeassistant.components.rfxtrx import ( + ATTR_PACKETID, ATTR_NAME, ATTR_DATA_TYPE) DEPENDENCIES = ['rfxtrx'] @@ -29,25 +31,41 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): """Setup the RFXtrx platform.""" 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): """Callback for sensor updates from the RFXtrx gateway.""" - if isinstance(event, SensorEvent): - entity_id = slugify(event.device.id_string.lower()) + if not isinstance(event, SensorEvent): + return - # 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', True) - if automatic_add: - _LOGGER.info("Automatic add %s rfxtrx.sensor", entity_id) - new_sensor = RfxtrxSensor(event) - rfxtrx.RFX_DEVICES[entity_id] = new_sensor - add_devices_callback([new_sensor]) - else: - _LOGGER.debug( - "EntityID: %s sensor_update", - entity_id, - ) - rfxtrx.RFX_DEVICES[entity_id].event = event + 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 + if config.get('automatic_add', True): + pkt_id = "".join("{0:02x}".format(x) for x in event.data) + entity_name = "%s : %s" % (device_id, pkt_id) + _LOGGER.info( + "Automatic add rfxtrx.sensor: (%s : %s)", + device_id, + pkt_id) + + new_sensor = RfxtrxSensor(event, entity_name) + rfxtrx.RFX_DEVICES[device_id] = new_sensor + add_devices_callback([new_sensor]) if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS: 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): """Represents a RFXtrx sensor.""" - def __init__(self, event): + def __init__(self, event, name, data_type=None): self.event = event self._unit_of_measurement = 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: if data_type in self.event.values: self._unit_of_measurement = DATA_TYPES[data_type] self._data_type = data_type 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): """Returns the name.""" return self._name diff --git a/homeassistant/components/switch/rfxtrx.py b/homeassistant/components/switch/rfxtrx.py index 52cbac2319d..52168db3167 100644 --- a/homeassistant/components/switch/rfxtrx.py +++ b/homeassistant/components/switch/rfxtrx.py @@ -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() } )