Skip to content

Expose DeviceStateMonitor sources as public seams and drop the PLR0904 grandfather - #2037

Merged
J. Nick Koston (bdraco) merged 2 commits into
mainfrom
monitor-public-seams
Jul 14, 2026
Merged

J. Nick Koston (bdraco) merged 2 commits into
mainfrom
monitor-public-seams

Conversation

@bdraco

Copy link
Copy Markdown
Member

What does this implement/fix?

DeviceStateMonitor carried a grandfathered # noqa: PLR0904 (26 public defs vs the cap of 20), which froze its public surface — so in-package siblings reached private internals directly: shared.py used monitor._mdns (has_live_ptr, resolve_and_claim, zeroconf) and the API reviver used monitor._ping (icmp_available, icmp_concurrency, ping_once) and the raw monitor._on_persisted_ip_invalidated callback.

This PR does the surface refactor and adds the sanctioned seams, with no behaviour change:

  • The five sources become public attributes — monitor.mdns, monitor.ping, monitor.importable, monitor.api_info, monitor.api_reviver. Per-source queries and nudges route through them; shared.py and api_reviver.py no longer touch _-prefixed monitor attributes.
  • 12 thin one-line delegators are deleted (zeroconf property, refresh_mdns, get_mdns_a_record_ttl_remaining, get_mdns_cache_info, get_cached_addresses, reconcile_from_mdns_cache, probe_device, revisit_importable, revisit_all_importables, get_importable_devices, request_version_reprobe, get_cached_dns_addresses); callers use the public source attributes (or monitor.state.dns_cache for the DNS-cache read).
  • One seam added: invalidate_persisted_ip(name, stale_ip) folds the _on_persisted_ip_invalidated is not None guard so the reviver calls it unconditionally. (The issue's other named seams already exist post-Revive stuck-offline devices from the persisted IP after identity verification #2014: ping.icmp_available, ping.ping_once, and shared.apply_ping_result as the None-guarded record_ping_rtt wrapper.)
  • The monitor keeps the genuinely cross-source core (lifecycle, precedence ledger, the apply_* dedupe family, probe_device_ping, probe_reachability) — 15 public defs, so the PLR0904 noqa is dropped with 5 methods of headroom.
  • tests/controllers/devices/conftest.py's RecordingStateMonitor mirrors the new surface with nested typed sub-fakes sharing the one calls list; call-tuple names are unchanged so existing assertions kept their meaning.

Reviewer notes — two reroutes change the method name, not just the path (the deleted delegators had renamed their targets):

  • monitor.reconcile_from_mdns_cache(name) → monitor.mdns.reconcile_from_cache(name)
  • monitor.request_version_reprobe(name) → monitor.api_info.request_reprobe(name)

revisit_importable and get_importable_devices had no production callers on the monitor (the DevicesController method of the same name reads controller.state.import_result); their behaviour is still covered by tests through monitor.importable.

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.

Copilot AI review requested due to automatic review settings July 14, 2026 00:46
@bdraco

Copy link
Copy Markdown
Member Author

ESPHome Bot (@esphbot) review

@github-actions github-actions Bot added the refactor Code refactor with no user-visible behaviour change label Jul 14, 2026
@esphbot

ESPHome Bot (esphbot) commented Jul 14, 2026 •

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

@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 (e7a20ea) to head (10d219d).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #2037      +/-   ##
==========================================
- Coverage   99.60%   99.60%   -0.01%     
==========================================
  Files         243      243              
  Lines       19739    19716      -23     
==========================================
- Hits        19661    19638      -23     
  Misses         78       78              
Flag Coverage Δ
py3.12 99.56% <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 Δ
...lder/controllers/_device_state_monitor/api_info.py 100.00% <100.00%> (ø)
...r/controllers/_device_state_monitor/api_reviver.py 100.00% <100.00%> (ø)
...er/controllers/_device_state_monitor/controller.py 98.37% <100.00%> (-0.18%) ⬇️
...er/controllers/_device_state_monitor/importable.py 98.13% <100.00%> (ø)
..._builder/controllers/_device_state_monitor/mdns.py 99.57% <100.00%> (ø)
..._builder/controllers/_device_state_monitor/ping.py 100.00% <100.00%> (ø)
...uilder/controllers/_device_state_monitor/shared.py 100.00% <100.00%> (ø)
...e_device_builder/controllers/devices/controller.py 99.73% <100.00%> (ø)
...evice_builder/controllers/devices/firmware_sync.py 100.00% <100.00%> (ø)
...home_device_builder/controllers/devices/helpers.py 99.35% <ø> (ø)
... and 3 more
🚀 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.

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 DeviceStateMonitor to expose its per-signal sources (mdns, ping, importable, api_info, api_reviver) as public seams, removing the need for sibling modules to reach into _-prefixed internals and allowing the PLR0904 grandfather to be dropped without changing runtime behavior.

Changes:

  • Promote the monitor’s internal sources to public attributes and reroute cross-module access through those seams.
  • Remove thin monitor-level delegators (e.g., mDNS refresh/cache reads, importable probing, version reprobe) and update production callers accordingly.
  • Update/reshape tests and fakes to mirror the new public surface (RecordingStateMonitor now provides nested typed source fakes).

Reviewed changes

Copilot reviewed 32 out of 32 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
tests/test_state_monitor_reachability.py Updates test wiring and call sites to use monitor.mdns/monitor.ping/monitor.importable.
tests/test_state_monitor_lifecycle.py Adjusts lifecycle tests to the new public source attributes.
tests/test_probe_device.py Switches probing tests to route through monitor.importable / monitor.mdns.
tests/test_probe_device_ping.py Updates ping-loop/task spawning assertions to use monitor.ping.
tests/test_ping_loop_pause.py Rewires ping pause/resume tests to the monitor.ping public seam.
tests/test_non_api_mdns_resolve.py Updates non-API resolve tests to use monitor.mdns._zeroconf.
tests/test_mdns_resolve_first.py Migrates sweep tests from monitor._mdns/delegators to monitor.mdns.
tests/test_mdns_name_match.py Updates mDNS start call to use monitor.mdns.start().
tests/test_mdns_http_txt.py Routes HTTP TXT mDNS tests through monitor.mdns.
tests/test_mdns_cache_reconcile.py Moves cache reconcile calls to monitor.mdns.reconcile_from_cache.
tests/test_importable_discovery.py Switches importable discovery tests to monitor.importable.
tests/test_dns_cache.py Updates ping-sweep calls and DNS-cache reads to monitor.state.dns_cache.
tests/test_dashboard_advertise.py Updates advertise tests to patch MdnsSource.zeroconf rather than a monitor facade.
tests/test_api_reviver.py Updates reviver tests to use monitor.ping and monitor.api_reviver.
tests/test_api_info_fallback.py Updates API-info tests to use monitor.api_info and monitor.mdns.reconcile_from_cache.
tests/controllers/firmware/test_refresh.py Updates reprobe assertions to state_monitor.api_info.request_reprobe.
tests/controllers/devices/test_subscribe_reachability.py Adjusts device controller tests to stub state_monitor.mdns.* and state_monitor.importable.*.
tests/controllers/devices/conftest.py Refactors RecordingStateMonitor to provide typed nested fakes for mdns/importable/api_info and state.dns_cache.
esphome_device_builder/controllers/devices/scan_change.py Routes REMOVED handling through state_monitor.importable.revisit_all_importables().
esphome_device_builder/controllers/devices/reachability.py Uses state_monitor.mdns for TTL reads and refresh calls.
esphome_device_builder/controllers/devices/importable.py Uses state_monitor.mdns.get_cached_addresses and state_monitor.importable.probe_device.
esphome_device_builder/controllers/devices/helpers.py Uses monitor.mdns.get_cached_addresses and monitor.state.dns_cache.get_cached_addresses.
esphome_device_builder/controllers/devices/firmware_sync.py Routes version reprobe to state_monitor.api_info.request_reprobe.
esphome_device_builder/controllers/devices/controller.py Connects reachability tracker and zeroconf access via state_monitor.mdns.
esphome_device_builder/controllers/_device_state_monitor/shared.py Switches shared helpers to use the new public source seams.
esphome_device_builder/controllers/_device_state_monitor/ping.py Uses monitor.mdns.get_cached_addresses within ping resolve fallback.
esphome_device_builder/controllers/_device_state_monitor/mdns.py Uses monitor.importable via the public seam during start/setup.
esphome_device_builder/controllers/_device_state_monitor/importable.py Routes probe/resolve paths through monitor.mdns via the public seam.
esphome_device_builder/controllers/_device_state_monitor/controller.py Promotes sources to public attributes, drops PLR0904, removes delegator methods, and adds invalidate_persisted_ip.
esphome_device_builder/controllers/_device_state_monitor/api_reviver.py Switches ICMP/ping access to monitor.ping and invalidation to monitor.invalidate_persisted_ip.
esphome_device_builder/controllers/_device_state_monitor/api_info.py Routes cache reconciliation through monitor.mdns.reconcile_from_cache.
CLAUDE.md Updates documentation reference from request_version_reprobe to api_info.request_reprobe.

Comment thread tests/test_dns_cache.py Resolved
Comment thread tests/test_api_info_fallback.py Resolved
@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 monitor-public-seams (10d219d) with main (e7a20ea)

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. ↩

@bdraco

Copy link
Copy Markdown
Member Author

ESPHome Bot (@esphbot) review

@esphbot

Copy link
Copy Markdown
Contributor

PR Review — Expose DeviceStateMonitor sources as public seams and drop the PLR0904 grandfather

Clean, behavior-preserving refactor — merge-ready.

  • All 12 production call sites that reached _mdns/_ping/_importable/_api_info on main are rerouted through the new public seams; no dangling private access left.
  • invalidate_persisted_ip folds the _on_persisted_ip_invalidated is not None guard exactly, so the reviver's unconditional call is semantically identical to the prior inline check.
  • Import hygiene is correct: MdnsCacheInfo and AsyncEsphomeZeroconf are dropped with the methods that used them, so F401 stays clean and PLR0904 can be un-grandfathered (15 public defs, 5 of headroom).
  • The two name-changing reroutes (reconcile_from_mdns_cache→mdns.reconcile_from_cache, request_version_reprobe→api_info.request_reprobe) are applied consistently across code, tests, and CLAUDE.md.
  • The Copilot nits about stale wording are already addressed in-diff: the test_dns_cache.py:260 comment was rewritten to reference state.dns_cache.get_cached_addresses, and the test_monitor_request_version_reprobe_forwards_to_api_info test was deleted outright — both comments are moot.

No behavior change, no security/error-handling/performance surface touched. Coverage moves with the surface (100% on the touched source modules).



Checklist

  • All production callers of deleted delegators rerouted
  • No unused imports left after method deletion
  • invalidate_persisted_ip preserves the None guard
  • Name-changing reroutes applied consistently in code/tests/docs
  • Test fake mirrors new public surface without losing assertions

Automated review by Kōan (Claude) HEAD=10d219d 21s

@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.

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

Copilot reviewed 32 out of 32 changed files in this pull request and generated no new comments.

@bdraco
J. Nick Koston (bdraco) merged commit 3b5c653 into main Jul 14, 2026
22 checks passed
@bdraco
J. Nick Koston (bdraco) deleted the monitor-public-seams branch July 14, 2026 01:05
@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.

Refactor DeviceStateMonitor's public surface and add sibling seams

3 participants