Skip to content

Instantly share code, notes, and snippets.

@ma2shita
Last active August 12, 2019 01:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ma2shita/f5cafe63d54ffb74b318836f0da674e0 to your computer and use it in GitHub Desktop.
Save ma2shita/f5cafe63d54ffb74b318836f0da674e0 to your computer and use it in GitHub Desktop.
Operation for SwitchBot (using Pyhton3)
#!/usr/bin/env python3
import binascii
from bluepy.btle import Peripheral
class SwitchBot:
def __init__(self, mac_address):
self.p = Peripheral(mac_address, "random")
hand_service = self.p.getServiceByUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b")
self.hand = hand_service.getCharacteristics("cba20002-224d-11e6-9fb8-0002a5d5c51b")[0]
def press(self):
self.hand.write(binascii.a2b_hex("570100"))
self.p.disconnect()
def on(self):
self.p.disconnect()
raise NotImplementedError()
def off(self):
self.p.disconnect()
raise NotImplementedError()
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a", "--address", dest="addr", help="MAC address of SwitchBot")
parser.add_option("-c", "--command", dest="cmd", help="Command for SwitchBot (press|on|off)")
(options, args) = parser.parse_args()
getattr(SwitchBot(options.addr), options.cmd)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment