about summary refs log tree commit diff
path: root/cli/broadlink_cli
blob: 0dc6db66170b54cdf14fa39503ba213a424ca8f3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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..."