From 9117fa6eb820decc64bb9390b5d489bd19db4e4e Mon Sep 17 00:00:00 2001 From: MartinHjelmare Date: Tue, 26 Jan 2016 03:35:34 +0100 Subject: [PATCH] Fix adding devices on the fly Devices were not added without persistence enabled and restart of HA. Node id was added to defaultdict(list) by mistake when checking if list of defaultdict was empty. * Fix adding devices in mysensors_callback. * Change devices to regular dict. --- homeassistant/components/mysensors.py | 28 +++++++++----------- homeassistant/components/sensor/mysensors.py | 3 +-- homeassistant/components/switch/mysensors.py | 3 +-- 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/mysensors.py b/homeassistant/components/mysensors.py index 97c2329656b..48fa95eaea3 100644 --- a/homeassistant/components/mysensors.py +++ b/homeassistant/components/mysensors.py @@ -143,28 +143,24 @@ def pf_callback_factory( if gateway.sensors[node_id].sketch_name is None: _LOGGER.info('No sketch_name: node %s', node_id) return - # previously discovered, just update state with latest info - if node_id in devices: - for entity in devices[node_id]: - entity.update_ha_state(True) - return - # First time we see this node, detect sensors for child in gateway.sensors[node_id].children.values(): - name = '{} {}.{}'.format( - gateway.sensors[node_id].sketch_name, node_id, child.id) - for value_type in child.values.keys(): + key = node_id, child.id, value_type if child.type not in s_types or value_type not in v_types: continue + if key in devices: + devices[key].update_ha_state(True) + continue + name = '{} {}.{}'.format( + gateway.sensors[node_id].sketch_name, node_id, child.id) + devices[key] = entity_class( + gateway, node_id, child.id, name, value_type) - devices[node_id].append( - entity_class(gateway, node_id, child.id, name, value_type)) - if devices[node_id]: - _LOGGER.info('adding new devices: %s', devices[node_id]) - add_devices(devices[node_id]) - for entity in devices[node_id]: - entity.update_ha_state(True) + _LOGGER.info('Adding new devices: %s', devices[key]) + add_devices([devices[key]]) + if key in devices: + devices[key].update_ha_state(True) return mysensors_callback diff --git a/homeassistant/components/sensor/mysensors.py b/homeassistant/components/sensor/mysensors.py index a3d5da11cbb..fba0e8edcb7 100644 --- a/homeassistant/components/sensor/mysensors.py +++ b/homeassistant/components/sensor/mysensors.py @@ -7,7 +7,6 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.mysensors/ """ import logging -from collections import defaultdict from homeassistant.helpers.entity import Entity @@ -72,7 +71,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): v_types = [member for member in gateway.const.SetReq if member.value not in not_v_types] - devices = defaultdict(list) + devices = {} gateway.platform_callbacks.append(mysensors.pf_callback_factory( s_types, v_types, devices, add_devices, MySensorsSensor)) diff --git a/homeassistant/components/switch/mysensors.py b/homeassistant/components/switch/mysensors.py index d8d7d4d2473..ec9845c0bf1 100644 --- a/homeassistant/components/switch/mysensors.py +++ b/homeassistant/components/switch/mysensors.py @@ -7,7 +7,6 @@ For more details about this platform, please refer to the documentation at https://home-assistant.io/components/sensor.mysensors.html """ import logging -from collections import defaultdict from homeassistant.components.switch import SwitchDevice @@ -54,7 +53,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): ]) v_types.extend([gateway.const.SetReq.V_STATUS, ]) - devices = defaultdict(list) + devices = {} gateway.platform_callbacks.append(mysensors.pf_callback_factory( s_types, v_types, devices, add_devices, MySensorsSwitch))