Fix ESPHome unnecessary probing on DHCP discovery (#149713)

This commit is contained in:
J. Nick Koston 2025-07-30 13:06:08 -10:00 committed by GitHub
parent 94dc2e2ea3
commit f9e7459901
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 3 deletions

View File

@ -316,10 +316,11 @@ class EsphomeFlowHandler(ConfigFlow, domain=DOMAIN):
# Don't call _fetch_device_info() for ignored entries # Don't call _fetch_device_info() for ignored entries
raise AbortFlow("already_configured") raise AbortFlow("already_configured")
configured_host: str | None = entry.data.get(CONF_HOST) configured_host: str | None = entry.data.get(CONF_HOST)
configured_port: int | None = entry.data.get(CONF_PORT) configured_port: int = entry.data.get(CONF_PORT, DEFAULT_PORT)
if configured_host == host and configured_port == port: # When port is None (from DHCP discovery), only compare hosts
if configured_host == host and (port is None or configured_port == port):
# Don't probe to verify the mac is correct since # Don't probe to verify the mac is correct since
# the host and port matches. # the host matches (and port matches if provided).
raise AbortFlow("already_configured") raise AbortFlow("already_configured")
configured_psk: str | None = entry.data.get(CONF_NOISE_PSK) configured_psk: str | None = entry.data.get(CONF_NOISE_PSK)
await self._fetch_device_info(host, port or configured_port, configured_psk) await self._fetch_device_info(host, port or configured_port, configured_psk)

View File

@ -2485,3 +2485,36 @@ async def test_reconfig_name_conflict_overwrite(
) )
is None is None
) )
@pytest.mark.usefixtures("mock_setup_entry")
async def test_discovery_dhcp_no_probe_same_host_port_none(
hass: HomeAssistant, mock_client: APIClient
) -> None:
"""Test dhcp discovery does not probe when host matches and port is None."""
entry = MockConfigEntry(
domain=DOMAIN,
data={CONF_HOST: "192.168.43.183", CONF_PORT: 6053, CONF_PASSWORD: ""},
unique_id="11:22:33:44:55:aa",
)
entry.add_to_hass(hass)
# DHCP discovery with same MAC and host (WiFi device)
service_info = DhcpServiceInfo(
ip="192.168.43.183",
hostname="test8266",
macaddress="11:22:33:44:55:aa", # Same MAC as configured
)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_DHCP}, data=service_info
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
# Verify device_info was NOT called (no probing)
mock_client.device_info.assert_not_called()
# Host should remain unchanged
assert entry.data[CONF_HOST] == "192.168.43.183"