From eea9e26ef5025ffe7385dfba4e8e632cc87f60c0 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Mon, 31 Jan 2022 17:24:55 -0600 Subject: [PATCH] Fix guardian being rediscovered via dhcp (#65332) --- .../components/guardian/config_flow.py | 6 +++ tests/components/guardian/test_config_flow.py | 44 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/homeassistant/components/guardian/config_flow.py b/homeassistant/components/guardian/config_flow.py index ea4589ddd42..c027fe8bc20 100644 --- a/homeassistant/components/guardian/config_flow.py +++ b/homeassistant/components/guardian/config_flow.py @@ -68,6 +68,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self._abort_if_unique_id_configured( updates={CONF_IP_ADDRESS: self.discovery_info[CONF_IP_ADDRESS]} ) + self._async_abort_entries_match( + {CONF_IP_ADDRESS: self.discovery_info[CONF_IP_ADDRESS]} + ) else: self._abort_if_unique_id_configured() @@ -103,6 +106,9 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): CONF_IP_ADDRESS: discovery_info.ip, CONF_PORT: DEFAULT_PORT, } + await self._async_set_unique_id( + async_get_pin_from_uid(discovery_info.macaddress.replace(":", "").upper()) + ) return await self._async_handle_discovery() async def async_step_zeroconf( diff --git a/tests/components/guardian/test_config_flow.py b/tests/components/guardian/test_config_flow.py index b8d8a10752d..fc3157289e9 100644 --- a/tests/components/guardian/test_config_flow.py +++ b/tests/components/guardian/test_config_flow.py @@ -13,6 +13,8 @@ from homeassistant.components.guardian.config_flow import ( from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, SOURCE_ZEROCONF from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT +from tests.common import MockConfigEntry + async def test_duplicate_error(hass, config, config_entry, setup_guardian): """Test that errors are shown when duplicate entries are added.""" @@ -166,3 +168,45 @@ async def test_step_dhcp_already_in_progress(hass): ) assert result["type"] == "abort" assert result["reason"] == "already_in_progress" + + +async def test_step_dhcp_already_setup_match_mac(hass): + """Test we abort if the device is already setup with matching unique id and discovered via DHCP.""" + entry = MockConfigEntry( + domain=DOMAIN, data={CONF_IP_ADDRESS: "1.2.3.4"}, unique_id="guardian_ABCD" + ) + entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=dhcp.DhcpServiceInfo( + ip="192.168.1.100", + hostname="GVC1-ABCD.local.", + macaddress="aa:bb:cc:dd:ab:cd", + ), + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "already_configured" + + +async def test_step_dhcp_already_setup_match_ip(hass): + """Test we abort if the device is already setup with matching ip and discovered via DHCP.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={CONF_IP_ADDRESS: "192.168.1.100"}, + unique_id="guardian_0000", + ) + entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=dhcp.DhcpServiceInfo( + ip="192.168.1.100", + hostname="GVC1-ABCD.local.", + macaddress="aa:bb:cc:dd:ab:cd", + ), + ) + assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT + assert result["reason"] == "already_configured"