From 9b90ed04e5b3b76bab45233304938d08e5ee1944 Mon Sep 17 00:00:00 2001 From: Arie Catsman <120491684+catsmanac@users.noreply.github.com> Date: Tue, 10 Jun 2025 16:25:26 +0200 Subject: [PATCH] fix possible mac collision in enphase_envoy (#145549) * fix possible mac collision in enphase_envoy * remove redundant device registry async_get --- .../components/enphase_envoy/coordinator.py | 12 +++- tests/components/enphase_envoy/test_init.py | 58 ++++++++++++++++++- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/enphase_envoy/coordinator.py b/homeassistant/components/enphase_envoy/coordinator.py index 40c690b29ec..cfff0777af5 100644 --- a/homeassistant/components/enphase_envoy/coordinator.py +++ b/homeassistant/components/enphase_envoy/coordinator.py @@ -180,9 +180,15 @@ class EnphaseUpdateCoordinator(DataUpdateCoordinator[dict[str, Any]]): ) return - device_registry.async_update_device( - device_id=envoy_device.id, - new_connections={connection}, + device_registry.async_get_or_create( + config_entry_id=self.config_entry.entry_id, + identifiers={ + ( + DOMAIN, + self.envoy_serial_number, + ) + }, + connections={connection}, ) _LOGGER.debug("added connection: %s to %s", connection, self.name) diff --git a/tests/components/enphase_envoy/test_init.py b/tests/components/enphase_envoy/test_init.py index ef071b421fe..560d0719424 100644 --- a/tests/components/enphase_envoy/test_init.py +++ b/tests/components/enphase_envoy/test_init.py @@ -510,7 +510,6 @@ async def test_coordinator_interface_information_no_device( ) # update device to force no device found in mac verification - device_registry = dr.async_get(hass) envoy_device = device_registry.async_get_device( identifiers={ ( @@ -531,3 +530,60 @@ async def test_coordinator_interface_information_no_device( # verify no device found message in log assert "No envoy device found in device registry" in caplog.text + + +@respx.mock +async def test_coordinator_interface_information_mac_also_in_other_device( + hass: HomeAssistant, + config_entry: MockConfigEntry, + mock_envoy: AsyncMock, + freezer: FrozenDateTimeFactory, + caplog: pytest.LogCaptureFixture, + device_registry: dr.DeviceRegistry, +) -> None: + """Test coordinator interface mac verification with MAC also in other existing device.""" + await setup_integration(hass, config_entry) + + caplog.set_level(logging.DEBUG) + logging.getLogger("homeassistant.components.enphase_envoy.coordinator").setLevel( + logging.DEBUG + ) + + # add existing device with MAC and sparsely populated i.e. unifi that found envoy + other_config_entry = MockConfigEntry(domain="test", data={}) + other_config_entry.add_to_hass(hass) + device_registry.async_get_or_create( + config_entry_id=other_config_entry.entry_id, + connections={(dr.CONNECTION_NETWORK_MAC, "00:11:22:33:44:55")}, + manufacturer="Enphase Energy", + ) + + envoy_device = device_registry.async_get_device( + identifiers={ + ( + DOMAIN, + mock_envoy.serial_number, + ) + } + ) + assert envoy_device + + # move time forward so interface information is fetched + freezer.tick(MAC_VERIFICATION_DELAY) + async_fire_time_changed(hass) + await hass.async_block_till_done(wait_background_tasks=True) + + # verify mac was added + assert "added connection: ('mac', '00:11:22:33:44:55') to Envoy 1234" in caplog.text + + # verify connection is now in envoy device + envoy_device_refetched = device_registry.async_get(envoy_device.id) + assert envoy_device_refetched + assert envoy_device_refetched.name == "Envoy 1234" + assert envoy_device_refetched.serial_number == "1234" + assert envoy_device_refetched.connections == { + ( + dr.CONNECTION_NETWORK_MAC, + "00:11:22:33:44:55", + ) + }