A Python API for SwitchBots that allows to control actions, settings and timers.
Tested on a Raspberry Pi (BlueZ 5.82) with Python 3.13 and a Switchbot running firmware 4.9 (earlier versions were tested on a Raspberry Pi 3 with Raspbian Buster and firmware 4.4 and 4.5).
pip install switchbotpyRequirements:
- Linux with BlueZ >= 5.55 (BLE goes through bluetoothd via bleak), no root privileges needed
- Python >= 3.10
The API is synchronous: every call runs its own asyncio event loop, so it must not be called from a running event loop (e.g. from within an async def).
The examples folder contains demonstrations how to scan for switchbots, press / switch the switchbot, read and set settings (battery, firmware, hold time, etc.), read and set timers, and set / change / remove the password.
Use the scanner to find all switchbots in the area:
from switchbotpy import Scanner
scanner = Scanner()
mac_addresses = scanner.scan()Use the mac address to create a bot instance providing methods to control the switchbots:
from switchbotpy import Bot
bot = Bot(bot_id=bot_id, mac=mac, name=name)
bot.encrypted(password) # optional (only required in case the bot has a password)
bot.press() # press the switchbot
settings = bot.get_settings() # get a dict with the current bot settings
bot.set_password(new_password) # set / change / remove the password on the bot
# all other options can be found in the example folderThis library was originally built on the results of my own reverse engineering, at a time when no official documentation of the BLE protocol existed, and this section used to list them. SwitchBot has since published official documentation of the BLE commands, which is the better source now: SwitchBotAPI-BLE, Bot.
Not covered by the official docs are password protected bots, so these are still the results of reverse engineering (no guarantee of correctness or completeness):
- Commands with password: set bit 4 of the command byte (e.g.
0x01→0x11) and insert the CRC32 of the password (4 bytes, big endian) right after it:0x57 0x11 <crc32>instead of0x57 0x01. Without the password the bot answers0x07(device is encrypted), with a wrong one0x09(password error). - Set / change / remove the password (from the reverse engineered
python-switchbot-ble library, setting a password is tested on firmware 4.9):
- set:
0x57 0x07 0x01 0x04 <crc32 new> - change:
0x57 0x17 <crc32 current> 0x01 0x04 <crc32 new> - remove:
0x57 0x17 <crc32 current>
- set:
- Nicolas Küchler - Initial work - nicolas-kuechler