diff --git a/homeassistant/components/deconz/config_flow.py b/homeassistant/components/deconz/config_flow.py index cf172ad7991..ea93cc590e2 100644 --- a/homeassistant/components/deconz/config_flow.py +++ b/homeassistant/components/deconz/config_flow.py @@ -18,6 +18,7 @@ from .const import CONF_BRIDGEID, DEFAULT_PORT, DOMAIN DECONZ_MANUFACTURERURL = 'http://www.dresden-elektronik.de' CONF_SERIAL = 'serial' +ATTR_UUID = 'udn' @callback @@ -156,25 +157,28 @@ class DeconzFlowHandler(config_entries.ConfigFlow): if discovery_info[ATTR_MANUFACTURERURL] != DECONZ_MANUFACTURERURL: return self.async_abort(reason='not_deconz_bridge') - bridgeid = discovery_info[ATTR_SERIAL] - gateway_entries = configured_gateways(self.hass) + uuid = discovery_info[ATTR_UUID].replace('uuid:', '') + gateways = { + gateway.api.config.uuid: gateway + for gateway in self.hass.data.get(DOMAIN, {}).values() + } - if bridgeid in gateway_entries: - entry = gateway_entries[bridgeid] + if uuid in gateways: + entry = gateways[uuid].config_entry await self._update_entry(entry, discovery_info[CONF_HOST]) return self.async_abort(reason='updated_instance') - # pylint: disable=unsupported-assignment-operation - self.context[ATTR_SERIAL] = bridgeid - - if any(bridgeid == flow['context'][ATTR_SERIAL] + bridgeid = discovery_info[ATTR_SERIAL] + if any(bridgeid == flow['context'][CONF_BRIDGEID] for flow in self._async_in_progress()): return self.async_abort(reason='already_in_progress') + # pylint: disable=unsupported-assignment-operation + self.context[CONF_BRIDGEID] = bridgeid + deconz_config = { CONF_HOST: discovery_info[CONF_HOST], CONF_PORT: discovery_info[CONF_PORT], - CONF_BRIDGEID: bridgeid } return await self.async_step_import(deconz_config) diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index 2b9f2c013b0..ac22c964151 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -1,5 +1,5 @@ """Tests for deCONZ config flow.""" -from unittest.mock import patch +from unittest.mock import Mock, patch import asyncio @@ -177,7 +177,8 @@ async def test_bridge_ssdp_discovery(hass): config_flow.CONF_PORT: 80, config_flow.ATTR_SERIAL: 'id', config_flow.ATTR_MANUFACTURERURL: - config_flow.DECONZ_MANUFACTURERURL + config_flow.DECONZ_MANUFACTURERURL, + config_flow.ATTR_UUID: 'uuid:1234' }, context={'source': 'ssdp'} ) @@ -207,13 +208,19 @@ async def test_bridge_discovery_update_existing_entry(hass): }) entry.add_to_hass(hass) + gateway = Mock() + gateway.config_entry = entry + gateway.api.config.uuid = '1234' + hass.data[config_flow.DOMAIN] = {'id': gateway} + result = await hass.config_entries.flow.async_init( config_flow.DOMAIN, data={ config_flow.CONF_HOST: 'mock-deconz', config_flow.ATTR_SERIAL: 'id', config_flow.ATTR_MANUFACTURERURL: - config_flow.DECONZ_MANUFACTURERURL + config_flow.DECONZ_MANUFACTURERURL, + config_flow.ATTR_UUID: 'uuid:1234' }, context={'source': 'ssdp'} )