Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to RF scan a specific frequency #613

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions broadlink/remote.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for universal remotes."""
import struct
import typing as t

from . import exceptions as e
from .device import Device
Expand Down Expand Up @@ -46,14 +47,19 @@ def sweep_frequency(self) -> None:
"""Sweep frequency."""
self._send(0x19)

def check_frequency(self) -> bool:
def check_frequency(self) -> t.Tuple[bool, float]:
"""Return True if the frequency was identified successfully."""
resp = self._send(0x1A)
return resp[0] == 1
is_found = bool(resp[0])
frequency = struct.unpack("<I", resp[1:5])[0] / 1000.0
return is_found, frequency

def find_rf_packet(self) -> None:
def find_rf_packet(self, frequency: float = None) -> None:
"""Enter radiofrequency learning mode."""
self._send(0x1B)
payload = bytearray()
if frequency:
payload += struct.pack("<I", int(frequency * 1000))
self._send(0x1B, payload)

def cancel_sweep_frequency(self) -> None:
"""Cancel sweep frequency."""
Expand Down
46 changes: 26 additions & 20 deletions cli/broadlink_cli
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ parser.add_argument("--switch", action="store_true", help="switch state from on
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("--rflearn", action="store_true", help="rf scan learning")
parser.add_argument("--frequency", type=float, help="specify radiofrequency for learning")
parser.add_argument("--learnfile", help="save learned command to a specified file")
parser.add_argument("--durations", action="store_true",
help="use durations in micro seconds instead of the Broadlink format")
Expand Down Expand Up @@ -127,7 +128,7 @@ if args.send:
data = durations_to_broadlink(parse_durations(' '.join(args.data))) \
if args.durations else bytearray.fromhex(''.join(args.data))
dev.send_data(data)
if args.learn or (args.learnfile and not args.rfscanlearn):
if args.learn or (args.learnfile and not args.rflearn):
dev.enter_learning()
print("Learning...")
start = time.time()
Expand Down Expand Up @@ -195,28 +196,33 @@ if args.switch:
else:
dev.set_power(True)
print('* Switch to ON *')
if args.rfscanlearn:
dev.sweep_frequency()
print("Learning RF Frequency, press and hold the button to learn...")

start = time.time()
while time.time() - start < TIMEOUT:
time.sleep(1)
if dev.check_frequency():
break
if args.rflearn:
if args.frequency:
frequency = args.frequency
print("Press the button you want to learn, a short press...")
else:
print("RF Frequency not found")
dev.cancel_sweep_frequency()
exit(1)
dev.sweep_frequency()
print("Detecting radiofrequency, press and hold the button to learn...")

start = time.time()
while time.time() - start < TIMEOUT:
time.sleep(1)
locked, frequency = dev.check_frequency()
if locked:
break
else:
print("Radiofrequency not found")
dev.cancel_sweep_frequency()
exit(1)

print("Found RF Frequency - 1 of 2!")
print("You can now let go of the button")
print("Radiofrequency detected: {}MHz".format(frequency))
print("You can now let go of the button")

input("Press enter to continue...")
input("Press enter to continue...")

print("To complete learning, single press the button you want to learn")
print("Press the button again, now a short press.")

dev.find_rf_packet()
dev.find_rf_packet(frequency)

start = time.time()
while time.time() - start < TIMEOUT:
Expand All @@ -231,7 +237,7 @@ if args.rfscanlearn:
print("No data received...")
exit(1)

print("Found RF Frequency - 2 of 2!")
print("Packet found!")
learned = format_durations(to_microseconds(bytearray(data))) \
if args.durations \
else ''.join(format(x, '02x') for x in bytearray(data))
Expand Down