Last active
June 20, 2022 04:49
-
-
Save anyakichi/f9308ea6728f54f88d104d5b2bd7990a to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env python3 | |
# ./switchbot-plug-mini <ble-addr> [<state>] | |
# | |
# state: | |
# None: Get the current state | |
# 0: OFF | |
# 1: ON | |
# -1: TOGGLE | |
from enum import Enum | |
import sys | |
from bluepy import btle | |
class State(Enum): | |
TOGGLE = b"\x02\x80" | |
OFF = b"\x01\x00" | |
ON = b"\x01\x80" | |
class MyDelegate(btle.DefaultDelegate): | |
def handleNotification(self, cHandle, data): | |
if cHandle == 0x2A: | |
if data == State.OFF.value: | |
print("State: OFF") | |
elif data == State.ON.value: | |
print("State: ON") | |
else: | |
print("State: UNKNOWN") | |
addr = sys.argv[1] | |
state = None | |
if len(sys.argv) > 2: | |
state = list(State)[int(sys.argv[2]) + 1] | |
peri = btle.Peripheral(addr) | |
peri.setDelegate(MyDelegate()) | |
if state is not None: | |
peri.writeCharacteristic(0x2D, b"\x57\x0f\x50\x01" + state.value) | |
else: | |
peri.writeCharacteristic(0x2D, b"\x57\x0f\x51\x01") | |
peri.waitForNotifications(1.0) | |
peri.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment