Switchbot

Just adding my attempts to make this work, low code mostly just configuration through UI so far. My goal would be to turn this into the start of a binding if it ends up working out.

I’m running OH4 on RP4.
The target device is the SwitchBot Curtain 3.

I followed this tutorial from oh3 which seemed to work out, adding mosquito service to the same raspberry pi running openhabian (hostname is just localhost), adding the MQTT binding and broker bridge through the UI, and then I ran TheengsGateway on demand to test it out.

I then ran MQTT-Explorer’s UI on my windows laptop and connected to it using the local network IP for my raspberry pi.

And this almost worked. I saw the events in MQTT-Explorer with "mfr": "Woan Technology (Shenzhen) Co., Ltd." in the json body. Which seems to be the actual device manufacturing company for SwitchBot devices. I later confirmed with the vendor distributed python-host scripts (linked in the thread’s original post) that the MAC addresses were the same ones which it detected.

However, it seems that Theengs might not be supporting the Curtain 3? Or else there’s something wrong with my board’s bluetooth service, as no events from the devices were properly decoded, and when I tried pasting the raw events to their webparser (click on “Web Parser” in the top right on their docs website), I got Error: Cannot read properties of undefined (reading 'properties'), so clearly not fully implemented or something.

The python-host script kept running into failures for me when running it on my raspberry pi, and using bluetoothctl to attempt manual connections kept failing as well:

I'd often see errors like this from the script
$ sudo python3 switchbot_py3.py --scan
Scanning for bluetooth low-energy devices
Discovering Switchbot services
PyGattLib ERROR: connect error: Function not implemented (38)
free(): double free detected in tcache 2
Aborted
or it would get futher and then error out
 $ sudo python3 switchbot_py3.py --scan
 Scanning for bluetooth low-energy devices
 Discovering Switchbot services
  *  Found Switchbot service on device <MACADDR> handle 16
 PyGattLib ERROR: connect error: Function not implemented (38)
 free(): double free detected in tcache 2
 Aborted
when I tried specifying the device and giving a command with the script, I'd get similarly abysmal results
 $ sudo python3 switchbot_py3.py --device <MACADDR> -c close
 Connected!
 Traceback (most recent call last):
   File "/home/openhabian/python-host/switchbot_py3.py", line 210, in <module>
     main()
   File "/home/openhabian/python-host/switchbot_py3.py", line 205, in main
     driver.run_command(opts.command)
   File "/home/openhabian/python-host/switchbot_py3.py", line 154, in run_command
     return req.write_by_handle(self.handles[command], self.commands[command])
 gattlib.GATTException: Characteristic value/descriptor operation failed: Attribute can't be written
using `bluetoothctl` while `TheensGateway` was not running, connecting would work, but pairing wouldn't
 $ bluetoothctl
 Agent registered
 [CH  G] Controller <Ctlr MACADDR> Pairable: yes
 [bluetooth]# connect <SwitchBot MACADDR>
 Device <SwitchBot MACADDR> not available
 [bluetooth]# agent on
 Agent is already registered
 [bluetooth]# default-agent
 Default agent request successful
 [bluetooth]# pairable on
 Changing pairable on succeeded
 [bluetooth]# discoverable on
 Changing discoverable on succeeded
 [CHG] Controller <Ctlr MACADDR> Discoverable: yes
 [bluetooth]# scan on
 Discovery started
 [CHG] Controller <Ctlr MACADDR> Discovering: yes
 [NEW] Device <SwitchBot MACADDR> <SwitchBot MACADDR>
 [bluetooth]# connect <SwitchBot MACADDR>
 Attempting to connect to <SwitchBot MACADDR>
 [CHG] Device <SwitchBot MACADDR> Connected: yes
 Connection successful
 [NEW] Primary Service (Handle 0xb584)
         /org/bluez/hci0/dev_<SwitchBot MACADDR>/service0001
         00001801-0000-1000-8000-00805f9b34fb
         Generic Attribute Profile
 [NEW] Characteristic (Handle 0x006c)
         /org/bluez/hci0/dev_<SwitchBot MACADDR>/service0001/char0002
         00002a05-0000-1000-8000-00805f9b34fb
         Service Changed
 [NEW] Descriptor (Handle 0x84fc)
         /org/bluez/hci0/dev_<SwitchBot MACADDR>/service0001/char0002/desc0004
         00002902-0000-1000-8000-00805f9b34fb
         Client Characteristic Configuration
 [NEW] Primary Service (Handle 0xc6e4)
         /org/bluez/hci0/dev_<SwitchBot MACADDR>/service000e
         cba20d00-224d-11e6-9fb8-0002a5d5c51b
         Vendor specific
 [NEW] Characteristic (Handle 0x006c)
         /org/bluez/hci0/dev_<SwitchBot MACADDR>/service000e/char000f
         cba20002-224d-11e6-9fb8-0002a5d5c51b
         Vendor specific
 [NEW] Characteristic (Handle 0x006c)
         /org/bluez/hci0/dev_<SwitchBot MACADDR>/service000e/char0011
         cba20003-224d-11e6-9fb8-0002a5d5c51b
         Vendor specific
 [NEW] Descriptor (Handle 0x84fc)
         /org/bluez/hci0/dev_<SwitchBot MACADDR>/service000e/char0011/desc0013
         00002902-0000-1000-8000-00805f9b34fb
         Client Characteristic Configuration
 [CHG] Device <SwitchBot MACADDR> UUIDs: 00001800-0000-1000-8000-00805f9b34fb
 [CHG] Device <SwitchBot MACADDR> UUIDs: 00001801-0000-1000-8000-00805f9b34fb
 [CHG] Device <SwitchBot MACADDR> UUIDs: cba20d00-224d-11e6-9fb8-0002a5d5c51b
 [CHG] Device <SwitchBot MACADDR> ServicesResolved: yes
 [CHG] Device <SwitchBot MACADDR> Name: WoCtn3
 [CHG] Device <SwitchBot MACADDR> Alias: WoCtn3
 [CHG] Device <SwitchBot MACADDR> RSSI: -77
 [CHG] Device <SwitchBot MACADDR> ServicesResolved: no
 [CHG] Device <SwitchBot MACADDR> Connected: no
 [bluetooth]# pair <SwitchBot MACADDR>
 Attempting to pair with <SwitchBot MACADDR>
 [CHG] Device <SwitchBot MACADDR> Connected: yes
 Failed to pair: org.bluez.Error.AuthenticationFailed
 [CHG] Device <SwitchBot MACADDR> Connected: no

So, I’ll likely try again with another RP or some other bluetooth device so I can confirm that the SwitchBot is not the problem.

If the problem is Theens decoding just not having an implementation for Curtain 3, then my options become quite limited, as C/C++ is not my strong suit at all, but I’ll probably give it a go if that turns out to be my only option. I’d have to make some sort of sense of the github documentation for the curtain’s BLE interface first though. I did confirm that the UUIDs match with that document, and they are still the same. Just need to figure out what the authentication failure might be for :sweat_smile: :grimacing:

Otherwise, maybe I’ll figure out an arduino/esp32-based solution.