Skip to content

Commit 76dd4cf

Browse files
ivanfmartinezmjg59
authored andcommitted
command line programs to control broadlink devices
1 parent 846cc35 commit 76dd4cf

2 files changed

Lines changed: 84 additions & 0 deletions

File tree

cli/broadlink_cli

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python
2+
3+
import broadlink
4+
import sys
5+
import argparse
6+
import time
7+
8+
def auto_int(x):
9+
return int(x, 0)
10+
11+
parser = argparse.ArgumentParser(fromfile_prefix_chars='@');
12+
parser.add_argument("--device", help="device definition as 'type host mac'")
13+
parser.add_argument("--type", type=auto_int, default=0x2712, help="type of device")
14+
parser.add_argument("--host", help="host address")
15+
parser.add_argument("--mac", help="mac address (hex reverse), as used by python-broadlink library")
16+
parser.add_argument("--temperature",action="store_true", help="request temperature from device")
17+
parser.add_argument("--send", help="send command")
18+
parser.add_argument("--learn",action="store_true", help="learn command")
19+
parser.add_argument("--learnfile", help="learn command and save to specified file")
20+
args = parser.parse_args()
21+
22+
if args.device:
23+
values = args.device.split();
24+
type = int(values[0],0)
25+
host = values[1]
26+
mac = bytearray.fromhex(values[2])
27+
else:
28+
type = args.type
29+
host = args.host
30+
mac = bytearray.fromhex(args.mac)
31+
32+
33+
dev = broadlink.gendevice(type, (host, 80), mac)
34+
dev.auth()
35+
if args.temperature:
36+
print dev.check_temperature()
37+
if args.send:
38+
data = bytearray.fromhex(args.send)
39+
dev.send_data(data)
40+
if args.learn or args.learnfile:
41+
dev.enter_learning()
42+
data = None
43+
print "Learning..."
44+
timeout = 30
45+
while (data is None) and (timeout > 0):
46+
time.sleep(2)
47+
timeout -= 2
48+
data = dev.check_data()
49+
if data:
50+
learned = ''.join(format(x, '02x') for x in bytearray(data))
51+
if args.learn:
52+
print learned
53+
if args.learnfile:
54+
print "Saving to {}".format(args.learnfile)
55+
with open(args.learnfile, "w") as text_file:
56+
text_file.write(learned)
57+
else:
58+
print "No data received..."
59+

cli/broadlink_discovery

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/python
2+
3+
import broadlink
4+
import time
5+
import argparse
6+
7+
parser = argparse.ArgumentParser(fromfile_prefix_chars='@');
8+
parser.add_argument("--timeout", type=int, default=5, help="timeout to wait for receiving discovery responses")
9+
args = parser.parse_args()
10+
11+
print "discover"
12+
devices = broadlink.discover(timeout=args.timeout)
13+
#print devices
14+
for device in devices:
15+
if device.auth():
16+
print "###########################################"
17+
# print device
18+
print device.type
19+
print "# broadlink_cli --type 0x2712 --host {} --mac {}".format(device.host[0], ''.join(format(x, '02x') for x in device.mac))
20+
print "Device file data (to be used with --device @filename in broadlink_cli) : "
21+
print "0x2712 {} {}".format(device.host[0], ''.join(format(x, '02x') for x in device.mac))
22+
print "temperature = {}".format(device.check_temperature())
23+
print ""
24+
else:
25+
print "Error authenticating with device : {}".format(device.host)

0 commit comments

Comments
 (0)