From 1e52d5c7f22122df1a869e1ee107d741b692e95b Mon Sep 17 00:00:00 2001 From: MartinHjelmare Date: Wed, 9 Dec 2015 04:43:18 +0100 Subject: [PATCH] Add S_TYPES to platform type and fix persistence * Add S_TYPES to platform type. * Fix persistence update on startup. * Clean up code. --- homeassistant/components/mysensors.py | 52 +++++++++++++++----- homeassistant/components/sensor/mysensors.py | 50 +++++++++++++------ homeassistant/components/switch/mysensors.py | 45 ++++++++++------- 3 files changed, 99 insertions(+), 48 deletions(-) diff --git a/homeassistant/components/mysensors.py b/homeassistant/components/mysensors.py index 1dcd65ab8e6..3ab1a96d80f 100644 --- a/homeassistant/components/mysensors.py +++ b/homeassistant/components/mysensors.py @@ -12,6 +12,7 @@ import logging from homeassistant.helpers import (validate_config) from homeassistant.const import ( + EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, TEMP_CELCIUS) @@ -94,9 +95,15 @@ def setup(hass, config): # noqa 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) + if persistence: - for nid in gateway.sensors: - gateway.event_callback('node_update', nid) + hass.bus.listen_once( + EVENT_HOMEASSISTANT_START, persistence_update) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, lambda event: gateway.stop()) @@ -134,33 +141,52 @@ def mysensors_update(platform_type): """Decorator for callback function for mysensor updates.""" def wrapper(gateway, port, devices, nid): """Wrapper function in the decorator.""" - sensor = gateway.sensors[nid] - if sensor.sketch_name is None: + if gateway.sensors[nid].sketch_name is None: _LOGGER.info('No sketch_name: node %s', nid) return if nid not in devices: devices[nid] = {} node = devices[nid] new_devices = [] - # Get platform specific V_TYPES, class and add_devices function. - platform_v_types, platform_class, add_devices = platform_type( - gateway, port, devices, nid) - for child_id, child in sensor.children.items(): + # Get platform specific S_TYPES, V_TYPES, class and add_devices. + (platform_s_types, + platform_v_types, + platform_class, + add_devices) = platform_type(gateway, port, devices, nid) + 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(): - if ((value_type not in node[child_id]) and - (value_type in platform_v_types)): + if (value_type not in node[child_id] and + child.type in platform_s_types and + value_type in platform_v_types): name = '{} {}.{}'.format( - sensor.sketch_name, nid, child.id) + 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 value_type in platform_v_types: + elif (child.type in platform_s_types and + value_type in platform_v_types): node[child_id][value_type].update_sensor( - child.values, sensor.battery_level) + 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 + + +def event_update(update): + """Decorator for callback function for mysensor event updates.""" + def wrapper(event): + """Wrapper function in the decorator.""" + _LOGGER.info( + 'update %s: node %s', event.data[ATTR_UPDATE_TYPE], + event.data[ATTR_NODE_ID]) + sensor_update = update(event) + sensor_update(GATEWAYS[event.data[ATTR_PORT]], + 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 9c4d3d3fcc4..e3843448763 100644 --- a/homeassistant/components/sensor/mysensors.py +++ b/homeassistant/components/sensor/mysensors.py @@ -23,31 +23,49 @@ DEPENDENCIES = ['mysensors'] def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the mysensors platform for sensors.""" - # Define the V_TYPES that the platform should handle as states. + # Define the S_TYPES and V_TYPES that the platform should handle as states. + s_types = [ + mysensors.CONST.Presentation.S_TEMP, + mysensors.CONST.Presentation.S_HUM, + mysensors.CONST.Presentation.S_BARO, + mysensors.CONST.Presentation.S_WIND, + mysensors.CONST.Presentation.S_RAIN, + mysensors.CONST.Presentation.S_UV, + mysensors.CONST.Presentation.S_WEIGHT, + mysensors.CONST.Presentation.S_POWER, + mysensors.CONST.Presentation.S_DISTANCE, + mysensors.CONST.Presentation.S_LIGHT_LEVEL, + mysensors.CONST.Presentation.S_IR, + mysensors.CONST.Presentation.S_WATER, + mysensors.CONST.Presentation.S_AIR_QUALITY, + 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 (member.value != mysensors.CONST.SetReq.V_ARMED and - member.value != mysensors.CONST.SetReq.V_STATUS and - member.value != mysensors.CONST.SetReq.V_LIGHT and - member.value != mysensors.CONST.SetReq.V_LOCK_STATUS): + if all(test != member.value for test in not_v_types): v_types.append(member) @mysensors.mysensors_update def _sensor_update(gateway, port, devices, nid): """Internal callback for sensor updates.""" - return (v_types, MySensorsSensor, add_devices) + return (s_types, v_types, MySensorsSensor, add_devices) - def sensor_update(event): - """Callback for sensor updates from the MySensors component.""" - _LOGGER.info( - 'update %s: node %s', event.data[mysensors.ATTR_UPDATE_TYPE], - event.data[mysensors.ATTR_NODE_ID]) - _sensor_update(mysensors.GATEWAYS[event.data[mysensors.ATTR_PORT]], - event.data[mysensors.ATTR_PORT], - event.data[mysensors.ATTR_DEVICES], - event.data[mysensors.ATTR_NODE_ID]) + @mysensors.event_update + def event_update(event): + """Callback for event updates from the MySensors component.""" + return _sensor_update - hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, sensor_update) + hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, event_update) class MySensorsSensor(Entity): diff --git a/homeassistant/components/switch/mysensors.py b/homeassistant/components/switch/mysensors.py index a2557900141..792502aef07 100644 --- a/homeassistant/components/switch/mysensors.py +++ b/homeassistant/components/switch/mysensors.py @@ -22,31 +22,38 @@ DEPENDENCIES = ['mysensors'] def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the mysensors platform for switches.""" - # Define the V_TYPES that the platform should handle as states. - v_types = [] - for _, member in mysensors.CONST.SetReq.__members__.items(): - if (member.value == mysensors.CONST.SetReq.V_ARMED or - member.value == mysensors.CONST.SetReq.V_STATUS or - member.value == mysensors.CONST.SetReq.V_LIGHT or - member.value == mysensors.CONST.SetReq.V_LOCK_STATUS): - v_types.append(member) + # Define the S_TYPES and V_TYPES that the platform should handle as states. + s_types = [ + mysensors.CONST.Presentation.S_DOOR, + mysensors.CONST.Presentation.S_MOTION, + mysensors.CONST.Presentation.S_SMOKE, + 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, + ] @mysensors.mysensors_update def _sensor_update(gateway, port, devices, nid): """Internal callback for sensor updates.""" - return (v_types, MySensorsSwitch, add_devices) + return (s_types, v_types, MySensorsSwitch, add_devices) - def sensor_update(event): - """Callback for sensor updates from the MySensors component.""" - _LOGGER.info( - 'update %s: node %s', event.data[mysensors.ATTR_UPDATE_TYPE], - event.data[mysensors.ATTR_NODE_ID]) - _sensor_update(mysensors.GATEWAYS[event.data[mysensors.ATTR_PORT]], - event.data[mysensors.ATTR_PORT], - event.data[mysensors.ATTR_DEVICES], - event.data[mysensors.ATTR_NODE_ID]) + @mysensors.event_update + def event_update(event): + """Callback for event updates from the MySensors component.""" + return _sensor_update - hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, sensor_update) + hass.bus.listen(mysensors.EVENT_MYSENSORS_NODE_UPDATE, event_update) class MySensorsSwitch(SwitchDevice):