fix possible mac collision in enphase_envoy (#145549)

* fix possible mac collision in enphase_envoy

* remove redundant device registry async_get
This commit is contained in:
Arie Catsman 2025-06-10 16:25:26 +02:00 committed by GitHub
parent 0f27d0bf4a
commit 9b90ed04e5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 66 additions and 4 deletions

View File

@ -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)

View File

@ -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",
)
}