docs: add an example for re-announcing on interface changes by bdraco · Pull Request #1803 · python-zeroconf/python-zeroconf · GitHub
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

@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

@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