From d8d1e98d4eea6755f62b1d01ff53ae0b27a9f91c Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 12 Apr 2022 11:37:05 +0200 Subject: [PATCH] Fix unique id in SamsungTV config flow (#69899) * Fix unique id in SamsungTV config flow * coverage Co-authored-by: J. Nick Koston --- .../components/samsungtv/config_flow.py | 5 ++-- .../components/samsungtv/test_config_flow.py | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/samsungtv/config_flow.py b/homeassistant/components/samsungtv/config_flow.py index f37020cb029..adcec0e8b2b 100644 --- a/homeassistant/components/samsungtv/config_flow.py +++ b/homeassistant/components/samsungtv/config_flow.py @@ -363,9 +363,8 @@ class SamsungTVConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): if not entry: return None entry_kw_args: dict = {} - if ( - self.unique_id - and entry.unique_id is None + if self.unique_id and ( + entry.unique_id is None or (is_unique_match and self.unique_id != entry.unique_id) ): entry_kw_args["unique_id"] = self.unique_id diff --git a/tests/components/samsungtv/test_config_flow.py b/tests/components/samsungtv/test_config_flow.py index 19169c20dd3..d2a9d10caf2 100644 --- a/tests/components/samsungtv/test_config_flow.py +++ b/tests/components/samsungtv/test_config_flow.py @@ -1451,6 +1451,31 @@ async def test_update_missing_mac_unique_id_ssdp_location_added_from_ssdp( assert entry.unique_id == "be9554b9-c9fb-41f4-8920-22da015376a4" +@pytest.mark.usefixtures( + "remote", "remotews", "remoteencws_failing", "rest_api_failing" +) +async def test_update_zeroconf_discovery_preserved_unique_id( + hass: HomeAssistant, +) -> None: + """Test zeroconf discovery preserves unique id.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={**MOCK_OLD_ENTRY, CONF_MAC: "aa:bb:zz:ee:rr:oo"}, + unique_id="original", + ) + entry.add_to_hass(hass) + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_ZEROCONF}, + data=MOCK_ZEROCONF_DATA, + ) + await hass.async_block_till_done() + assert result["type"] == "abort" + assert result["reason"] == "not_supported" + assert entry.data[CONF_MAC] == "aa:bb:zz:ee:rr:oo" + assert entry.unique_id == "original" + + @pytest.mark.usefixtures("remotews", "rest_api", "remoteencws_failing") async def test_update_missing_mac_unique_id_added_ssdp_location_updated_from_ssdp( hass: HomeAssistant,