about summary refs log tree commit diff
path: root/cli/broadlink_cli
diff options
context:
space:
mode:
Diffstat (limited to 'cli/broadlink_cli')
-rwxr-xr-xcli/broadlink_cli70
1 files changed, 57 insertions, 13 deletions
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