diff options
author | Brent Avery <brent.avery0@gmail.com> | 2018-01-29T20·45+1100 |
---|---|---|
committer | Brent Avery <brent.avery0@gmail.com> | 2018-01-29T20·50+1100 |
commit | 4e33ef446520275e0a9426dc8182dcc3af276da3 (patch) | |
tree | bdd0af6775e1d79dd59f1b90dd8bcaf2ccb654e8 | |
parent | dd6eb8767e7a7f1f54aa1e7a7b57309bc4fc5fba (diff) |
Get rf scan learning working in CLI tool (#87)
-rw-r--r-- | broadlink/__init__.py | 9 | ||||
-rwxr-xr-x | cli/broadlink_cli | 70 |
2 files changed, 64 insertions, 15 deletions
diff --git a/broadlink/__init__.py b/broadlink/__init__.py index a4c8a4841aff..8a193d556101 100644 --- a/broadlink/__init__.py +++ b/broadlink/__init__.py @@ -503,7 +503,12 @@ class rm(device): def sweep_frequency(self): packet = bytearray(16) - packet[0] = 0x19; + packet[0] = 0x19 + self.send_packet(0x6a, packet) + + def cancel_sweep_frequency(self): + packet = bytearray(16) + packet[0] = 0x1e self.send_packet(0x6a, packet) def check_frequency(self): @@ -541,7 +546,7 @@ class rm(device): temp = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0 return temp -# For legay compatibility - don't use this +# For legacy compatibility - don't use this class rm2(rm): def __init__ (self): device.__init__(self, None, None) diff --git a/cli/broadlink_cli b/cli/broadlink_cli index 3acf168a920d..dca0a3d6b639 100755 --- a/cli/broadlink_cli +++ b/cli/broadlink_cli @@ -1,7 +1,6 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 import broadlink -import sys import argparse import time @@ -71,6 +70,7 @@ parser.add_argument("--temperature",action="store_true", help="request temperatu parser.add_argument("--send", action="store_true", help="send command") parser.add_argument("--sensors", action="store_true", help="check all sensors") parser.add_argument("--learn", action="store_true", help="learn command") +parser.add_argument("--rfscanlearn", action="store_true", help="rf scan learning") parser.add_argument("--learnfile", help="learn command and save to specified file") parser.add_argument("--durations", action="store_true", help="use durations in micro seconds instead of the Broadlink format") parser.add_argument("--convert", action="store_true", help="convert input data to durations") @@ -78,8 +78,8 @@ parser.add_argument("data", nargs='*', help="Data to send or convert") args = parser.parse_args() if args.device: - values = args.device.split(); - type = int(values[0],0) + values = args.device.split() + type = int(values[0], 0) host = values[1] mac = bytearray.fromhex(values[2]) elif args.mac: @@ -87,16 +87,16 @@ elif args.mac: host = args.host mac = bytearray.fromhex(args.mac) -if args.host: +if args.host or host is not None: dev = broadlink.gendevice(type, (host, 80), mac) dev.auth() if args.convert: data = bytearray.fromhex(''.join(args.data)) durations = to_microseconds(data) - print format_durations(durations) + print(format_durations(durations)) if args.temperature: - print dev.check_temperature() + print(dev.check_temperature()) if args.sensors: try: data = dev.check_sensors() @@ -104,7 +104,7 @@ if args.sensors: data = {} data['temperature'] = dev.check_temperature() for key in data: - print "{} {}".format(key, data[key]) + print("{} {}".format(key, data[key])) if args.send: data = durations_to_broadlink(parse_durations(' '.join(args.data))) \ if args.durations else bytearray.fromhex(''.join(args.data)) @@ -112,7 +112,7 @@ if args.send: if args.learn or args.learnfile: dev.enter_learning() data = None - print "Learning..." + print("Learning...") timeout = 30 while (data is None) and (timeout > 0): time.sleep(2) @@ -123,11 +123,55 @@ if args.learn or args.learnfile: if args.durations \ else ''.join(format(x, '02x') for x in bytearray(data)) if args.learn: - print learned + print(learned) if args.learnfile: - print "Saving to {}".format(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..." - + print("No data received...") +if args.rfscanlearn: + dev.sweep_frequency() + print("Learning RF Frequency, press and hold the button to learn...") + + timeout = 20 + + while (not dev.check_frequency()) and (timeout > 0): + time.sleep(1) + timeout -= 1 + + if timeout <= 0: + print("RF Frequency not found") + dev.cancel_sweep_frequency() + exit(1) + + print("Found RF Frequency - 1 of 2!") + print("You can now let go of the button") + + input("Press any key to continue...") + + print("To complete learning, single press the button you want to learn") + + dev.find_rf_packet() + + data = None + timeout = 20 + + while (data is None) and (timeout > 0): + time.sleep(1) + timeout -= 1 + data = dev.check_data() + + if data: + print("Found RF Frequency - 2 of 2!") + learned = format_durations(to_microseconds(bytearray(data))) \ + if args.durations \ + else ''.join(format(x, '02x') for x in bytearray(data)) + if args.learn | args.rfscanlearn: + 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 |