From df32e6bf68984a834bff3b8a9868163968d39f22 Mon Sep 17 00:00:00 2001 From: Erwin Douna Date: Thu, 1 Oct 2026 09:44:57 +0200 Subject: [PATCH] Match SMA DHCP MAC updates on serial (#183845) --- homeassistant/components/sma/config_flow.py | 19 +++---------- tests/components/sma/test_config_flow.py | 30 +++++++++++++++++++++ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/homeassistant/components/sma/config_flow.py b/homeassistant/components/sma/config_flow.py index 8ed6e6938c54..7d610768bf47 100644 --- a/homeassistant/components/sma/config_flow.py +++ b/homeassistant/components/sma/config_flow.py @@ -280,25 +280,12 @@ class SmaConfigFlow(ConfigFlow, domain=DOMAIN): self._discovery_data[CONF_MAC], ) - existing_entries_with_host = [ - entry - for entry in self._async_current_entries(include_ignore=False) - if entry.data.get(CONF_HOST) == self._data[CONF_HOST] - and not entry.data.get(CONF_MAC) - ] - - # If we have an existing entry with the same host but no MAC address, - # we update the entry with the MAC address and reload it. - if existing_entries_with_host: - entry = existing_entries_with_host[0] - self.async_update_reload_and_abort( - entry, data_updates={CONF_MAC: self._data[CONF_MAC]} - ) - if not (match := HOSTNAME_SERIAL.match(discovery_info.hostname)): return self.async_abort(reason="not_supported") await self.async_set_unique_id(match.group(1)) - self._abort_if_unique_id_configured(updates={CONF_HOST: self._data[CONF_HOST]}) + self._abort_if_unique_id_configured( + updates={CONF_HOST: self._data[CONF_HOST], CONF_MAC: self._data[CONF_MAC]} + ) return await self.async_step_discovery_confirm() diff --git a/tests/components/sma/test_config_flow.py b/tests/components/sma/test_config_flow.py index 725e53705c9e..32e2a9a97ed8 100644 --- a/tests/components/sma/test_config_flow.py +++ b/tests/components/sma/test_config_flow.py @@ -242,6 +242,36 @@ async def test_dhcp_already_configured_duplicate( ) +@pytest.mark.parametrize( + ("hostname", "result_type"), + [ + pytest.param("SMA987654321", FlowResultType.FORM, id="other_serial"), + pytest.param("evcharger", FlowResultType.ABORT, id="not_sma"), + ], +) +async def test_dhcp_other_device_on_same_host( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + hostname: str, + result_type: FlowResultType, +) -> None: + """Test another device on the host of an entry doesn't change that entry.""" + mock_config_entry.add_to_hass(hass) + + result = await hass.config_entries.flow.async_init( + DOMAIN, + context={"source": SOURCE_DHCP}, + data=DhcpServiceInfo( + ip=mock_config_entry.data[CONF_HOST], + hostname=hostname, + macaddress="0015bb00ffff", + ), + ) + + assert result["type"] is result_type + assert CONF_MAC not in mock_config_entry.data + + @pytest.mark.parametrize( ("hostname", "unique_id"), [