diff --git a/homeassistant/components/deconz/config_flow.py b/homeassistant/components/deconz/config_flow.py index 337ca73cd049..c1842efd5593 100644 --- a/homeassistant/components/deconz/config_flow.py +++ b/homeassistant/components/deconz/config_flow.py @@ -92,6 +92,11 @@ class DeconzFlowHandler(ConfigFlow, domain=DOMAIN): self.bridge_id = bridge["id"] self.host = bridge[CONF_HOST] self.port = bridge[CONF_PORT] + await self.async_set_unique_id(self.bridge_id) + self._abort_if_unique_id_configured( + updates={CONF_HOST: self.host, CONF_PORT: self.port}, + reload_on_update=False, + ) return await self.async_step_link() session = aiohttp_client.async_get_clientsession(self.hass) diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index 962f5fe31b8d..4e7c37d31b8c 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -76,7 +76,6 @@ async def test_flow_discovered_bridges( result["flow_id"], user_input={} ) - # pylint: disable-next=home-assistant-tests-config-flow-unique-id assert result["type"] is FlowResultType.CREATE_ENTRY assert result["title"] == BRIDGE_ID assert result["data"] == { @@ -84,6 +83,35 @@ async def test_flow_discovered_bridges( CONF_PORT: 80, CONF_API_KEY: API_KEY, } + assert result["result"].unique_id == BRIDGE_ID + + +async def test_flow_discovered_bridge_already_configured( + hass: HomeAssistant, + aioclient_mock: AiohttpClientMocker, + config_entry_setup: MockConfigEntry, +) -> None: + """Test that a discovered bridge updates an existing config entry.""" + aioclient_mock.get( + pydeconz.utils.URL_DISCOVER, + json=[{"id": BRIDGE_ID, "internalipaddress": "2.3.4.5", "internalport": 80}], + headers={"content-type": CONTENT_TYPE_JSON}, + ) + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_USER} + ) + + assert result["type"] is FlowResultType.FORM + assert result["step_id"] == "user" + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], user_input={CONF_HOST: "2.3.4.5"} + ) + + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + assert config_entry_setup.data[CONF_HOST] == "2.3.4.5" async def test_flow_manual_configuration_decision(