OpenWonderLabs / node-switchbot

The node-switchbot is a Node.js module which allows you communicate with SwitchBot Devices over BLE or OpenAPI
https://openwonderlabs.github.io/node-switchbot/
ISC License
71 stars 25 forks source link

bot BLE state parsed backwards? #206

Closed grelca closed 10 months ago

grelca commented 11 months ago

Analysis

i forked https://github.com/OpenWonderLabs/homebridge-switchbot because i wanted to add (even if just for myself) a way to keep the homebridge bot state in sync even if i control it via the switchbot app and not homebridge. however, i noticed that the bot state read from node-switchbot is always opposite of what the bot's actual state is.

using @abandonware/noble directly, i found the following values for the second byte in the broadcast service data

since node-switchbot takes the value of the second bit of this byte for the bot's state, the bot state gets reported backwards.

i am posting an issue first rather than opening a PR in case this is possibly an issue with my bot? but i'm happy to open a PR if it's not just an issue with my bot.

i only have the one bot so i'm unable to compare this with the behavior of other bots, but the bot's on/off state is always correctly displayed in the switchbot app. and i don't have a hub so i am also unable to compare the BLE behavior with API behavior.

Expected Behavior

when a bot in turned on, its BLE state is parsed as true when a bot is turned off, its BLE state is parsed as false

Steps To Reproduce

  1. in the switchbot app, set up a switchbot bot in switch mode
  2. turn on the switch
  3. scan for it using node-switchbot
  4. service data is parsed with state: false
  5. turn off the switch
  6. scan for it using node-switchbot
  7. service data is parsed with state: true

Logs

from the basic homebridge-switchbot plugin, not my fork

when the bot is off:

[03/12/2023, 18:04:37] [SwitchBot] [DEBUG] Bot: Air Conditioner BLERefreshStatus
[03/12/2023, 18:04:37] [SwitchBot] [DEBUG] Bot: Air Conditioner BLE Address: ff:47:4d:2e:de:28
[03/12/2023, 18:04:37] [SwitchBot] [DEBUG] Bot: Air Conditioner Config BLE Address: ff:47:4d:2e:de:28, BLE Address Found: ff:47:4d:2e:de:28
[03/12/2023, 18:04:37] [SwitchBot] [DEBUG] Bot: Air Conditioner serviceData: {"model":"H","modelName":"WoHand","mode":true,"state":true,"battery":97}
[03/12/2023, 18:04:37] [SwitchBot] [DEBUG] Bot: Air Conditioner, model: H, modelName: WoHand, mode: true, state: true, battery: 97

when the bot is on:

[03/12/2023, 18:06:37] [SwitchBot] [DEBUG] Bot: Air Conditioner BLERefreshStatus
[03/12/2023, 18:06:37] [SwitchBot] [DEBUG] Bot: Air Conditioner BLE Address: ff:47:4d:2e:de:28
[03/12/2023, 18:06:37] [SwitchBot] [DEBUG] Bot: Air Conditioner Config BLE Address: ff:47:4d:2e:de:28, BLE Address Found: ff:47:4d:2e:de:28
[03/12/2023, 18:06:37] [SwitchBot] [DEBUG] Bot: Air Conditioner serviceData: {"model":"H","modelName":"WoHand","mode":true,"state":false,"battery":98}
[03/12/2023, 18:06:37] [SwitchBot] [DEBUG] Bot: Air Conditioner, model: H, modelName: WoHand, mode: true, state: false, battery: 98

Configuration

n/a

Environment

Additional Context

i have reset my bot with the physical reset button under the cover and the behavior did not change. i'm not sure what the most up-to-date firmware version is for the bot, but mine is running v6.3 and does not show a firmware update available in the app.

grelca commented 11 months ago

this (third party) repo looks like it agrees with my observation, and parses 0 as on and 1 as off. i think i will open a PR

https://github.com/carlsondev/python-switchbot-ble/blob/main/switchbot_api/bot_information.py#L130-L132

donavanbecker commented 11 months ago

@grelca what we can do is once you have a PR open then we can merge it into a beta of node-switchbot and then have that beta be in a beta of homebridge-switchbot and we can verify all is working as expected.

I just published a beta branch if you can base your PR on that. Branch beta-1.9.2

donavanbecker commented 10 months ago

Released v1.10.0