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+
0 commit comments