Skip to content

Simplify the ping sweep after the sweep-base and public-seam refactors - #2046

Merged
J. Nick Koston (bdraco) merged 2 commits into
mainfrom
ping-sweep-simplify
Jul 14, 2026
Merged

J. Nick Koston (bdraco) merged 2 commits into
mainfrom
ping-sweep-simplify

Conversation

@bdraco

Copy link
Copy Markdown
Member

What does this implement/fix?

Cleanup pass over the ping sweep following the sweep-base adoption (#2035) and the public-seam rename (#2037). Behavior-free apart from log lines:

  • wake() moves onto SweepSource; it was base machinery stranded on PingSource after Rehome the sweep-loop base and adopt it in PingSource #2035, and ApiInfoSource.request_reprobe was re-spelling self._wake.set() inline.
  • The two direct monitor.apply(name, OFFLINE, "ping") calls (no-target selection, resolve-empty) now route through shared.apply_ping_result(monitor, name, None) like the probe path and the reviver, so the ping-sourced transition has one spelling.
  • New helpers.async_.log_gather_failures owns the gather triage (log failures, re-raise cancellations). _sweep's hand-rolled isinstance loop uses it, and the _ping_sweep gather, which previously discarded exceptions silently, now logs them. mdns.close_zeroconf's hand-rolled single-task drain becomes drain_tasks(log_exceptions=True).
  • shared.address_resolution_exhausted becomes shared.sweep_has_no_target(monitor, device), folding in the not runtime_state.ip_addresses conjunct both call sites (ping selection, reviver cohort gate) were adding by hand; the "keep in lockstep" contract now lives in one predicate. Checks run cheapest-first, so the zeroconf cache walk only pays off after a proven DNS failure.
  • icmp_available de-tri-states to a plain bool; its only consumer (the reviver's _prepare) never distinguished None from False.
  • Stale class docstring fixed (the loop lives on the base now) and ping_once moved into the public section beside the other cross-source seams.

The flicker log test needed its driver fixed: the cheap-first predicate no longer consults the DNS-failure cache once RAM addresses exist, so the test's flaky device now genuinely never resolves, making the bucket flicker real instead of an artifact of call counting.

Out-of-scope efficiency findings from the same review (the per-device has_live_ptr full-bucket scan, per-sweep fleet snapshot reuse, bounded ping workers) are tracked in a separate issue.

Related issue or feature (if applicable):

Types of changes

  • Bugfix (non-breaking change which fixes an issue) — bugfix
  • New feature (non-breaking change which adds functionality) — new-feature
  • Enhancement to an existing feature — enhancement
  • Breaking change (fix or feature that would cause existing functionality to not work as expected) — breaking-change
  • Refactor (no behaviour change) — refactor
  • Documentation only — docs
  • Maintenance / chore — maintenance
  • CI / workflow change — ci
  • Dependencies bump — dependencies

Frontend coordination

  • No frontend change needed
  • Companion frontend PR: esphome/device-builder-frontend#

Checklist

  • The code change is tested and works locally.
  • Pre-commit hooks pass (ruff, codespell, yaml/json/python checks).
  • Tests have been added or updated under tests/ where applicable.
  • components.index.json / definitions/components/*.json have not been hand-edited (regenerate via script/sync_components.py if a sync is needed).
  • Architecture-level changes are reflected in docs/ARCHITECTURE.md and/or docs/API.md.

@bdraco J. Nick Koston (bdraco) added the refactor Code refactor with no user-visible behaviour change label Jul 14, 2026
@bdraco

Copy link
Copy Markdown
Member Author

ESPHome Bot (@esphbot) review

@codspeed

codspeed Bot commented Jul 14, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 27 untouched benchmarks
⏩ 1 skipped benchmark1


Comparing ping-sweep-simplify (5d58c1b) with main (56f8407)

Open in CodSpeed

Footnotes

  1. 1 benchmark was skipped, so the baseline result was used instead. If it was deleted from the codebase, click here and archive it to remove it from the performance reports. ↩

@esphbot

ESPHome Bot (esphbot) commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors the device-state monitor’s ping sweep implementation after the SweepSource/base-loop and public-seam changes, aiming to keep behavior unchanged aside from improved logging and consolidated predicates.

Changes:

  • Moves wake() onto SweepSource and updates callers to use the shared wake mechanism.
  • Consolidates “no target” and “ping OFFLINE” spellings via shared.sweep_has_no_target() and shared.apply_ping_result().
  • Adds helpers.async_.log_gather_failures() and uses it to log gather(..., return_exceptions=True) failures consistently (including in the ping pass and mDNS pre-resolve), plus simplifies zeroconf shutdown draining.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/test_state_monitor_lifecycle.py Adjusts the flicker-driver to reflect the updated cheapest-first target predicate.
tests/test_api_reviver.py Updates the ICMP availability test to match the new boolean icmp_available contract.
esphome_device_builder/helpers/async_.py Adds log_gather_failures() helper to standardize gather-result triage/logging.
esphome_device_builder/controllers/_device_state_monitor/shared.py Replaces address_resolution_exhausted with sweep_has_no_target and folds in the RAM-addresses gate.
esphome_device_builder/controllers/_device_state_monitor/ping.py Uses log_gather_failures, routes OFFLINE transitions through apply_ping_result, and moves ping_once into the public section.
esphome_device_builder/controllers/_device_state_monitor/mdns.py Simplifies interface-monitor shutdown by using drain_tasks(log_exceptions=True).
esphome_device_builder/controllers/_device_state_monitor/api_reviver.py Updates cohort gating and docs to use sweep_has_no_target and boolean icmp_available.
esphome_device_builder/controllers/_device_state_monitor/api_info.py Uses self.wake() instead of directly setting _wake.
esphome_device_builder/controllers/_device_state_monitor/_sweep_source.py Adds SweepSource.wake() as shared base machinery for sweep sources.

@bdraco

Copy link
Copy Markdown
Member Author

ESPHome Bot (@esphbot) review

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking issues found.

@bdraco

Copy link
Copy Markdown
Member Author

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

PR Review — Simplify the ping sweep after the sweep-base and public-seam refactors

Clean, behavior-preserving refactor. Merge-ready.

Verified strengths:

  • sweep_has_no_target is provably boolean-equivalent to the old address_resolution_exhausted(...) AND not ip_addresses at both call sites (ping selection + reviver cohort gate); both reduce to F ∧ ¬L. The cheapest-first reorder (DNS-failure cache before the zeroconf cache walk) is a real efficiency win with identical results.

  • icmp_available de-tri-state is safe — _prepare always assigns privileged is not None (a bool), and the sole consumer (api_reviver._prepare) treated None/False identically. False init + not icmp_available matches the old is not True at every input.

  • log_gather_failures centralizes the raise-cancellation / log-exception triage; the _ping_sweep gather now surfaces per-device errors it previously swallowed. The Copilot format-string concern is fixed (warning("%s", message, ...)).

  • OFFLINE transitions now route through shared.apply_ping_result(monitor, name, None), unifying the ping-sourced spelling; wake() correctly rehomed onto SweepSource.

  • Flicker test fix makes zom.local genuinely never resolve, so the bucket flicker is real behavior rather than a call-counting artifact.

  • Only observable change: close_zeroconf crashed-task log level DEBUG → WARNING, disclosed in the description. Non-blocking.



Checklist

  • Refactor is behavior-preserving (boolean equivalence, apply_ping_result, icmp_available)
  • No swallowed errors / cancellation masking in gather triage
  • Test changes verify observable behavior, not source inspection
  • Log-level / side-effect changes disclosed

Automated review by Kōan (Claude) HEAD=5d58c1b 1 min 22s

@bdraco
J. Nick Koston (bdraco) enabled auto-merge (squash) July 14, 2026 02:14

@esphbot ESPHome Bot (esphbot) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No blocking issues found.

@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.60%. Comparing base (56f8407) to head (5d58c1b).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #2046   +/-   ##
=======================================
  Coverage   99.60%   99.60%           
=======================================
  Files         243      243           
  Lines       19718    19725    +7     
=======================================
+ Hits        19641    19648    +7     
  Misses         77       77           
Flag Coverage Δ
py3.12 99.57% <100.00%> (+<0.01%) ⬆️
py3.14 99.60% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...controllers/_device_state_monitor/_sweep_source.py 100.00% <100.00%> (ø)
...lder/controllers/_device_state_monitor/api_info.py 100.00% <100.00%> (ø)
...r/controllers/_device_state_monitor/api_reviver.py 100.00% <100.00%> (ø)
..._builder/controllers/_device_state_monitor/ping.py 100.00% <100.00%> (ø)
...uilder/controllers/_device_state_monitor/shared.py 100.00% <100.00%> (ø)
esphome_device_builder/helpers/async_.py 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bdraco
J. Nick Koston (bdraco) merged commit 4159b37 into main Jul 14, 2026
21 checks passed
@bdraco
J. Nick Koston (bdraco) deleted the ping-sweep-simplify branch July 14, 2026 02:21
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

refactor Code refactor with no user-visible behaviour change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants