Match SMA DHCP MAC updates on serial (#183845)

This commit is contained in:
Erwin Douna
2026-10-05 11:43:37 +00:00
committed by Franck Nijhof
parent 0bc55bc32a
commit df32e6bf68
2 changed files with 33 additions and 16 deletions
+3 -16
View File
@@ -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()
+30
View File
@@ -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"),
[