feat: implement passcode management and cloud synchronization for SwitchBot Keypad#518
feat: implement passcode management and cloud synchronization for SwitchBot Keypad#518deece wants to merge 3 commits into
Conversation
81d7288 to
d10c932
Compare
|
|
bluetoothbot
left a comment
There was a problem hiding this comment.
Blocking issues found.
- MagicMock used in annotations but never imported — test module fails to import
- Cloud sync silently skipped on partial credentials; BLE write already committed if cloud call then fails
There was a problem hiding this comment.
Pull request overview
Adds full BLE passcode management and RTC clock synchronization for the classic SwitchBot Keypad (WoKeypad), with an optional SwitchBot Cloud “passcode list” synchronization path, plus advertisement parsing coverage for attempt_state.
Changes:
- Introduces
SwitchbotKeypaddevice implementation with add/modify/delete passcode APIs, passcode counts, and RTC sync. - Adds a cloud-sync option to
add_passwordthat posts passcode metadata to SwitchBot Cloud (functionID 4245). - Adds unit tests and an advertisement fixture for keypad battery and
attempt_state.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
switchbot/devices/keypad.py |
New Keypad device class implementing passcode + time sync APIs and optional cloud sync. |
switchbot/__init__.py |
Exports SwitchbotKeypad from the package public surface. |
tests/test_keypad.py |
Adds coverage for passcode ops, password counts, RTC sync, cloud sync payload shape, and adv parsing. |
tests/__init__.py |
Adds KEYPAD_INFO advertisement fixture used by keypad tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
599ce3f to
3910589
Compare
Codecov Report❌ Patch coverage is
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
3910589 to
0d95e5e
Compare
… Keypad This patch adds complete local and cloud passcode management and clock synchronization capabilities for the non-vision SwitchBot Keypad (WoKeypad). What Was Implemented: 1. Passcode Management: - `add_password`: Sends passcode bytes over BLE to register a new PIN code on the keypad. Parses response to retrieve the device-assigned index. - `modify_password`: Modifies an existing passcode by index. Overwrites values and configures active duration ranges. - `delete_password`: Instantly deletes a passcode from device memory by index. - `get_password_count`: Queries counts of stored PINs, NFC tags, fingerprints, and duress credentials. 2. Clock Synchronization (RTC): - `sync_time`: Synchronizes the internal device clock with millisecond precision (sending 8-byte big-endian milliseconds timestamp). Automatically scales second-level timestamps for backwards compatibility. - Note: Clock querying (`get_time`) was tested but found to return error 05 (unsupported) by the keypad hardware, indicating it is a write-only clock, so it has been dropped from the implementation. 3. SwitchBot Cloud Sync Option: - Added keyword-only parameters to `add_password`: `session`, `token`, `region`, `name`, and `creator`. - If provided, automatically triggers an HTTP POST request to API function 4245 to register the passcode in the SwitchBot Cloud database so it appears correctly in the official smartphone app. 4. Passive Advertisement Property Integration (from sblibs#488): - Exposes `attempt_state` property via `_get_adv_value` in `SwitchbotKeypad`. - Adds the missing `KEYPAD_INFO` test fixture to `tests/__init__.py`. Testing Methodology: 1. Automated Tests (tests/test_keypad.py): - Created full coverage suite for add/modify/delete password, counts, and sync_time. - Implemented `test_add_password_with_cloud_sync` mocking `api_request` to verify payload structures, regional routing, and authorization headers. - Added advertisement parsing unit tests for battery percentage and attempt state. - Verified that all unit tests pass cleanly and achieve 100% test coverage. 2. Manual/Hardware Verification: - Tested using test_hardware.py on a physical WoKeypad and Lock. - Verified that syncing clock with millisecond timestamps sets the correct RTC, allowing time-limited (temporary) passcodes to evaluate successfully offline and unlock the lock. Why Cloud Sync is Needed: - Keypad passcodes are stored and validated locally (allowing the Keypad to directly command the Lock offline without requiring internet connectivity). - However, for security reasons, the Keypad's BLE protocol is write-only for passcodes. There is no command to read back or list saved codes over BLE. - Since the SwitchBot app cannot query the Keypad to build its UI passcode list, it relies entirely on the SwitchBot Cloud database to track which codes exist. - If a passcode is added locally over BLE but not synced to the cloud, it will successfully operate the Lock offline, but will be completely invisible in the official mobile app. Integrating the cloud sync option ensures the app UI stays in sync with the keypad's physical storage. Co-authored-by: Alastair D'Silva <alastair@d-silva.org> Co-authored-by: Antigravity <antigravity@google.com>
0d95e5e to
a9f1048
Compare
|
❌ Permission denied. Only users with write access can trigger bot commands. |
PR Review — feat: implement passcode management and cloud synchronization for SwitchBot KeypadSolid, well-tested device implementation; a few partial-failure and input-validation gaps to tighten before merge. No blocking criticals. Note: the existing CHANGES_REQUESTED comments about a missing What's done well:
What needs attention:
🟡 Important
1. Orphan passcode when time-window write fails after add succeeds
|
bluetoothbot
left a comment
There was a problem hiding this comment.
Blocking issues found.
- Orphan passcode when time-window write fails after add succeeds
Implement passcode management and cloud synchronization for SwitchBot Keypad
This Pull Request adds complete local and cloud passcode management and clock
synchronization capabilities for the non-vision SwitchBot Keypad (WoKeypad).
It also integrates the passive
attempt_stateadvertisement property and tests proposed in PR #488 (by @italo-lombardi).Why Cloud Sync is Needed:
What Was Implemented:
add_password: Sends passcode bytes over BLE to register a new PIN code on the keypad. Parses response to retrieve the device-assigned index.modify_password: Modifies an existing passcode by index. Overwrites values and configures active duration ranges.delete_password: Instantly deletes a passcode from device memory by index.get_password_count: Queries counts of stored PINs, NFC tags, fingerprints, and duress credentials.sync_time: Synchronizes the internal device clock with millisecond precision (sending 8-byte big-endian milliseconds timestamp). Automatically scales second-level timestamps for backwards compatibility.get_time) was tested but found to return error 05 (unsupported) by the keypad hardware, indicating it is a write-only clock, so it has been dropped from the implementation.add_password:session,token,region,name, andcreator.attempt_stateproperty via_get_adv_valueinSwitchbotKeypad.KEYPAD_INFOtest fixture totests/__init__.py.Testing Methodology:
test_add_password_with_cloud_syncmockingapi_requestto verify payload structures, regional routing, and authorization headers.Co-authored-by: Alastair D'Silva alastair@d-silva.org
Co-authored-by: Antigravity antigravity@google.com