From 8ffa3684e3f08b4a2a84a10e15f31fe9ab7cd768 Mon Sep 17 00:00:00 2001
From: Daniel <Daniel.H.Iversen@ntnu.no>
Date: Thu, 25 Feb 2016 17:40:24 +0100
Subject: [PATCH] rfxtrx refactor

---
 homeassistant/components/light/rfxtrx.py  | 27 ++++++++++++-----------
 homeassistant/components/sensor/rfxtrx.py | 15 +++++++------
 homeassistant/components/switch/rfxtrx.py | 27 ++++++++++++-----------
 3 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/homeassistant/components/light/rfxtrx.py b/homeassistant/components/light/rfxtrx.py
index 194676c8054..60d30645e9f 100644
--- a/homeassistant/components/light/rfxtrx.py
+++ b/homeassistant/components/light/rfxtrx.py
@@ -29,19 +29,20 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
     signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS)
 
     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}
-
-            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)
+        if device_id in rfxtrx.RFX_DEVICES:
+            continue
+        _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}
+
+        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)
 
diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py
index 4c1a96ce40d..e4d1c4e37b4 100644
--- a/homeassistant/components/sensor/rfxtrx.py
+++ b/homeassistant/components/sensor/rfxtrx.py
@@ -33,13 +33,14 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
 
     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)
+        if device_id in rfxtrx.RFX_DEVICES:
+            continue
+        _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)
 
diff --git a/homeassistant/components/switch/rfxtrx.py b/homeassistant/components/switch/rfxtrx.py
index 52168db3167..799053920ae 100644
--- a/homeassistant/components/switch/rfxtrx.py
+++ b/homeassistant/components/switch/rfxtrx.py
@@ -29,19 +29,20 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None):
     switchs = []
     signal_repetitions = config.get('signal_repetitions', SIGNAL_REPETITIONS)
     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}
-
-            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)
+        if device_id in rfxtrx.RFX_DEVICES:
+            continue
+        _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}
+
+        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)
 
-- 
GitLab