feat(lock): add Quick Key support for Lock Ultra#515
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 12 files with indirect coverage changes 🚀 New features to boost your workflow:
|
0a0cd06 to
ff04a06
Compare
Expose the Lock Ultra "Quick Key" setting. All three of its settings live in a single config byte: get_quick_key() reads and parses it, set_quick_key() does a masked write of any subset of enabled / single-vs-double press / function (Lock & Unlock, Unlock Only, Lock Only). Adds a QuickKeyFunction enum and unit tests. Scoped to LOCK_ULTRA (other lock models are untested). Tested on a SwitchBot Lock Ultra (firmware V2.6). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
ff04a06 to
1f1e33f
Compare
There was a problem hiding this comment.
Pull request overview
Adds Lock Ultra support for reading and updating the “Quick Key” configuration over BLE, including a new enum to represent the 2-bit function field and corresponding unit tests.
Changes:
- Introduces
QuickKeyFunctionenum for the Quick Key function field. - Adds
SwitchbotLock.get_quick_key()andSwitchbotLock.set_quick_key()(masked write) for Lock Ultra. - Adds unit tests covering read, write (single/multi-field), no-op guard, and unsupported-model guard.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
switchbot/devices/lock.py |
Adds Quick Key BLE command constants and get/set APIs with bitmask parsing/verification. |
switchbot/const/lock.py |
Defines QuickKeyFunction enum used by the new Quick Key APIs. |
tests/test_lock.py |
Adds unit tests validating Quick Key read/parse behavior and masked-write command generation. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @staticmethod | ||
| def _parse_quick_key(cfg: int) -> dict[str, Any]: | ||
| """Parse the Quick Key config byte.""" | ||
| return { | ||
| "enabled": bool(cfg & QUICK_KEY_ENABLED_BIT), | ||
| "double_press": bool(cfg & QUICK_KEY_DOUBLE_PRESS_BIT), | ||
| "function": QuickKeyFunction(cfg & QUICK_KEY_FUNCTION_MASK), | ||
| } |
PR Review — feat(lock): add Quick Key support for Lock UltraClean, well-tested Quick Key feature for Lock Ultra; one defensive-parsing gap worth fixing before merge. Strengths:
Needs attention:
🟡 Important
1. Undefined 4th function value crashes get_quick_key with ValueError
|
bluetoothbot
left a comment
There was a problem hiding this comment.
Blocking issues found.
- Undefined 4th function value crashes get_quick_key with ValueError
Address PR review feedback: - get_quick_key() no longer raises on an undefined function value. The 2-bit function field has 4 possible values but only 3 are defined, so a 0b11 reading (firmware variant or corrupted BLE byte) now logs and returns None, matching the return-None-on-malformed contract that the other parse paths already honor. - Export QuickKeyFunction from switchbot/__init__.py and the const package alongside LockStatus, so callers of set_quick_key(function=...) can import it from the package root. - Add a test for the undefined-function-value path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Thanks for the review. Both points are addressed in 08a5feb:
Full test suite passes (1222) and ruff is clean. |
The 2-bit function field has 4 possible values but only 3 are defined. A firmware variant or a corrupted BLE byte reporting the undefined 4th (0b11) now logs and surfaces the function as unknown, keeping the well-defined enable and trigger bits, instead of passing a stray value to the select. Mirrors the same fix in the upstream pySwitchbot PR (sblibs/pySwitchbot#515). Bumps version to 0.3.1. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Summary
Adds support for the Quick Key setting on the SwitchBot Lock Ultra. All three of its sub-settings live in a single config byte on the lock, read/written over BLE:
SwitchbotLock.get_quick_key()— reads + parses the byte into{"enabled": bool, "double_press": bool, "function": QuickKeyFunction}.SwitchbotLock.set_quick_key(*, enabled=None, double_press=None, function=None)— a masked write that changes only the fields you pass (the rest keep their current value) and confirms the lock echoed the requested bits.QuickKeyFunctionenum (LOCK_AND_UNLOCK/UNLOCK_ONLY/LOCK_ONLY).Scoped to
LOCK_ULTRA; other lock models raiseSwitchbotOperationError(the command set is untested there).Protocol
CFG low nibble: bit 3 = enabled, bit 2 = double-press, bits 1-0 = function.
Testing
tests/test_lock.py(get, masked single + multi-field set, no-op guard, unsupported-model guard). The fulltests/test_lock.pypasses (203 tests).Note
As an interim solution I've published a Home Assistant custom integration that uses this protocol (via the existing connection) for anyone who needs it before this merges: https://github.com/mayerwin/ha-switchbot-lock-quick-key-support — it can be retired once this lands.