mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 05:07:41 +00:00
Refactoring the code for pylint & flake test
This commit is contained in:
parent
174aeacd76
commit
d64f0ddd41
@ -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()
|
||||
self.update_ha_state()
|
||||
|
@ -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
|
||||
return True
|
||||
|
@ -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. """
|
||||
|
||||
|
@ -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()
|
||||
self.update_ha_state()
|
||||
|
Loading…
x
Reference in New Issue
Block a user