From afa459e851c764bf81d39298af6b4e9810f87104 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 3 Aug 2026 11:38:24 +0200 Subject: [PATCH] Add test --- tests/components/knocki/test_config_flow.py | 55 ++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/tests/components/knocki/test_config_flow.py b/tests/components/knocki/test_config_flow.py index 8dbd394755d0..462c7a3bdcf6 100644 --- a/tests/components/knocki/test_config_flow.py +++ b/tests/components/knocki/test_config_flow.py @@ -17,9 +17,12 @@ from . import setup_integration from tests.common import MockConfigEntry +DEVICE_ID = "KNC1-W-00000214" +FORMATTED_MAC = "aa:bb:cc:dd:ee:ff" + DHCP_DISCOVERY = DhcpServiceInfo( ip="1.1.1.1", - hostname="KNC1-W-00000214", + hostname=DEVICE_ID, macaddress="aabbccddeeff", ) @@ -168,6 +171,56 @@ async def test_dhcp_mac( assert device.connections == {(dr.CONNECTION_NETWORK_MAC, "aa:bb:cc:dd:ee:ff")} +async def test_dhcp_mac_multiple_entries( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + device_registry: dr.DeviceRegistry, +) -> None: + """Test the DHCP discovery updates the mac on every matching device. + + Two config entries each own a device sharing the discovered identifier. The + migration to async_get_devices must update both devices; the deprecated + async_get_device would only have resolved to a single one. + """ + mock_config_entry.add_to_hass(hass) + second_config_entry = MockConfigEntry( + domain=DOMAIN, + title="Knocki 2", + unique_id="test-id-2", + data={CONF_TOKEN: "test-token"}, + ) + second_config_entry.add_to_hass(hass) + + device_1 = device_registry.async_get_or_create( + config_entry_id=mock_config_entry.entry_id, + identifiers={(DOMAIN, DEVICE_ID)}, + ) + device_2 = device_registry.async_get_or_create( + config_entry_id=second_config_entry.entry_id, + identifiers={(DOMAIN, DEVICE_ID)}, + ) + assert device_1.id != device_2.id + assert device_1.connections == set() + assert device_2.connections == set() + + result = await hass.config_entries.flow.async_init( + DOMAIN, context={"source": SOURCE_DHCP}, data=DHCP_DISCOVERY + ) + assert result["type"] is FlowResultType.ABORT + assert result["reason"] == "already_configured" + + device_1 = device_registry.async_get_device_by_identifier( + (DOMAIN, DEVICE_ID), mock_config_entry.entry_id + ) + device_2 = device_registry.async_get_device_by_identifier( + (DOMAIN, DEVICE_ID), second_config_entry.entry_id + ) + assert device_1 + assert device_2 + assert device_1.connections == {(dr.CONNECTION_NETWORK_MAC, FORMATTED_MAC)} + assert device_2.connections == {(dr.CONNECTION_NETWORK_MAC, FORMATTED_MAC)} + + async def test_dhcp_already_setup( hass: HomeAssistant, mock_knocki_client: AsyncMock,