Library to control Switchbot IoT devices https://www.switch-bot.com/
python3 -m venv .venv
source .venv/bin/activate
pip install .Using the script scripts/get_encryption_key.py you can manually obtain locks encryption key.
Usage:
$ python3 scripts/get_encryption_key.py MAC USERNAME
Key ID: XX
Encryption key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXWhere MAC is MAC address of the lock and USERNAME is your SwitchBot account username, after that script will ask for your password.
If authentication succeeds then script should output your key id and encryption key.
Unlock:
importasynciofromswitchbot.discoveryimportGetSwitchbotDevicesfromswitchbot.devicesimportlockfromswitchbot.constimportSwitchbotModelBLE_MAC="XX:XX:XX:XX:XX:XX"# The MAC of your lockKEY_ID="XX"# The key-ID of your encryption-key for your lockENC_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"# The encryption-key with key-ID "XX"LOCK_MODEL=SwitchbotModel.LOCK_PRO# Your lock model (here we use the Lock-Pro)asyncdefmain():
wolock=awaitGetSwitchbotDevices().get_locks()
awaitlock.SwitchbotLock(
wolock[BLE_MAC].device, KEY_ID, ENCRYPTION_KEY, model=LOCK_MODEL
).unlock()
asyncio.run(main())Lock:
importasynciofromswitchbot.discoveryimportGetSwitchbotDevicesfromswitchbot.devicesimportlockfromswitchbot.constimportSwitchbotModelBLE_MAC="XX:XX:XX:XX:XX:XX"# The MAC of your lockKEY_ID="XX"# The key-ID of your encryption-key for your lockENC_KEY="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"# The encryption-key with key-ID "XX"LOCK_MODEL=SwitchbotModel.LOCK_PRO# Your lock model (here we use the Lock-Pro)asyncdefmain():
wolock=awaitGetSwitchbotDevices().get_locks()
awaitlock.SwitchbotLock(
wolock[BLE_MAC].device, KEY_ID, ENCRYPTION_KEY, model=LOCK_MODEL
).lock()
asyncio.run(main())importasynciofrompprintimportpprintfromswitchbotimportGetSwitchbotDevicesfromswitchbot.devicesimportcurtainasyncdefmain():
# get the BLE advertisement data of all switchbot devices in the vicinityadvertisement_data=awaitGetSwitchbotDevices().discover()
foriinadvertisement_data.values():
pprint(i)
print() # print newline so that devices' data is separated visually# find your device's BLE address by inspecting the above printed debug logs, example belowble_address="9915077C-C6FD-5FF6-27D3-45087898790B"# get the BLE device (via its address) and construct a curtain deviceble_device=advertisement_data[ble_address].devicecurtain_device=curtain.SwitchbotCurtain(ble_device, reverse_mode=False)
pprint(awaitcurtain_device.get_device_data())
pprint(awaitcurtain_device.get_basic_info())
awaitcurtain_device.set_position(100)
if__name__=="__main__":
asyncio.run(main())