deCONZ - properly identify configured bridge (#24378)

This commit is contained in:
Robert Svensson 2019-06-08 08:43:18 +02:00 committed by GitHub
parent 4cb1d77783
commit 48276b041c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 12 deletions

View File

@ -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)

View File

@ -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'}
)