Get rf scan learning working in CLI tool (#87)

This commit is contained in:
Brent Avery 2018-01-30 07:45:21 +11:00
parent dd6eb8767e
commit 4e33ef4465
2 changed files with 64 additions and 15 deletions

View file

@ -503,7 +503,12 @@ class rm(device):
def sweep_frequency(self): def sweep_frequency(self):
packet = bytearray(16) 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) self.send_packet(0x6a, packet)
def check_frequency(self): def check_frequency(self):
@ -541,7 +546,7 @@ class rm(device):
temp = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0 temp = (ord(payload[0x4]) * 10 + ord(payload[0x5])) / 10.0
return temp return temp
# For legay compatibility - don't use this # For legacy compatibility - don't use this
class rm2(rm): class rm2(rm):
def __init__ (self): def __init__ (self):
device.__init__(self, None, None) device.__init__(self, None, None)

View file

@ -1,7 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python3
import broadlink import broadlink
import sys
import argparse import argparse
import time 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("--send", action="store_true", help="send command")
parser.add_argument("--sensors", action="store_true", help="check all sensors") parser.add_argument("--sensors", action="store_true", help="check all sensors")
parser.add_argument("--learn", action="store_true", help="learn command") 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("--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("--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") 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() args = parser.parse_args()
if args.device: if args.device:
values = args.device.split(); values = args.device.split()
type = int(values[0],0) type = int(values[0], 0)
host = values[1] host = values[1]
mac = bytearray.fromhex(values[2]) mac = bytearray.fromhex(values[2])
elif args.mac: elif args.mac:
@ -87,16 +87,16 @@ elif args.mac:
host = args.host host = args.host
mac = bytearray.fromhex(args.mac) mac = bytearray.fromhex(args.mac)
if args.host: if args.host or host is not None:
dev = broadlink.gendevice(type, (host, 80), mac) dev = broadlink.gendevice(type, (host, 80), mac)
dev.auth() dev.auth()
if args.convert: if args.convert:
data = bytearray.fromhex(''.join(args.data)) data = bytearray.fromhex(''.join(args.data))
durations = to_microseconds(data) durations = to_microseconds(data)
print format_durations(durations) print(format_durations(durations))
if args.temperature: if args.temperature:
print dev.check_temperature() print(dev.check_temperature())
if args.sensors: if args.sensors:
try: try:
data = dev.check_sensors() data = dev.check_sensors()
@ -104,7 +104,7 @@ if args.sensors:
data = {} data = {}
data['temperature'] = dev.check_temperature() data['temperature'] = dev.check_temperature()
for key in data: for key in data:
print "{} {}".format(key, data[key]) print("{} {}".format(key, data[key]))
if args.send: if args.send:
data = durations_to_broadlink(parse_durations(' '.join(args.data))) \ data = durations_to_broadlink(parse_durations(' '.join(args.data))) \
if args.durations else bytearray.fromhex(''.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: if args.learn or args.learnfile:
dev.enter_learning() dev.enter_learning()
data = None data = None
print "Learning..." print("Learning...")
timeout = 30 timeout = 30
while (data is None) and (timeout > 0): while (data is None) and (timeout > 0):
time.sleep(2) time.sleep(2)
@ -123,11 +123,55 @@ if args.learn or args.learnfile:
if args.durations \ if args.durations \
else ''.join(format(x, '02x') for x in bytearray(data)) else ''.join(format(x, '02x') for x in bytearray(data))
if args.learn: if args.learn:
print learned print(learned)
if args.learnfile: if args.learnfile:
print "Saving to {}".format(args.learnfile) print("Saving to {}".format(args.learnfile))
with open(args.learnfile, "w") as text_file: with open(args.learnfile, "w") as text_file:
text_file.write(learned) text_file.write(learned)
else: 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...")