diff --git a/homeassistant/components/binary_sensor/mysensors.py b/homeassistant/components/binary_sensor/mysensors.py index 789e188537e..e938f946457 100644 --- a/homeassistant/components/binary_sensor/mysensors.py +++ b/homeassistant/components/binary_sensor/mysensors.py @@ -22,7 +22,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if discovery_info is None: return - for gateway in mysensors.GATEWAYS.values(): + gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS) + if not gateways: + return + + for gateway in gateways: # Define the S_TYPES and V_TYPES that the platform should handle as # states. Map them in a dict of lists. pres = gateway.const.Presentation diff --git a/homeassistant/components/climate/mysensors.py b/homeassistant/components/climate/mysensors.py index 2a815625434..13a062a335e 100755 --- a/homeassistant/components/climate/mysensors.py +++ b/homeassistant/components/climate/mysensors.py @@ -24,7 +24,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the mysensors climate.""" if discovery_info is None: return - for gateway in mysensors.GATEWAYS.values(): + + gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS) + if not gateways: + return + + for gateway in gateways: if float(gateway.protocol_version) < 1.5: continue pres = gateway.const.Presentation diff --git a/homeassistant/components/cover/mysensors.py b/homeassistant/components/cover/mysensors.py index aa3d866bcd6..7dd63a8c745 100644 --- a/homeassistant/components/cover/mysensors.py +++ b/homeassistant/components/cover/mysensors.py @@ -18,7 +18,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None): """Setup the mysensors platform for covers.""" if discovery_info is None: return - for gateway in mysensors.GATEWAYS.values(): + + gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS) + if not gateways: + return + + for gateway in gateways: pres = gateway.const.Presentation set_req = gateway.const.SetReq map_sv_types = { diff --git a/homeassistant/components/light/mysensors.py b/homeassistant/components/light/mysensors.py index 3bd53ff9064..20da91682ad 100644 --- a/homeassistant/components/light/mysensors.py +++ b/homeassistant/components/light/mysensors.py @@ -31,7 +31,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if discovery_info is None: return - for gateway in mysensors.GATEWAYS.values(): + gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS) + if not gateways: + return + + for gateway in gateways: # Define the S_TYPES and V_TYPES that the platform should handle as # states. Map them in a dict of lists. pres = gateway.const.Presentation diff --git a/homeassistant/components/mysensors.py b/homeassistant/components/mysensors.py index b86bed57b82..b6778760b1a 100644 --- a/homeassistant/components/mysensors.py +++ b/homeassistant/components/mysensors.py @@ -38,7 +38,7 @@ DEFAULT_VERSION = 1.4 DEFAULT_BAUD_RATE = 115200 DEFAULT_TCP_PORT = 5003 DOMAIN = 'mysensors' -GATEWAYS = None +MYSENSORS_GATEWAYS = 'mysensors_gateways' MQTT_COMPONENT = 'mqtt' REQUIREMENTS = [ 'https://github.com/theolind/pymysensors/archive/' @@ -132,9 +132,15 @@ def setup(hass, config): return gateway + gateways = hass.data.get(MYSENSORS_GATEWAYS) + if gateways is not None: + _LOGGER.error( + '%s already exists in %s, will not setup %s component', + MYSENSORS_GATEWAYS, hass.data, DOMAIN) + return False + # Setup all devices from config - global GATEWAYS - GATEWAYS = {} + gateways = [] conf_gateways = config[DOMAIN][CONF_GATEWAYS] for index, gway in enumerate(conf_gateways): @@ -146,17 +152,19 @@ def setup(hass, config): tcp_port = gway.get(CONF_TCP_PORT) in_prefix = gway.get(CONF_TOPIC_IN_PREFIX) out_prefix = gway.get(CONF_TOPIC_OUT_PREFIX) - GATEWAYS[device] = setup_gateway( + ready_gateway = setup_gateway( device, persistence_file, baud_rate, tcp_port, in_prefix, out_prefix) - if GATEWAYS[device] is None: - GATEWAYS.pop(device) + if ready_gateway is not None: + gateways.append(ready_gateway) - if not GATEWAYS: + if not gateways: _LOGGER.error( 'No devices could be setup as gateways, check your configuration') return False + hass.data[MYSENSORS_GATEWAYS] = gateways + for component in ['sensor', 'switch', 'light', 'binary_sensor', 'climate', 'cover']: discovery.load_platform(hass, component, DOMAIN, {}, config) diff --git a/homeassistant/components/sensor/mysensors.py b/homeassistant/components/sensor/mysensors.py index ec39f0aea56..2742713cb24 100644 --- a/homeassistant/components/sensor/mysensors.py +++ b/homeassistant/components/sensor/mysensors.py @@ -20,7 +20,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if discovery_info is None: return - for gateway in mysensors.GATEWAYS.values(): + gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS) + if not gateways: + return + + for gateway in gateways: # Define the S_TYPES and V_TYPES that the platform should handle as # states. Map them in a dict of lists. pres = gateway.const.Presentation diff --git a/homeassistant/components/switch/mysensors.py b/homeassistant/components/switch/mysensors.py index f303a07686c..6c26a79f21a 100644 --- a/homeassistant/components/switch/mysensors.py +++ b/homeassistant/components/switch/mysensors.py @@ -34,7 +34,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None): if discovery_info is None: return - for gateway in mysensors.GATEWAYS.values(): + gateways = hass.data.get(mysensors.MYSENSORS_GATEWAYS) + if not gateways: + return + + for gateway in gateways: # Define the S_TYPES and V_TYPES that the platform should handle as # states. Map them in a dict of lists. pres = gateway.const.Presentation