Skip to content

Commit 9ff4f26

Browse files
mayerwinclaude
andcommitted
Harden Quick Key parse against an undefined function value
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>
1 parent e778258 commit 9ff4f26

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

custom_components/switchbot_quickkey/coordinator.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
CMD_READ,
2525
CONFIRM_READ_DEADLINE_S,
2626
DOMAIN,
27+
FUNCTION_LABELS,
2728
MASK_FUNCTION,
2829
READ_DEADLINE_S,
2930
RETRY_BACKOFF_S,
@@ -90,11 +91,21 @@ async def write_config(dev: SwitchbotLock, mask: int, value: int,
9091
def parse_config(cfg: int | None) -> dict:
9192
if cfg is None:
9293
return {"raw": None, "enabled": None, "double_press": None, "function": None}
94+
func = cfg & MASK_FUNCTION
95+
if func not in FUNCTION_LABELS:
96+
# The 2-bit function field has 4 possible values but only 3 are defined.
97+
# A firmware variant or a corrupted BLE byte could report the undefined 4th
98+
# (0b11); keep the well-defined enable/trigger bits and surface the function
99+
# as unknown rather than guessing or passing a stray value to the select.
100+
_LOGGER.warning(
101+
"Unknown Quick Key function value: 0x%02x (config byte 0x%02x)", func, cfg
102+
)
103+
func = None
93104
return {
94105
"raw": cfg,
95106
"enabled": bool(cfg & BIT_ENABLE),
96107
"double_press": bool(cfg & BIT_DOUBLE),
97-
"function": cfg & MASK_FUNCTION,
108+
"function": func,
98109
}
99110

100111

custom_components/switchbot_quickkey/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"domain": "switchbot_quickkey",
33
"name": "SwitchBot Lock Quick Key (interim)",
4-
"version": "0.3.0",
4+
"version": "0.3.1",
55
"config_flow": true,
66
"documentation": "https://github.com/mayerwin/ha-switchbot-lock-quick-key-support",
77
"issue_tracker": "https://github.com/mayerwin/ha-switchbot-lock-quick-key-support/issues",

0 commit comments

Comments
 (0)