From 76dd4cfc70fa95ae703c680998757be180deed36 Mon Sep 17 00:00:00 2001 From: Ivan Martinez Date: Tue, 10 Jan 2017 22:55:02 -0200 Subject: [PATCH] command line programs to control broadlink devices --- cli/broadlink_cli | 59 +++++++++++++++++++++++++++++++++++++++++ cli/broadlink_discovery | 25 +++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100755 cli/broadlink_cli create mode 100755 cli/broadlink_discovery diff --git a/cli/broadlink_cli b/cli/broadlink_cli new file mode 100755 index 0000000..0dc6db6 --- /dev/null +++ b/cli/broadlink_cli @@ -0,0 +1,59 @@ +#!/usr/bin/python + +import broadlink +import sys +import argparse +import time + +def auto_int(x): + return int(x, 0) + +parser = argparse.ArgumentParser(fromfile_prefix_chars='@'); +parser.add_argument("--device", help="device definition as 'type host mac'") +parser.add_argument("--type", type=auto_int, default=0x2712, help="type of device") +parser.add_argument("--host", help="host address") +parser.add_argument("--mac", help="mac address (hex reverse), as used by python-broadlink library") +parser.add_argument("--temperature",action="store_true", help="request temperature from device") +parser.add_argument("--send", help="send command") +parser.add_argument("--learn",action="store_true", help="learn command") +parser.add_argument("--learnfile", help="learn command and save to specified file") +args = parser.parse_args() + +if args.device: + values = args.device.split(); + type = int(values[0],0) + host = values[1] + mac = bytearray.fromhex(values[2]) +else: + type = args.type + host = args.host + mac = bytearray.fromhex(args.mac) + + +dev = broadlink.gendevice(type, (host, 80), mac) +dev.auth() +if args.temperature: + print dev.check_temperature() +if args.send: + data = bytearray.fromhex(args.send) + dev.send_data(data) +if args.learn or args.learnfile: + dev.enter_learning() + data = None + print "Learning..." + timeout = 30 + while (data is None) and (timeout > 0): + time.sleep(2) + timeout -= 2 + data = dev.check_data() + if data: + learned = ''.join(format(x, '02x') for x in bytearray(data)) + if args.learn: + print learned + if args.learnfile: + print "Saving to {}".format(args.learnfile) + with open(args.learnfile, "w") as text_file: + text_file.write(learned) + else: + print "No data received..." + \ No newline at end of file diff --git a/cli/broadlink_discovery b/cli/broadlink_discovery new file mode 100755 index 0000000..84d96df --- /dev/null +++ b/cli/broadlink_discovery @@ -0,0 +1,25 @@ +#!/usr/bin/python + +import broadlink +import time +import argparse + +parser = argparse.ArgumentParser(fromfile_prefix_chars='@'); +parser.add_argument("--timeout", type=int, default=5, help="timeout to wait for receiving discovery responses") +args = parser.parse_args() + +print "discover" +devices = broadlink.discover(timeout=args.timeout) +#print devices +for device in devices: + if device.auth(): + print "###########################################" +# print device + print device.type + print "# broadlink_cli --type 0x2712 --host {} --mac {}".format(device.host[0], ''.join(format(x, '02x') for x in device.mac)) + print "Device file data (to be used with --device @filename in broadlink_cli) : " + print "0x2712 {} {}".format(device.host[0], ''.join(format(x, '02x') for x in device.mac)) + print "temperature = {}".format(device.check_temperature()) + print "" + else: + print "Error authenticating with device : {}".format(device.host)