Last active
August 8, 2021 09:52
-
-
Save smly/598ca09f9878e59586130f625777b72c to your computer and use it in GitHub Desktop.
CLI for swithbot
This file contains hidden or 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 python | |
# -*- coding: utf-8 -*- | |
""" | |
Usage: switchbot [OPTIONS] | |
Options: | |
-d, --device [light|speaker.tv|speaker.mpd|aircon] | |
[required] | |
--on | |
--off | |
--help Show this message and exit. | |
Examples: | |
$ switchbot -d light --on | |
$ switchbot -d light --off | |
$ switchbot -d aircon | |
$ switchbot -d speaker.mpd | |
""" | |
import binascii | |
from bluepy.btle import Peripheral | |
import click | |
DEVICES = { | |
'light': { | |
'method': 'toggle', | |
'addr': 'EF:81:09:XX:XX:XX', | |
}, | |
'speaker.tv': { | |
'method': 'push', | |
'addr': 'F2:00:5C:XX:XX:XX', | |
}, | |
'speaker.mpd': { | |
'method': 'push', | |
'addr': 'E9:34:90:XX:XX:XX', | |
}, | |
'aircon': { | |
'method': 'push', | |
'addr': 'EF:9F:E1:XX:XX:XX', | |
}, | |
} | |
@click.command() | |
@click.option('--device', '-d', type=click.Choice(DEVICES.keys()), required=True) | |
@click.option('--on', 'toggle_action', flag_value='on', default=True) | |
@click.option('--off', 'toggle_action', flag_value='off') | |
def main(device, toggle_action): | |
device_addr = DEVICES[device]['addr'] | |
command = '570100' | |
if DEVICES[device]['method'] == 'toggle': | |
command = '570101' if toggle_action == 'on' else '570102' | |
p = Peripheral(device_addr, "random") | |
hand_service = p.getServiceByUUID("cba20d00-224d-11e6-9fb8-0002a5d5c51b") | |
hand = hand_service.getCharacteristics("cba20002-224d-11e6-9fb8-0002a5d5c51b")[0] | |
hand.write(binascii.a2b_hex(command)) | |
p.disconnect() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment