From 9f54bcc21b1744f2850a35e54903f3581d14badb Mon Sep 17 00:00:00 2001 From: MartinHjelmare Date: Wed, 23 Dec 2015 23:20:39 +0100 Subject: [PATCH] Fix comments for pull request * Fix cleaner user config. * Remove bad disabling of linting. * Extract default mysensors version into constant. * Clean up selection of mysensors.CONST from version. * Update mysensors update decorator to add devices and update values in one go. * Fix persistence update. * Clean up setup of ports. * Setup of mysensors platforms from main mysensors component. * Clean up v_types selection in mysensors sensor platform. * Fix s_types and v_types selection version dependency in platforms. --- homeassistant/components/mysensors.py | 108 ++++++++++--------- homeassistant/components/sensor/mysensors.py | 17 +-- homeassistant/components/switch/mysensors.py | 17 +-- 3 files changed, 78 insertions(+), 64 deletions(-) diff --git a/homeassistant/components/mysensors.py b/homeassistant/components/mysensors.py index 0e2ba92627f..89bc14a4ef8 100644 --- a/homeassistant/components/mysensors.py +++ b/homeassistant/components/mysensors.py @@ -17,6 +17,16 @@ Multiple gateways are now supported. Configuration.yaml: +mysensors: + gateways: + - port: '/dev/ttyUSB0' + persistence_file: 'path/mysensors.json' + - port: '/dev/ttyACM1' + persistence_file: 'path/mysensors2.json' + debug: true + persistence: true + version: '1.5' + mysensors: port: - '/dev/ttyUSB0' @@ -36,18 +46,23 @@ switch: """ import logging -from homeassistant.helpers import (validate_config) +from homeassistant.helpers import validate_config +import homeassistant.bootstrap as bootstrap from homeassistant.const import ( EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, - TEMP_CELCIUS) + TEMP_CELCIUS, + CONF_PLATFORM) +CONF_GATEWAYS = 'gateways' CONF_PORT = 'port' CONF_DEBUG = 'debug' CONF_PERSISTENCE = 'persistence' CONF_PERSISTENCE_FILE = 'persistence_file' CONF_VERSION = 'version' +DEFAULT_VERSION = '1.4' +VERSION = None DOMAIN = 'mysensors' DEPENDENCIES = [] @@ -61,29 +76,31 @@ ATTR_NODE_ID = 'node_id' ATTR_CHILD_ID = 'child_id' ATTR_UPDATE_TYPE = 'update_type' +COMPONENTS_WITH_MYSENSORS_PLATFORM = [ + 'sensor', + 'switch', +] + IS_METRIC = None CONST = None GATEWAYS = None EVENT_MYSENSORS_NODE_UPDATE = 'MYSENSORS_NODE_UPDATE' -def setup(hass, config): # noqa - """ Setup the MySensors component. """ - # pylint:disable=no-name-in-module +def setup(hass, config): + """Setup the MySensors component.""" import mysensors.mysensors as mysensors if not validate_config(config, - {DOMAIN: [CONF_PORT]}, + {DOMAIN: [CONF_GATEWAYS]}, _LOGGER): return False - version = config[DOMAIN].get(CONF_VERSION, '1.4') + global VERSION + VERSION = config[DOMAIN].get(CONF_VERSION, DEFAULT_VERSION) global CONST - if version == '1.4': - import mysensors.const_14 as const - CONST = const - elif version == '1.5': + if VERSION == '1.5': import mysensors.const_15 as const CONST = const else: @@ -95,7 +112,14 @@ def setup(hass, config): # noqa global IS_METRIC IS_METRIC = (hass.config.temperature_unit == TEMP_CELCIUS) - def callback_generator(port, devices): + # Setup mysensors platforms + mysensors_config = config.copy() + for component in COMPONENTS_WITH_MYSENSORS_PLATFORM: + mysensors_config[component] = {CONF_PLATFORM: 'mysensors'} + if not bootstrap.setup_component(hass, component, mysensors_config): + return False + + def callback_factory(port, devices): """Return a new callback function. Run once per gateway setup.""" def node_update(update_type, nid): """Callback for node updates from the MySensors gateway.""" @@ -107,7 +131,7 @@ def setup(hass, config): # noqa ATTR_UPDATE_TYPE: update_type, ATTR_NODE_ID: nid }) - return + return node_update def setup_gateway(port, persistence, persistence_file): @@ -116,17 +140,16 @@ def setup(hass, config): # noqa gateway = mysensors.SerialGateway(port, persistence=persistence, persistence_file=persistence_file, - protocol_version=version) - gateway.event_callback = callback_generator(port, devices) + protocol_version=VERSION) + gateway.event_callback = callback_factory(port, devices) gateway.metric = IS_METRIC gateway.debug = config[DOMAIN].get(CONF_DEBUG, False) gateway.start() def persistence_update(event): """Callback to trigger update from persistence file.""" - for _ in range(2): - for nid in gateway.sensors: - gateway.event_callback('persistence', nid) + for nid in gateway.sensors: + gateway.event_callback('persistence', nid) if persistence: hass.bus.listen_once( @@ -134,32 +157,23 @@ def setup(hass, config): # noqa hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, lambda event: gateway.stop()) + return gateway - port = config[DOMAIN].get(CONF_PORT) - persistence_file = config[DOMAIN].get( - CONF_PERSISTENCE_FILE, hass.config.path('mysensors.pickle')) - - if isinstance(port, str): - port = [port] - if isinstance(persistence_file, str): - persistence_file = [persistence_file] - # Setup all ports from config global GATEWAYS GATEWAYS = {} - for index, port_item in enumerate(port): - persistence = config[DOMAIN].get(CONF_PERSISTENCE, True) - try: - persistence_f_item = persistence_file[index] - except IndexError: - _LOGGER.exception( - 'No persistence_file is set for port %s,' - ' disabling persistence', port_item) - persistence = False - persistence_f_item = None - GATEWAYS[port_item] = setup_gateway( - port_item, persistence, persistence_f_item) + conf_gateways = config[DOMAIN][CONF_GATEWAYS] + if isinstance(conf_gateways, dict): + conf_gateways = [conf_gateways] + persistence = config[DOMAIN].get(CONF_PERSISTENCE, True) + for index, gway in enumerate(conf_gateways): + port = gway[CONF_PORT] + persistence_file = gway.get( + CONF_PERSISTENCE_FILE, + hass.config.path('mysensors{}.pickle'.format(index + 1))) + GATEWAYS[port] = setup_gateway( + port, persistence, persistence_file) return True @@ -174,7 +188,6 @@ def mysensors_update(platform_type): if nid not in devices: devices[nid] = {} node = devices[nid] - new_devices = [] # Get platform specific S_TYPES, V_TYPES, class and add_devices. (platform_s_types, platform_v_types, @@ -183,7 +196,7 @@ def mysensors_update(platform_type): for child_id, child in gateway.sensors[nid].children.items(): if child_id not in node: node[child_id] = {} - for value_type, _ in child.values.items(): + for value_type in child.values.keys(): if (value_type not in node[child_id] and child.type in platform_s_types and value_type in platform_v_types): @@ -191,15 +204,13 @@ def mysensors_update(platform_type): gateway.sensors[nid].sketch_name, nid, child.id) node[child_id][value_type] = platform_class( port, nid, child_id, name, value_type) - new_devices.append(node[child_id][value_type]) - elif (child.type in platform_s_types and - value_type in platform_v_types): + _LOGGER.info('adding new device: %s', + node[child_id][value_type]) + add_devices([node[child_id][value_type]]) + if (child.type in platform_s_types and + value_type in platform_v_types): node[child_id][value_type].update_sensor( child.values, gateway.sensors[nid].battery_level) - if new_devices: - _LOGGER.info('adding new devices: %s', new_devices) - add_devices(new_devices) - return return wrapper @@ -215,5 +226,4 @@ def event_update(update): event.data[ATTR_PORT], event.data[ATTR_DEVICES], event.data[ATTR_NODE_ID]) - return return wrapper diff --git a/homeassistant/components/sensor/mysensors.py b/homeassistant/components/sensor/mysensors.py index e3843448763..16f047beaf4 100644 --- a/homeassistant/components/sensor/mysensors.py +++ b/homeassistant/components/sensor/mysensors.py @@ -18,7 +18,7 @@ from homeassistant.const import ( import homeassistant.components.mysensors as mysensors _LOGGER = logging.getLogger(__name__) -DEPENDENCIES = ['mysensors'] +DEPENDENCIES = [] def setup_platform(hass, config, add_devices, discovery_info=None): @@ -41,19 +41,20 @@ def setup_platform(hass, config, add_devices, discovery_info=None): mysensors.CONST.Presentation.S_CUSTOM, mysensors.CONST.Presentation.S_DUST, mysensors.CONST.Presentation.S_SCENE_CONTROLLER, - mysensors.CONST.Presentation.S_COLOR_SENSOR, - mysensors.CONST.Presentation.S_MULTIMETER, ] not_v_types = [ mysensors.CONST.SetReq.V_ARMED, - mysensors.CONST.SetReq.V_STATUS, mysensors.CONST.SetReq.V_LIGHT, mysensors.CONST.SetReq.V_LOCK_STATUS, ] - v_types = [] - for _, member in mysensors.CONST.SetReq.__members__.items(): - if all(test != member.value for test in not_v_types): - v_types.append(member) + if float(mysensors.VERSION) >= 1.5: + s_types.extend([ + mysensors.CONST.Presentation.S_COLOR_SENSOR, + mysensors.CONST.Presentation.S_MULTIMETER, + ]) + not_v_types.extend([mysensors.CONST.SetReq.V_STATUS, ]) + v_types = [member for member in mysensors.CONST.SetReq + if member.value not in not_v_types] @mysensors.mysensors_update def _sensor_update(gateway, port, devices, nid): diff --git a/homeassistant/components/switch/mysensors.py b/homeassistant/components/switch/mysensors.py index 792502aef07..efa3bd7f7c4 100644 --- a/homeassistant/components/switch/mysensors.py +++ b/homeassistant/components/switch/mysensors.py @@ -17,7 +17,7 @@ from homeassistant.const import ( import homeassistant.components.mysensors as mysensors _LOGGER = logging.getLogger(__name__) -DEPENDENCIES = ['mysensors'] +DEPENDENCIES = [] def setup_platform(hass, config, add_devices, discovery_info=None): @@ -30,18 +30,21 @@ def setup_platform(hass, config, add_devices, discovery_info=None): mysensors.CONST.Presentation.S_LIGHT, mysensors.CONST.Presentation.S_BINARY, mysensors.CONST.Presentation.S_LOCK, - mysensors.CONST.Presentation.S_SPRINKLER, - mysensors.CONST.Presentation.S_WATER_LEAK, - mysensors.CONST.Presentation.S_SOUND, - mysensors.CONST.Presentation.S_VIBRATION, - mysensors.CONST.Presentation.S_MOISTURE, ] v_types = [ mysensors.CONST.SetReq.V_ARMED, - mysensors.CONST.SetReq.V_STATUS, mysensors.CONST.SetReq.V_LIGHT, mysensors.CONST.SetReq.V_LOCK_STATUS, ] + if float(mysensors.VERSION) >= 1.5: + s_types.extend([ + mysensors.CONST.Presentation.S_SPRINKLER, + mysensors.CONST.Presentation.S_WATER_LEAK, + mysensors.CONST.Presentation.S_SOUND, + mysensors.CONST.Presentation.S_VIBRATION, + mysensors.CONST.Presentation.S_MOISTURE, + ]) + v_types.extend([mysensors.CONST.SetReq.V_STATUS, ]) @mysensors.mysensors_update def _sensor_update(gateway, port, devices, nid):