From d64f0ddd412b4a8bbcbe0ea5cc07df2355ad1633 Mon Sep 17 00:00:00 2001 From: badele Date: Tue, 29 Sep 2015 08:20:25 +0200 Subject: [PATCH] Refactoring the code for pylint & flake test --- homeassistant/components/light/rfxtrx.py | 12 +++++++----- homeassistant/components/rfxtrx.py | 9 ++++----- homeassistant/components/sensor/rfxtrx.py | 22 ++++++++++++---------- homeassistant/components/switch/rfxtrx.py | 12 +++++++----- 4 files changed, 30 insertions(+), 25 deletions(-) diff --git a/homeassistant/components/light/rfxtrx.py b/homeassistant/components/light/rfxtrx.py index ed9c8f99dfc..ad3fbb03d4a 100644 --- a/homeassistant/components/light/rfxtrx.py +++ b/homeassistant/components/light/rfxtrx.py @@ -33,17 +33,18 @@ REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/' + 'ec7a1aaddf8270db6e5da1c13d58c1547effd7cf.zip#RFXtrx==0.15'] DOMAIN = "rfxtrx" +_LOGGER = logging.getLogger(__name__) + def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Setup the RFXtrx platform. """ - logger = logging.getLogger(__name__) # Add light from config file lights = [] devices = config.get('devices') for entity_id, entity_name in devices.items(): if entity_id not in rfxtrx.RFX_DEVICES: - logger.info("Add %s rfxtrx.light" % entity_name) + _LOGGER.info("Add %s rfxtrx.light", entity_name) new_light = RfxtrxLight(entity_name, False) rfxtrx.RFX_DEVICES[entity_id] = new_light lights.append(new_light) @@ -59,14 +60,15 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): if entity_id not in rfxtrx.RFX_DEVICES: automatic_add = config.get('automatic_add', False) if automatic_add: - logger.info("Automatic add %s rfxtrx.light" % entity_name) + _LOGGER.info("Automatic add %s rfxtrx.light", entity_id) new_light = RfxtrxLight(entity_id, False) rfxtrx.RFX_DEVICES[entity_id] = new_light add_devices_callback([new_light]) # Check if entity exists (previous automatic added) if entity_id in rfxtrx.RFX_DEVICES: - if event.values['Command'] == 'On' or event.values['Command'] == 'Off': + if event.values['Command'] == 'On'\ + or event.values['Command'] == 'Off': if event.values['Command'] == 'On': rfxtrx.RFX_DEVICES[entity_id].turn_on() else: @@ -105,4 +107,4 @@ class RfxtrxLight(Light): def turn_off(self, **kwargs): """ Turn the device off. """ self._state = False - self.update_ha_state() \ No newline at end of file + self.update_ha_state() diff --git a/homeassistant/components/rfxtrx.py b/homeassistant/components/rfxtrx.py index 788778debeb..d72a9fe02ec 100644 --- a/homeassistant/components/rfxtrx.py +++ b/homeassistant/components/rfxtrx.py @@ -14,13 +14,12 @@ DOMAIN = "rfxtrx" CONF_DEVICE = 'device' RECEIVED_EVT_SUBSCRIBERS = [] RFX_DEVICES = {} +_LOGGER = logging.getLogger(__name__) + def setup(hass, config): """ Setup the Rfxtrx component. """ - # Init logger - logger = logging.getLogger(__name__) - # Declare the Handle event def handle_receive(event): """ Callback all subscribers for RFXtrx gateway. """ @@ -31,11 +30,11 @@ def setup(hass, config): try: import RFXtrx as rfxtrxmod except ImportError: - logger.exception("Failed to import rfxtrx") + _LOGGER.exception("Failed to import rfxtrx") return False # Init the rfxtrx module device = config[DOMAIN][CONF_DEVICE] rfxtrxmod.Core(device, handle_receive) - return True \ No newline at end of file + return True diff --git a/homeassistant/components/sensor/rfxtrx.py b/homeassistant/components/sensor/rfxtrx.py index 7dc6496555c..625fd50de84 100644 --- a/homeassistant/components/sensor/rfxtrx.py +++ b/homeassistant/components/sensor/rfxtrx.py @@ -37,30 +37,32 @@ DATA_TYPES = OrderedDict([ ('Barometer', ''), ('Wind direction', ''), ('Rain rate', '')]) +_LOGGER = logging.getLogger(__name__) def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Setup the RFXtrx platform. """ - logger = logging.getLogger(__name__) - - sensors = {} # keep track of sensors added to HA def sensor_update(event): """ Callback for sensor updates from the RFXtrx gateway. """ if isinstance(event.device, SensorEvent): - entity_id = '%s-%s' % (event.device.type_string.lower(), slugify(event.device.id_string.lower())) - if entity_id in rfxtrx.RFX_DEVICES: - rfxtrx.RFX_DEVICES[entity_id].event = event - else: + entity_id = slugify(event.device.id_string.lower()) + + # 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', False) if automatic_add: - new_light = RfxtrxSensor(entity_id, False) - rfxtrx.RFX_DEVICES[entity_id] = new_light - add_devices_callback([new_light]) + _LOGGER.info("Automatic add %s rfxtrx.light", entity_id) + new_sensor = RfxtrxSensor(event) + rfxtrx.RFX_DEVICES[entity_id] = new_sensor + add_devices_callback([new_sensor]) + else: + rfxtrx.RFX_DEVICES[entity_id].event = event if sensor_update not in rfxtrx.RECEIVED_EVT_SUBSCRIBERS: rfxtrx.RECEIVED_EVT_SUBSCRIBERS.append(sensor_update) + class RfxtrxSensor(Entity): """ Represents a RFXtrx sensor. """ diff --git a/homeassistant/components/switch/rfxtrx.py b/homeassistant/components/switch/rfxtrx.py index 3f2b957cc25..b64d0d9bf7a 100644 --- a/homeassistant/components/switch/rfxtrx.py +++ b/homeassistant/components/switch/rfxtrx.py @@ -33,17 +33,18 @@ REQUIREMENTS = ['https://github.com/Danielhiversen/pyRFXtrx/archive/' + 'ec7a1aaddf8270db6e5da1c13d58c1547effd7cf.zip#RFXtrx==0.15'] DOMAIN = "rfxtrx" +_LOGGER = logging.getLogger(__name__) + def setup_platform(hass, config, add_devices_callback, discovery_info=None): """ Setup the RFXtrx platform. """ - logger = logging.getLogger(__name__) # Add switch from config file switchs = [] devices = config.get('devices') for entity_id, entity_name in devices.items(): if entity_id not in rfxtrx.RFX_DEVICES: - logger.info("Add %s rfxtrx.switch" % entity_name) + _LOGGER.info("Add %s rfxtrx.switch", entity_name) new_switch = RfxtrxSwitch(entity_name, False) rfxtrx.RFX_DEVICES[entity_id] = new_switch switchs.append(new_switch) @@ -59,14 +60,15 @@ def setup_platform(hass, config, add_devices_callback, discovery_info=None): if entity_id not in rfxtrx.RFX_DEVICES: automatic_add = config.get('automatic_add', False) if automatic_add: - logger.info("Automatic add %s rfxtrx.switch" % entity_name) + _LOGGER.info("Automatic add %s rfxtrx.switch", entity_id) new_switch = RfxtrxSwitch(entity_id, False) rfxtrx.RFX_DEVICES[entity_id] = new_switch add_devices_callback([new_switch]) # Check if entity exists (previous automatic added) if entity_id in rfxtrx.RFX_DEVICES: - if event.values['Command'] == 'On' or event.values['Command'] == 'Off': + if event.values['Command'] == 'On'\ + or event.values['Command'] == 'Off': if event.values['Command'] == 'On': rfxtrx.RFX_DEVICES[entity_id].turn_on() else: @@ -105,4 +107,4 @@ class RfxtrxSwitch(SwitchDevice): def turn_off(self, **kwargs): """ Turn the device off. """ self._state = False - self.update_ha_state() \ No newline at end of file + self.update_ha_state()