Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -209,11 +209,12 @@ def _is_due(self, device: Device) -> bool:
Report whether *device* needs an API probe, ignoring cooldown.

Due means: online, exposes a Native API, reachable by IP, and either
still missing a field, flagged for a forced re-probe (post-flash
version verification, which probes even when both fields are filled),
or deployed encrypted firmware still reading plaintext. Only the
latter two defer to mDNS ownership — the announce carries the
fields, so the re-probe is redundant there. The missing-field case
still missing a field, holding unverified identity
(``deployed_identity_live`` false), flagged for a forced re-probe
(post-flash version verification), or deployed encrypted firmware
still reading plaintext. Only the latter three defer to mDNS
ownership — the announce vouches for the identity and carries the
fields, so the probe is redundant there. The missing-field case
deliberately doesn't: ownership proves an announce resolved once, not
that its TXT payload was ever applied (#1910), and the sweep's cache
reconcile has already run, so reaching here means the cache can't fill
Expand All @@ -227,7 +228,11 @@ def _is_due(self, device: Device) -> bool:
and (
not (device.mac_address and runtime.deployed_version)
or (
(device.name in self._force_reprobe or _reads_stale_plaintext(device))
(
not runtime.deployed_identity_live
or device.name in self._force_reprobe
or _reads_stale_plaintext(device)
)
and monitor.priority_for(device.name) != ReachabilitySource.MDNS
)
)
Expand Down Expand Up @@ -295,12 +300,13 @@ async def _fetch(self, device: Device) -> None:
# is a real miss → cool the device down.
if apply_worker_info(monitor, device.name, info):
return
# A forced re-probe that connected (``info`` truthy) but changed
# nothing confirmed the existing version — a success, not a miss, so
# don't cool it down. The normal path still cools down here: it was
# due *because* a field was missing, so "nothing newly filled" is a
# real miss to retry later.
if forced and info:
# A probe that confirmed existing fields (forced re-probe, or a
# seeded device whose live identity just stamped and cleared ``_is_due``)
# succeeded and shouldn't cool down. Any remaining reason to be due
# (still-missing field, stale plaintext, mDNS-refused stamp) is a miss.
if info and (
forced or (device.runtime_state.deployed_identity_live and not self._is_due(device))
):
return
self._record_failure(device)

Expand Down Expand Up @@ -331,8 +337,8 @@ def _evaluate_systemic_health(self) -> None:
self._warned_systemic = True
_LOGGER.warning(
"Native API info fallback is failing for %d devices; MAC/version "
"may stay blank — check device API reachability, encryption keys, "
"and the api.port setting",
"may stay blank or unverified — check device API reachability, "
"encryption keys, and the api.port setting",
failing,
)

Expand Down
54 changes: 52 additions & 2 deletions tests/test_api_info_fallback.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def test_select_targets_skips_when_both_fields_present() -> None:
"""MDNS already supplied both → never connect."""
devices = [make_online_api_device(mac_address="94:C9:60:1F:8C:F1", deployed_version="2026.6.1")]
monitor, _ = make_state_monitor_with_callbacks(devices)
monitor.state.state_source["kitchen"] = ReachabilitySource.MDNS
assert monitor.api_info._select_targets() == []


Expand Down Expand Up @@ -124,6 +125,7 @@ def _stuck_plaintext_device(**overrides: Any) -> Device:
"api_encryption_active": "",
"expected_config_hash": "abcd1234",
"deployed_config_hash": "abcd1234",
"deployed_identity_live": True,
}
base.update(overrides)
return make_online_api_device(**base)
Expand Down Expand Up @@ -162,6 +164,44 @@ def test_select_targets_picks_when_online_via_ping_not_mdns() -> None:
assert [d.name for d in monitor.api_info._select_targets()] == ["kitchen"]


@pytest.mark.parametrize("owner", [ReachabilitySource.PING, ReachabilitySource.MQTT])
async def test_seeded_mdns_dark_device_probes_once_for_live_identity(
owner: ReachabilitySource,
) -> None:
"""A restarted ping/mqtt-owned API device probes once to stamp live identity."""
device = make_online_api_device(mac_address="94:C9:60:1F:8C:F1", deployed_version="2026.6.1")
monitor, _ = make_state_monitor_with_callbacks([device])
monitor.state.state_source["kitchen"] = owner
src = monitor.api_info
assert [d.name for d in src._select_targets()] == ["kitchen"]

src._run_worker = AsyncMock( # type: ignore[method-assign]
return_value={"mac_address": "94c9601f8cf1", "esphome_version": "2026.6.1"}
)
await src._fetch(device)

assert device.runtime_state.deployed_identity_live is True
assert "kitchen" not in src._cooldown
assert not src._is_due(device)
assert src._select_targets() == []


async def test_seeded_mdns_dark_device_cools_down_when_probe_returns_no_identity() -> None:
"""A probe returning no identity leaves a seeded device due and cools it down."""
device = make_online_api_device(mac_address="94:C9:60:1F:8C:F1", deployed_version="2026.6.1")
monitor, _ = make_state_monitor_with_callbacks([device])
monitor.state.state_source["kitchen"] = ReachabilitySource.PING
src = monitor.api_info

src._run_worker = AsyncMock(return_value={"noise": False}) # type: ignore[method-assign]
await src._fetch(device)

assert device.runtime_state.deployed_identity_live is False
assert "kitchen" in src._cooldown
assert src._is_due(device)
assert src._select_targets() == []


# ----------------------------------------------------------------------
# _candidate_addresses
# ----------------------------------------------------------------------
Expand Down Expand Up @@ -503,6 +543,7 @@ async def test_systemic_warning_rearms_after_recovery(monkeypatch: Any) -> None:
for device in devices:
device.mac_address = "94:C9:60:1F:8C:F1"
device.runtime_state.deployed_version = "2026.6.1"
monitor.state.state_source[device.name] = ReachabilitySource.MDNS
src._evaluate_systemic_health()
assert src._warned_systemic is False

Expand Down Expand Up @@ -704,6 +745,7 @@ async def test_sweep_skips_api_probe_when_cache_reconcile_fills_fields() -> None
"""A device the zeroconf cache repairs drops out before the API-connect stage."""
device = make_online_api_device()
monitor, _ = make_state_monitor_with_callbacks([device])
monitor.state.state_source["kitchen"] = ReachabilitySource.MDNS

def _fill(_name: str) -> None:
device.mac_address = "94:C9:60:1F:8C:F1"
Expand Down Expand Up @@ -769,6 +811,7 @@ async def test_sweep_reconciles_unknown_encryption_state_even_when_fields_full()
mac_address="94:C9:60:1F:8C:F1",
deployed_version="2026.6.4",
deployed_config_hash="abcd1234",
deployed_identity_live=True,
)
assert device.runtime_state.api_encryption_active is None
monitor, _ = make_state_monitor_with_callbacks([device])
Expand All @@ -786,7 +829,10 @@ async def test_sweep_reconciles_unknown_encryption_state_even_when_fields_full()
async def test_sweep_reconciles_missing_config_hash_even_when_not_api_due() -> None:
"""mac+version present but no config_hash → cache reconcile runs, API probe doesn't."""
device = make_online_api_device(
mac_address="94:C9:60:1F:8C:F1", deployed_version="2026.6.4", deployed_config_hash=""
mac_address="94:C9:60:1F:8C:F1",
deployed_version="2026.6.4",
deployed_config_hash="",
deployed_identity_live=True,
)
monitor, _ = make_state_monitor_with_callbacks([device])
reconciled: list[str] = []
Expand Down Expand Up @@ -1064,7 +1110,11 @@ def test_log_task_exit_logs_crash(caplog: Any) -> None:

def test_request_reprobe_makes_filled_device_due() -> None:
"""A forced re-probe overrides the mac+version guard that would skip the device."""
device = make_online_api_device(mac_address="94:C9:60:1F:8C:F1", deployed_version="2026.6.1")
device = make_online_api_device(
mac_address="94:C9:60:1F:8C:F1",
deployed_version="2026.6.1",
deployed_identity_live=True,
)
monitor, _ = make_state_monitor_with_callbacks([device])
src = monitor.api_info
assert src._select_targets() == [] # both fields present → normally skipped
Expand Down
4 changes: 2 additions & 2 deletions tests/test_mdns_cache_reconcile.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ async def test_sweep_heals_blank_device_from_cache_end_to_end() -> None:
"""The #1910 shape: blank record + populated cache → one sweep fills it, no API connect."""
device = make_online_api_device()
monitor, _callbacks = make_state_monitor_with_callbacks([device])
monitor.state.state_source["kitchen"] = ReachabilitySource.PING
monitor.state.state_source["kitchen"] = ReachabilitySource.MDNS
_seed_txt_cache(
monitor,
[_txt_record({"version": "2026.6.4", "config_hash": "abcd1234", "mac": "94c9601f8cf1"})],
Expand All @@ -208,7 +208,7 @@ async def test_sweep_heals_blank_device_from_cache_end_to_end() -> None:
assert device.runtime_state.deployed_config_hash == "abcd1234"
assert device.mac_address == "94:C9:60:1F:8C:F1"
assert device.runtime_state.state == DeviceState.ONLINE
assert monitor.state.state_source["kitchen"] == ReachabilitySource.PING
assert monitor.state.state_source["kitchen"] == ReachabilitySource.MDNS
# The esphomelib TXT never vouches for the _http._tcp identity.
assert device.runtime_state.deployed_identity_live is False
fetch.assert_not_called()
Expand Down
Loading