From 120c76524dea0e3784d9142a9a445b764b6253da Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 26 Aug 2022 02:33:35 -0500 Subject: [PATCH] Fix incorrect key update for Gen2 locks with yalexs_ble (#77335) --- .../components/yalexs_ble/config_flow.py | 5 +- .../components/yalexs_ble/test_config_flow.py | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/yalexs_ble/config_flow.py b/homeassistant/components/yalexs_ble/config_flow.py index c7213eefbe9d..5fee6f62848e 100644 --- a/homeassistant/components/yalexs_ble/config_flow.py +++ b/homeassistant/components/yalexs_ble/config_flow.py @@ -98,7 +98,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): new_data = {CONF_KEY: lock_cfg.key, CONF_SLOT: lock_cfg.slot} self._abort_if_unique_id_configured(updates=new_data) for entry in self._async_current_entries(): - if entry.data.get(CONF_LOCAL_NAME) == lock_cfg.local_name: + if ( + local_name_is_unique(lock_cfg.local_name) + and entry.data.get(CONF_LOCAL_NAME) == lock_cfg.local_name + ): if hass.config_entries.async_update_entry( entry, data={**entry.data, **new_data} ): diff --git a/tests/components/yalexs_ble/test_config_flow.py b/tests/components/yalexs_ble/test_config_flow.py index 6ea1b4e8a638..64a4e93eae28 100644 --- a/tests/components/yalexs_ble/test_config_flow.py +++ b/tests/components/yalexs_ble/test_config_flow.py @@ -621,6 +621,58 @@ async def test_integration_discovery_updates_key_without_unique_local_name( assert entry.data[CONF_SLOT] == 66 +async def test_integration_discovery_updates_key_duplicate_local_name( + hass: HomeAssistant, +) -> None: + """Test integration discovery updates the key with duplicate local names.""" + entry = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_LOCAL_NAME: "Aug", + CONF_ADDRESS: OLD_FIRMWARE_LOCK_DISCOVERY_INFO.address, + CONF_KEY: "5fd51b8621c6a139eaffbedcb846b60f", + CONF_SLOT: 11, + }, + unique_id=OLD_FIRMWARE_LOCK_DISCOVERY_INFO.address, + ) + entry.add_to_hass(hass) + entry2 = MockConfigEntry( + domain=DOMAIN, + data={ + CONF_LOCAL_NAME: "Aug", + CONF_ADDRESS: "CC:DD:CC:DD:CC:DD", + CONF_KEY: "5fd51b8621c6a139eaffbedcb846b60f", + CONF_SLOT: 11, + }, + unique_id="CC:DD:CC:DD:CC:DD", + ) + entry2.add_to_hass(hass) + + with patch( + "homeassistant.components.yalexs_ble.util.async_process_advertisements", + return_value=LOCK_DISCOVERY_INFO_UUID_ADDRESS, + ): + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY}, + data={ + "name": "Front Door", + "address": OLD_FIRMWARE_LOCK_DISCOVERY_INFO.address, + "key": "2fd51b8621c6a139eaffbedcb846b60f", + "slot": 66, + "serial": "M1XXX012LU", + }, + ) + await hass.async_block_till_done() + assert result["type"] == FlowResultType.ABORT + assert result["reason"] == "already_configured" + assert entry.data[CONF_KEY] == "2fd51b8621c6a139eaffbedcb846b60f" + assert entry.data[CONF_SLOT] == 66 + + assert entry2.data[CONF_KEY] == "5fd51b8621c6a139eaffbedcb846b60f" + assert entry2.data[CONF_SLOT] == 11 + + async def test_integration_discovery_takes_precedence_over_bluetooth_uuid_address( hass: HomeAssistant, ) -> None: