-
Notifications
You must be signed in to change notification settings - Fork 62
feat(discovery): add callback support for device discovery #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
This introduces a `callback` parameter to the `GetSwitchbotDevices` constructor. It allows users to handle device advertisements immediately upon detection. Key changes: - Support both synchronous and asynchronous callback functions. - Add comprehensive tests for sync/async execution and error handling.
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
bdraco
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please see comments above
Only support sync functions following the feedback. Callers can manage their own tasks for async execution.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds a user-provided callback hook to device discovery so consumers can react to SwitchBot advertisements as they are received (instead of waiting for the full scan timeout).
Changes:
- Extend
GetSwitchbotDevices.__init__with an optionalcallbackparameter. - Invoke the callback from
detection_callbackand suppress/log callback exceptions. - Add pytest coverage for per-packet callback invocation and exception suppression.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
switchbot/discovery.py |
Adds optional callback and calls it on each parsed advertisement. |
tests/test_discovery_callback.py |
Introduces tests validating callback invocation and that callback exceptions don’t break discovery. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
bdraco
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @hnw
Co-authored-by: J. Nick Koston <nick@koston.org> Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Description
This PR adds a
callbackparameter to theGetSwitchbotDevicesclass. This allows consumers of the library to process device advertisements in real-time as they are discovered.Motivation / Real-world Use Case
I maintain an OSS project called switchbot-actions, which relies on
pySwitchbotfor device scanning. This tool triggers actions (webhooks, MQTT, shell commands) based on sensor updates. A common use case is turning on a light via webhook immediately when a motion sensor detects movement.Currently,
GetSwitchbotDevices.discover()waits for the fullscan_timeout(default 5s) to complete before returning results. This introduces an average latency of half the scan duration, causing a noticeable delay in automation.By adding a callback, applications can process the advertisement packet the moment it is received, enabling near real-time responsiveness without shortening the scan duration.
Changes
GetSwitchbotDevices.__init__to accept acallback.detection_callbackto invoke the user-provided callback.Technical Details
Backward Compatibility
callbackparameter is optional. Existing implementations usingdiscover()will function exactly as before without any changes.Example Usage
Tests
tests/test_discovery_callback.pycovering: