Fix mysensors overwriting gateway in GATEWAYS (#4013)

GATEWAYS was a dict, so would overwrite item if key was the same. This
would happen when using multiple MQTT gateways, since the device id is
the same (`mqtt`).

* Fix by changing GATEWAYS from dict into list.
* Use hass data to store mysensors gateways instead of having GATEWAYS
  be a global.
This commit is contained in:
Martin Hjelmare 2016-11-06 19:49:43 +01:00 committed by Paulus Schoutsen
parent 0c5e077091
commit 734bd75fd3
7 changed files with 47 additions and 13 deletions

View file

@ -22,7 +22,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if discovery_info is None: if discovery_info is None:
return 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 # Define the S_TYPES and V_TYPES that the platform should handle as
# states. Map them in a dict of lists. # states. Map them in a dict of lists.
pres = gateway.const.Presentation pres = gateway.const.Presentation

View file

@ -24,7 +24,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the mysensors climate.""" """Setup the mysensors climate."""
if discovery_info is None: if discovery_info is None:
return 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: if float(gateway.protocol_version) < 1.5:
continue continue
pres = gateway.const.Presentation pres = gateway.const.Presentation

View file

@ -18,7 +18,12 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the mysensors platform for covers.""" """Setup the mysensors platform for covers."""
if discovery_info is None: if discovery_info is None:
return 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 pres = gateway.const.Presentation
set_req = gateway.const.SetReq set_req = gateway.const.SetReq
map_sv_types = { map_sv_types = {

View file

@ -31,7 +31,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if discovery_info is None: if discovery_info is None:
return 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 # Define the S_TYPES and V_TYPES that the platform should handle as
# states. Map them in a dict of lists. # states. Map them in a dict of lists.
pres = gateway.const.Presentation pres = gateway.const.Presentation

View file

@ -38,7 +38,7 @@ DEFAULT_VERSION = 1.4
DEFAULT_BAUD_RATE = 115200 DEFAULT_BAUD_RATE = 115200
DEFAULT_TCP_PORT = 5003 DEFAULT_TCP_PORT = 5003
DOMAIN = 'mysensors' DOMAIN = 'mysensors'
GATEWAYS = None MYSENSORS_GATEWAYS = 'mysensors_gateways'
MQTT_COMPONENT = 'mqtt' MQTT_COMPONENT = 'mqtt'
REQUIREMENTS = [ REQUIREMENTS = [
'https://github.com/theolind/pymysensors/archive/' 'https://github.com/theolind/pymysensors/archive/'
@ -132,9 +132,15 @@ def setup(hass, config):
return gateway 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 # Setup all devices from config
global GATEWAYS gateways = []
GATEWAYS = {}
conf_gateways = config[DOMAIN][CONF_GATEWAYS] conf_gateways = config[DOMAIN][CONF_GATEWAYS]
for index, gway in enumerate(conf_gateways): for index, gway in enumerate(conf_gateways):
@ -146,17 +152,19 @@ def setup(hass, config):
tcp_port = gway.get(CONF_TCP_PORT) tcp_port = gway.get(CONF_TCP_PORT)
in_prefix = gway.get(CONF_TOPIC_IN_PREFIX) in_prefix = gway.get(CONF_TOPIC_IN_PREFIX)
out_prefix = gway.get(CONF_TOPIC_OUT_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, device, persistence_file, baud_rate, tcp_port, in_prefix,
out_prefix) out_prefix)
if GATEWAYS[device] is None: if ready_gateway is not None:
GATEWAYS.pop(device) gateways.append(ready_gateway)
if not GATEWAYS: if not gateways:
_LOGGER.error( _LOGGER.error(
'No devices could be setup as gateways, check your configuration') 'No devices could be setup as gateways, check your configuration')
return False return False
hass.data[MYSENSORS_GATEWAYS] = gateways
for component in ['sensor', 'switch', 'light', 'binary_sensor', 'climate', for component in ['sensor', 'switch', 'light', 'binary_sensor', 'climate',
'cover']: 'cover']:
discovery.load_platform(hass, component, DOMAIN, {}, config) discovery.load_platform(hass, component, DOMAIN, {}, config)

View file

@ -20,7 +20,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if discovery_info is None: if discovery_info is None:
return 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 # Define the S_TYPES and V_TYPES that the platform should handle as
# states. Map them in a dict of lists. # states. Map them in a dict of lists.
pres = gateway.const.Presentation pres = gateway.const.Presentation

View file

@ -34,7 +34,11 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if discovery_info is None: if discovery_info is None:
return 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 # Define the S_TYPES and V_TYPES that the platform should handle as
# states. Map them in a dict of lists. # states. Map them in a dict of lists.
pres = gateway.const.Presentation pres = gateway.const.Presentation