Repository navigation
fix(ble): read sensors from advertisements instead of connecting - #356
Merged
Merged
Conversation
Status over BLE was read by connecting to the device for every device type.
Meters and other sensors report their state in advertisements and often
refuse connections ("Unknown Connection Identifier (0x2)", "Command
Disallowed"), so reads failed, the BLE circuit breaker opened and values
stayed at 0 (homebridge-switchbot#1437). The reply from the connection was
also discarded: the status came from the cached advertisement anyway.
As in pySwitchbot, meters (Meter, Plus, Pro, Pro CO2, Indoor/Outdoor),
contact, motion, leak and water sensors are now read from their latest
advertisement. When it is older than 60 s, the device listens up to 10 s
for a new one, sharing a single short scan between devices; if none
arrives it returns the last advertisement, or fails over to the API when
there has never been one. Sensors are never connected to.
DeviceInfo gains bleServiceDataAt (when the advertisement was received).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TYWa2RXoWsaUTWh76oNv8s
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes homebridge-switchbot#1437: a Meter added over BLE shows temperature, humidity and battery stuck at 0.
Cause
getBLEStatus()connected to every device to read its status. Meters and other sensors report their state in BLE advertisements, and many refuse connections. The user's log shows the result:Unknown Connection Identifier (0x2)andCommand Disallowed (0xc)from noble;circuit opened - failure rate 1.00.The connection reply wasn't used anyway:
normalizeBLEStatusDatareturns the cached advertisement data when the reply is a Buffer.Fix
This follows pySwitchbot, where sensors are passive and status comes from the advertisement parsers.
WoSensorTHand its subclasses (Meter Plus, Meter Pro, Meter Pro CO2, Indoor/Outdoor),WoContact,WoPresence(motion/presence),WoLeakandWoWaterDetector. They are read from their latest advertisement and never connected to.SwitchBotsupplies this viaadvertisementSource; it runs one short scan shared by all waiting devices if none is running.getStatusWithFallbackfalls back to the API.DeviceInfo.bleServiceDataAtrecords when the advertisement was received.Non-sensor devices are unchanged.
Tests
test/devices/sensor-advertisement-status.test.tscovers:446 tests pass. Build and lint pass.
No breaking changes.
Generated by Claude Code