Refactoring the code for pylint & flake test

This commit is contained in:
badele 2015-09-29 08:20:25 +02:00
parent 174aeacd76
commit d64f0ddd41
4 changed files with 30 additions and 25 deletions

View File

@ -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:

View File

@ -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,7 +30,7 @@ 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

View File

@ -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. """

View File

@ -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: