mirror of
https://github.com/mjg59/python-broadlink.git
synced 2024-11-21 22:51:41 +01:00
Handle ReadError in the CLI (#365)
* Handle ReadError * Make it more pythonic * Clean up
This commit is contained in:
parent
a731c9c5a5
commit
7dec8f2355
@ -6,8 +6,10 @@ import codecs
|
||||
import time
|
||||
|
||||
import broadlink
|
||||
from broadlink.exceptions import ReadError
|
||||
|
||||
TICK = 32.84
|
||||
TIMEOUT = 30
|
||||
IR_TOKEN = 0x26
|
||||
|
||||
|
||||
@ -85,7 +87,7 @@ parser.add_argument("--learnfile", help="save learned command to a specified fil
|
||||
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("--joinwifi", nargs=2, help="Args are SSID PASSPHRASE to configure Broadlink device with");
|
||||
parser.add_argument("--joinwifi", nargs=2, help="Args are SSID PASSPHRASE to configure Broadlink device with")
|
||||
parser.add_argument("data", nargs='*', help="Data to send or convert")
|
||||
args = parser.parse_args()
|
||||
|
||||
@ -128,14 +130,18 @@ if args.send:
|
||||
dev.send_data(data)
|
||||
if args.learn or args.learnfile:
|
||||
dev.enter_learning()
|
||||
data = None
|
||||
print("Learning...")
|
||||
timeout = 30
|
||||
while (data is None) and (timeout > 0):
|
||||
time.sleep(2)
|
||||
timeout -= 2
|
||||
for second in range(TIMEOUT):
|
||||
try:
|
||||
data = dev.check_data()
|
||||
if data:
|
||||
except ReadError:
|
||||
time.sleep(1)
|
||||
else:
|
||||
break
|
||||
else:
|
||||
print("No data received...")
|
||||
exit(1)
|
||||
|
||||
learned = format_durations(to_microseconds(bytearray(data))) \
|
||||
if args.durations \
|
||||
else ''.join(format(x, '02x') for x in bytearray(data))
|
||||
@ -147,8 +153,6 @@ if args.learn or 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...")
|
||||
if args.check:
|
||||
if dev.check_power():
|
||||
print('* ON *')
|
||||
@ -194,13 +198,11 @@ 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):
|
||||
for second in range(TIMEOUT):
|
||||
time.sleep(1)
|
||||
timeout -= 1
|
||||
|
||||
if timeout <= 0:
|
||||
if dev.check_frequency():
|
||||
break
|
||||
else:
|
||||
print("RF Frequency not found")
|
||||
dev.cancel_sweep_frequency()
|
||||
exit(1)
|
||||
@ -214,15 +216,17 @@ if args.rfscanlearn:
|
||||
|
||||
dev.find_rf_packet()
|
||||
|
||||
data = None
|
||||
timeout = 20
|
||||
|
||||
while (data is None) and (timeout > 0):
|
||||
time.sleep(1)
|
||||
timeout -= 1
|
||||
for second in range(TIMEOUT):
|
||||
try:
|
||||
data = dev.check_data()
|
||||
except ReadError:
|
||||
time.sleep(1)
|
||||
else:
|
||||
break
|
||||
else:
|
||||
print("No data received...")
|
||||
exit(1)
|
||||
|
||||
if data:
|
||||
print("Found RF Frequency - 2 of 2!")
|
||||
learned = format_durations(to_microseconds(bytearray(data))) \
|
||||
if args.durations \
|
||||
@ -235,5 +239,3 @@ if args.rfscanlearn:
|
||||
print("Saving to {}".format(args.learnfile))
|
||||
with open(args.learnfile, "w") as text_file:
|
||||
text_file.write(learned)
|
||||
else:
|
||||
print("No data received...")
|
||||
|
Loading…
Reference in New Issue
Block a user