From 8774f34271e6e6be4064532791b0e6c947d5412b Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Fri, 16 Sep 2022 17:42:44 +0200 Subject: [PATCH] Update Awair config entry on discovery (#78521) --- homeassistant/components/awair/config_flow.py | 5 ++- tests/components/awair/test_config_flow.py | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/awair/config_flow.py b/homeassistant/components/awair/config_flow.py index becf6ce46ff..99b1545e792 100644 --- a/homeassistant/components/awair/config_flow.py +++ b/homeassistant/components/awair/config_flow.py @@ -39,7 +39,10 @@ class AwairFlowHandler(ConfigFlow, domain=DOMAIN): if self._device is not None: await self.async_set_unique_id(self._device.mac_address) - self._abort_if_unique_id_configured(error="already_configured_device") + self._abort_if_unique_id_configured( + updates={CONF_HOST: self._device.device_addr}, + error="already_configured_device", + ) self.context.update( { "host": host, diff --git a/tests/components/awair/test_config_flow.py b/tests/components/awair/test_config_flow.py index 16fca099d8c..b939b62641a 100644 --- a/tests/components/awair/test_config_flow.py +++ b/tests/components/awair/test_config_flow.py @@ -1,4 +1,5 @@ """Define tests for the Awair config flow.""" +from typing import Any from unittest.mock import Mock, patch from aiohttp.client_exceptions import ClientConnectorError @@ -364,3 +365,37 @@ async def test_unsuccessful_create_zeroconf_entry(hass: HomeAssistant): ) assert result["type"] == data_entry_flow.FlowResultType.ABORT + + +async def test_zeroconf_discovery_update_configuration( + hass: HomeAssistant, local_devices: Any +) -> None: + """Test updating an existing Awair config entry with discovery info.""" + + config_entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_HOST: "127.0.0.1"}, + unique_id=LOCAL_UNIQUE_ID, + ) + config_entry.add_to_hass(hass) + + with patch( + "homeassistant.components.awair.async_setup_entry", + return_value=True, + ) as mock_setup_entry, patch( + "python_awair.AwairClient.query", side_effect=[local_devices] + ), patch( + "homeassistant.components.awair.async_setup_entry", + return_value=True, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_ZEROCONF}, + data=ZEROCONF_DISCOVERY, + ) + + assert result["type"] == data_entry_flow.FlowResultType.ABORT + assert result["reason"] == "already_configured_device" + + assert config_entry.data[CONF_HOST] == ZEROCONF_DISCOVERY.host + assert mock_setup_entry.call_count == 0