Skip to content

docs: add an example for re-announcing on interface changes#1803

Merged
bdraco merged 3 commits into
masterfrom
docs/interface-rescan-example
Jun 22, 2026
Merged

docs: add an example for re-announcing on interface changes#1803
bdraco merged 3 commits into
masterfrom
docs/interface-rescan-example

Conversation

@bdraco

@bdraco bdraco commented Jun 22, 2026

Copy link
Copy Markdown
Member

Summary

Adds a runnable example showing how to re-announce services when the host's network interfaces change, using async_update_interfaces() from #1797.

zeroconf does not poll for interface changes itself; detection is platform specific and is best driven from whatever signal a host already has. When none is available, a small periodic poller is enough: snapshot the addresses and reconcile only when they change. The example also shows the part worth getting right, cancelling the monitor task before closing the instance.

This is the alternative to a built-in monitor (#1798, closed); the polling loop is small and belongs to the consumer, while the reconcile API does the hard part.

Test plan

  • examples/async_interface_monitor.py imports and runs; pre-commit run clean (ruff, format, flake8, mypy)

@codspeed-hq

codspeed-hq Bot commented Jun 22, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 21 untouched benchmarks


Comparing docs/interface-rescan-example (71ffff3) with master (471feb4)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (4f3fa73) during the generation of this report, so 471feb4 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@bdraco
bdraco marked this pull request as ready for review June 22, 2026 17:54
@bluetoothbot

bluetoothbot commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

Previous review — superseded by a newer review below.

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

Blocking issues found.

  • Example is built on deprecated get_all_addresses / get_all_addresses_v6

@codecov

codecov Bot commented Jun 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.81%. Comparing base (471feb4) to head (71ffff3).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1803   +/-   ##
=======================================
  Coverage   99.81%   99.81%           
=======================================
  Files          33       33           
  Lines        3750     3750           
  Branches      533      533           
=======================================
  Hits         3743     3743           
  Misses          5        5           
  Partials        2        2           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdraco

bdraco commented Jun 22, 2026

Copy link
Copy Markdown
Member Author

All addressed in 71ffff3:

  • Important Fix ServiceInfo __repr__ #1 (deprecated API): good catch. Swapped get_all_addresses / get_all_addresses_v6 (both deprecated, warn every call) for ifaddr.get_adapters() directly, snapshotting (str(ip.ip), ip.network_prefix). No more DeprecationWarning per poll, and it won't break when those are removed.
  • Suggestion Fix ServiceInfo __repr__ #1 (goodbye truncation): now awaits the returned unregister broadcast task before async_close (await (await async_unregister_service(info))), matching the other async examples so the TTL-0 goodbye actually goes out.
  • Silent-failure Fix ServiceInfo __repr__ #1/Create API with underscores instead of camel case, deprecate camel case API #2 (monitor dies on first error): the reconcile is wrapped in try/except that logs and retries on the next tick, and 'previous' only advances on success so a transient failure is re-attempted rather than permanently dropping the change. Since the loop is the part this example markets, keeping it resilient is the point.

Verified the snapshot raises no DeprecationWarning under -W error.

@bluetoothbot

Copy link
Copy Markdown
Contributor

@bdraco: All addressed in 71ffff3:

Confirmed. ifaddr is a declared main dependency, so the snapshot is safe. except Exception (not BaseException) keeps cancellation working — CancelledError still propagates and stops the monitor. Resilience and goodbye-await both correct.

@bluetoothbot

Copy link
Copy Markdown
Contributor

PR Review — docs: add an example for re-announcing on interface changes

Solid, runnable example — the prior blocking issue is resolved and the lifecycle handling is the part worth getting right, and it's correct.

Strengths:

  • Snapshot now uses ifaddr.get_adapters() directly (a declared main dependency, ifaddr = ">=0.1.7"), eliminating the per-poll DeprecationWarning from get_all_addresses/get_all_addresses_v6.
  • Monitor cancellation before async_close() is ordered correctly, and except Exception (not BaseException) lets CancelledError propagate so the cancel actually stops the task.
  • Reconcile is wrapped in try/except that logs via _LOGGER.exception and only advances previous on success, so a transient failure is retried rather than silently dropping the change.
  • Goodbye broadcast is awaited (await (await async_unregister_service(info))) so TTL-0 records are actually sent, matching the other async examples.

No blocking issues. The loop = asyncio.get_event_loop() / KeyboardInterrupt shape mirrors async_registration.py and the other examples, so it stays consistent.



Checklist

  • Error handling: monitor logs and retries rather than swallowing silently
  • Cancellation propagates (except Exception, not BaseException)
  • Dependency available (ifaddr declared in pyproject)
  • API referenced exists (async_update_interfaces)
  • Consistent with existing example conventions

Automated review by Kōan (Claude) HEAD=71ffff3 1 min 16s

@bluetoothbot bluetoothbot 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
bdraco merged commit c690b78 into master Jun 22, 2026
38 checks passed
@bdraco
bdraco deleted the docs/interface-rescan-example branch June 22, 2026 18:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants