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 time
|
||||||
|
|
||||||
import broadlink
|
import broadlink
|
||||||
|
from broadlink.exceptions import ReadError
|
||||||
|
|
||||||
TICK = 32.84
|
TICK = 32.84
|
||||||
|
TIMEOUT = 30
|
||||||
IR_TOKEN = 0x26
|
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",
|
parser.add_argument("--durations", action="store_true",
|
||||||
help="use durations in micro seconds instead of the Broadlink format")
|
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")
|
||||||
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")
|
parser.add_argument("data", nargs='*', help="Data to send or convert")
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
@ -128,27 +130,29 @@ if args.send:
|
|||||||
dev.send_data(data)
|
dev.send_data(data)
|
||||||
if args.learn or args.learnfile:
|
if args.learn or args.learnfile:
|
||||||
dev.enter_learning()
|
dev.enter_learning()
|
||||||
data = None
|
|
||||||
print("Learning...")
|
print("Learning...")
|
||||||
timeout = 30
|
for second in range(TIMEOUT):
|
||||||
while (data is None) and (timeout > 0):
|
try:
|
||||||
time.sleep(2)
|
data = dev.check_data()
|
||||||
timeout -= 2
|
except ReadError:
|
||||||
data = dev.check_data()
|
time.sleep(1)
|
||||||
if data:
|
else:
|
||||||
learned = format_durations(to_microseconds(bytearray(data))) \
|
break
|
||||||
if args.durations \
|
|
||||||
else ''.join(format(x, '02x') for x in bytearray(data))
|
|
||||||
if args.learn:
|
|
||||||
print(learned)
|
|
||||||
decode_hex = codecs.getdecoder("hex_codec")
|
|
||||||
print("Base64: " + str(base64.b64encode(decode_hex(learned)[0])))
|
|
||||||
if args.learnfile:
|
|
||||||
print("Saving to {}".format(args.learnfile))
|
|
||||||
with open(args.learnfile, "w") as text_file:
|
|
||||||
text_file.write(learned)
|
|
||||||
else:
|
else:
|
||||||
print("No data received...")
|
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))
|
||||||
|
if args.learn:
|
||||||
|
print(learned)
|
||||||
|
decode_hex = codecs.getdecoder("hex_codec")
|
||||||
|
print("Base64: " + str(base64.b64encode(decode_hex(learned)[0])))
|
||||||
|
if args.learnfile:
|
||||||
|
print("Saving to {}".format(args.learnfile))
|
||||||
|
with open(args.learnfile, "w") as text_file:
|
||||||
|
text_file.write(learned)
|
||||||
if args.check:
|
if args.check:
|
||||||
if dev.check_power():
|
if dev.check_power():
|
||||||
print('* ON *')
|
print('* ON *')
|
||||||
@ -194,13 +198,11 @@ if args.rfscanlearn:
|
|||||||
dev.sweep_frequency()
|
dev.sweep_frequency()
|
||||||
print("Learning RF Frequency, press and hold the button to learn...")
|
print("Learning RF Frequency, press and hold the button to learn...")
|
||||||
|
|
||||||
timeout = 20
|
for second in range(TIMEOUT):
|
||||||
|
|
||||||
while (not dev.check_frequency()) and (timeout > 0):
|
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
timeout -= 1
|
if dev.check_frequency():
|
||||||
|
break
|
||||||
if timeout <= 0:
|
else:
|
||||||
print("RF Frequency not found")
|
print("RF Frequency not found")
|
||||||
dev.cancel_sweep_frequency()
|
dev.cancel_sweep_frequency()
|
||||||
exit(1)
|
exit(1)
|
||||||
@ -214,26 +216,26 @@ if args.rfscanlearn:
|
|||||||
|
|
||||||
dev.find_rf_packet()
|
dev.find_rf_packet()
|
||||||
|
|
||||||
data = None
|
for second in range(TIMEOUT):
|
||||||
timeout = 20
|
try:
|
||||||
|
data = dev.check_data()
|
||||||
while (data is None) and (timeout > 0):
|
except ReadError:
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
timeout -= 1
|
else:
|
||||||
data = dev.check_data()
|
break
|
||||||
|
|
||||||
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.learnfile is None:
|
|
||||||
print(learned)
|
|
||||||
decode_hex = codecs.getdecoder("hex_codec")
|
|
||||||
print("Base64: {}".format(str(base64.b64encode(decode_hex(learned)[0]))))
|
|
||||||
if args.learnfile is not None:
|
|
||||||
print("Saving to {}".format(args.learnfile))
|
|
||||||
with open(args.learnfile, "w") as text_file:
|
|
||||||
text_file.write(learned)
|
|
||||||
else:
|
else:
|
||||||
print("No data received...")
|
print("No data received...")
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
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.learnfile is None:
|
||||||
|
print(learned)
|
||||||
|
decode_hex = codecs.getdecoder("hex_codec")
|
||||||
|
print("Base64: {}".format(str(base64.b64encode(decode_hex(learned)[0]))))
|
||||||
|
if args.learnfile is not None:
|
||||||
|
print("Saving to {}".format(args.learnfile))
|
||||||
|
with open(args.learnfile, "w") as text_file:
|
||||||
|
text_file.write(learned)
|
||||||
|
Loading…
Reference in New Issue
Block a user