Skip to content

Commit

Permalink
deCONZ - properly identify configured bridge (#24378)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane610 authored and balloob committed Jun 8, 2019
1 parent bfafe9c commit b68a796
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
22 changes: 13 additions & 9 deletions homeassistant/components/deconz/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

DECONZ_MANUFACTURERURL = 'http://www.dresden-elektronik.de'
CONF_SERIAL = 'serial'
ATTR_UUID = 'udn'


@callback
Expand Down Expand Up @@ -156,25 +157,28 @@ async def async_step_ssdp(self, discovery_info):
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)
Expand Down
13 changes: 10 additions & 3 deletions tests/components/deconz/test_config_flow.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"""Tests for deCONZ config flow."""
from unittest.mock import patch
from unittest.mock import Mock, patch

import asyncio

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

0 comments on commit b68a796

Please sign in to comment.