diff --git a/homeassistant/components/deconz/config_flow.py b/homeassistant/components/deconz/config_flow.py index 43c6cee9193..614d2378c88 100644 --- a/homeassistant/components/deconz/config_flow.py +++ b/homeassistant/components/deconz/config_flow.py @@ -147,41 +147,21 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): self.bridge_id = await async_get_bridge_id( session, **self.deconz_config ) - - for entry in self.hass.config_entries.async_entries(DOMAIN): - if self.bridge_id == entry.unique_id: - return self._update_entry( - entry, - host=self.deconz_config[CONF_HOST], - port=self.deconz_config[CONF_PORT], - api_key=self.deconz_config[CONF_API_KEY], - ) - await self.async_set_unique_id(self.bridge_id) + self._abort_if_unique_id_configured( + updates={ + CONF_HOST: self.deconz_config[CONF_HOST], + CONF_PORT: self.deconz_config[CONF_PORT], + CONF_API_KEY: self.deconz_config[CONF_API_KEY], + } + ) + except asyncio.TimeoutError: return self.async_abort(reason="no_bridges") return self.async_create_entry(title=self.bridge_id, data=self.deconz_config) - def _update_entry(self, entry, host, port, api_key=None): - """Update existing entry.""" - if ( - entry.data[CONF_HOST] == host - and entry.data[CONF_PORT] == port - and (api_key is None or entry.data[CONF_API_KEY] == api_key) - ): - return self.async_abort(reason="already_configured") - - entry.data[CONF_HOST] = host - entry.data[CONF_PORT] = port - - if api_key is not None: - entry.data[CONF_API_KEY] = api_key - - self.hass.config_entries.async_update_entry(entry) - return self.async_abort(reason="updated_instance") - async def async_step_ssdp(self, discovery_info): """Handle a discovered deCONZ bridge.""" if ( @@ -193,13 +173,14 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): self.bridge_id = normalize_bridge_id(discovery_info[ssdp.ATTR_UPNP_SERIAL]) parsed_url = urlparse(discovery_info[ssdp.ATTR_SSDP_LOCATION]) - for entry in self.hass.config_entries.async_entries(DOMAIN): - if self.bridge_id == entry.unique_id: - if entry.source == "hassio": - return self.async_abort(reason="already_configured") - return self._update_entry(entry, parsed_url.hostname, parsed_url.port) + entry = await self.async_set_unique_id(self.bridge_id) + if entry and entry.source == "hassio": + return self.async_abort(reason="already_configured") + + self._abort_if_unique_id_configured( + updates={CONF_HOST: parsed_url.hostname, CONF_PORT: parsed_url.port} + ) - await self.async_set_unique_id(self.bridge_id) # pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167 self.context["title_placeholders"] = {"host": parsed_url.hostname} @@ -216,17 +197,16 @@ class DeconzFlowHandler(config_entries.ConfigFlow, domain=DOMAIN): This flow is triggered by the discovery component. """ self.bridge_id = normalize_bridge_id(user_input[CONF_SERIAL]) - - for entry in self.hass.config_entries.async_entries(DOMAIN): - if self.bridge_id == entry.unique_id: - return self._update_entry( - entry, - user_input[CONF_HOST], - user_input[CONF_PORT], - user_input[CONF_API_KEY], - ) - await self.async_set_unique_id(self.bridge_id) + + self._abort_if_unique_id_configured( + updates={ + CONF_HOST: user_input[CONF_HOST], + CONF_PORT: user_input[CONF_PORT], + CONF_API_KEY: user_input[CONF_API_KEY], + } + ) + self._hassio_discovery = user_input return await self.async_step_hassio_confirm() diff --git a/tests/components/deconz/test_config_flow.py b/tests/components/deconz/test_config_flow.py index 92dd95fc0c6..d79f80b96b0 100644 --- a/tests/components/deconz/test_config_flow.py +++ b/tests/components/deconz/test_config_flow.py @@ -205,7 +205,7 @@ async def test_manual_configuration_update_configuration(hass, aioclient_mock): ) assert result["type"] == "abort" - assert result["reason"] == "updated_instance" + assert result["reason"] == "already_configured" assert gateway.config_entry.data[config_flow.CONF_HOST] == "2.3.4.5" @@ -382,7 +382,7 @@ async def test_ssdp_discovery_update_configuration(hass): ) assert result["type"] == "abort" - assert result["reason"] == "updated_instance" + assert result["reason"] == "already_configured" assert gateway.config_entry.data[config_flow.CONF_HOST] == "2.3.4.5" @@ -469,7 +469,7 @@ async def test_hassio_discovery_update_configuration(hass): ) assert result["type"] == "abort" - assert result["reason"] == "updated_instance" + assert result["reason"] == "already_configured" assert gateway.config_entry.data[config_flow.CONF_HOST] == "2.3.4.5" assert gateway.config_entry.data[config_flow.CONF_PORT] == 8080 assert gateway.config_entry.data[config_flow.CONF_API_KEY] == "updated"