2018-01-29 21:45:21 +01:00
|
|
|
#!/usr/bin/env python3
|
2017-01-11 01:55:02 +01:00
|
|
|
|
|
|
|
import argparse
|
2019-05-23 15:13:14 +02:00
|
|
|
import base64
|
|
|
|
import codecs
|
2019-10-02 08:25:27 +02:00
|
|
|
import time
|
|
|
|
|
|
|
|
import broadlink
|
2017-01-11 01:55:02 +01:00
|
|
|
|
2017-11-25 21:06:12 +01:00
|
|
|
TICK = 32.84
|
|
|
|
IR_TOKEN = 0x26
|
|
|
|
|
|
|
|
|
2017-01-11 01:55:02 +01:00
|
|
|
def auto_int(x):
|
|
|
|
return int(x, 0)
|
|
|
|
|
2017-11-25 21:06:12 +01:00
|
|
|
|
|
|
|
def to_microseconds(bytes):
|
|
|
|
result = []
|
|
|
|
# print bytes[0] # 0x26 = 38for IR
|
|
|
|
index = 4
|
|
|
|
while index < len(bytes):
|
|
|
|
chunk = bytes[index]
|
|
|
|
index += 1
|
|
|
|
if chunk == 0:
|
|
|
|
chunk = bytes[index]
|
|
|
|
chunk = 256 * chunk + bytes[index + 1]
|
|
|
|
index += 2
|
2019-10-02 08:25:27 +02:00
|
|
|
result.append(int(round(chunk * TICK)))
|
2017-11-25 21:06:12 +01:00
|
|
|
if chunk == 0x0d05:
|
|
|
|
break
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def durations_to_broadlink(durations):
|
|
|
|
result = bytearray()
|
|
|
|
result.append(IR_TOKEN)
|
|
|
|
result.append(0)
|
|
|
|
result.append(len(durations) % 256)
|
|
|
|
result.append(len(durations) / 256)
|
|
|
|
for dur in durations:
|
2019-10-02 08:25:27 +02:00
|
|
|
num = int(round(dur / TICK))
|
2017-11-25 21:06:12 +01:00
|
|
|
if num > 255:
|
|
|
|
result.append(0)
|
|
|
|
result.append(num / 256)
|
|
|
|
result.append(num % 256)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def format_durations(data):
|
|
|
|
result = ''
|
|
|
|
for i in range(0, len(data)):
|
|
|
|
if len(result) > 0:
|
|
|
|
result += ' '
|
|
|
|
result += ('+' if i % 2 == 0 else '-') + str(data[i])
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def parse_durations(str):
|
|
|
|
result = []
|
|
|
|
for s in str.split():
|
|
|
|
result.append(abs(int(s)))
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2018-07-15 02:01:49 +02:00
|
|
|
parser = argparse.ArgumentParser(fromfile_prefix_chars='@')
|
2017-01-11 01:55:02 +01:00
|
|
|
parser.add_argument("--device", help="device definition as 'type host mac'")
|
|
|
|
parser.add_argument("--type", type=auto_int, default=0x2712, help="type of device")
|
|
|
|
parser.add_argument("--host", help="host address")
|
|
|
|
parser.add_argument("--mac", help="mac address (hex reverse), as used by python-broadlink library")
|
2019-10-02 08:25:27 +02:00
|
|
|
parser.add_argument("--temperature", action="store_true", help="request temperature from device")
|
|
|
|
parser.add_argument("--energy", action="store_true", help="request energy consumption from device")
|
2017-12-25 01:35:09 +01:00
|
|
|
parser.add_argument("--check", action="store_true", help="check current power state")
|
2018-03-18 23:03:46 +01:00
|
|
|
parser.add_argument("--checknl", action="store_true", help="check current nightlight state")
|
2017-12-25 01:35:09 +01:00
|
|
|
parser.add_argument("--turnon", action="store_true", help="turn on device")
|
|
|
|
parser.add_argument("--turnoff", action="store_true", help="turn off device")
|
2018-03-18 23:03:46 +01:00
|
|
|
parser.add_argument("--turnnlon", action="store_true", help="turn on nightlight on the device")
|
|
|
|
parser.add_argument("--turnnloff", action="store_true", help="turn off nightlight on the device")
|
2018-03-18 22:55:03 +01:00
|
|
|
parser.add_argument("--switch", action="store_true", help="switch state from on to off and off to on")
|
2017-11-25 21:06:12 +01:00
|
|
|
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")
|
2018-01-29 21:45:21 +01:00
|
|
|
parser.add_argument("--rfscanlearn", action="store_true", help="rf scan learning")
|
2018-11-25 23:21:37 +01:00
|
|
|
parser.add_argument("--learnfile", help="save learned command to a specified file")
|
2019-10-02 08:25:27 +02:00
|
|
|
parser.add_argument("--durations", action="store_true",
|
|
|
|
help="use durations in micro seconds instead of the Broadlink format")
|
2017-11-25 21:06:12 +01:00
|
|
|
parser.add_argument("--convert", action="store_true", help="convert input data to durations")
|
2020-03-04 22:27:55 +01:00
|
|
|
parser.add_argument("--joinwifi", nargs=2, help="Args are SSID PASSPHRASE to configure Broadlink device with");
|
2017-11-25 21:06:12 +01:00
|
|
|
parser.add_argument("data", nargs='*', help="Data to send or convert")
|
2017-01-11 01:55:02 +01:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.device:
|
2018-07-15 02:01:49 +02:00
|
|
|
values = args.device.split()
|
2019-10-02 08:25:27 +02:00
|
|
|
type = int(values[0], 0)
|
2017-01-11 01:55:02 +01:00
|
|
|
host = values[1]
|
|
|
|
mac = bytearray.fromhex(values[2])
|
2017-11-25 21:06:12 +01:00
|
|
|
elif args.mac:
|
2017-01-11 01:55:02 +01:00
|
|
|
type = args.type
|
|
|
|
host = args.host
|
|
|
|
mac = bytearray.fromhex(args.mac)
|
|
|
|
|
2017-11-26 20:10:19 +01:00
|
|
|
if args.host or args.device:
|
2017-11-25 21:06:12 +01:00
|
|
|
dev = broadlink.gendevice(type, (host, 80), mac)
|
|
|
|
dev.auth()
|
2017-01-11 01:55:02 +01:00
|
|
|
|
2020-03-04 22:27:55 +01:00
|
|
|
if args.joinwifi:
|
|
|
|
broadlink.setup(args.joinwifi[0], args.joinwifi[1], 4)
|
|
|
|
|
2017-11-25 21:06:12 +01:00
|
|
|
if args.convert:
|
|
|
|
data = bytearray.fromhex(''.join(args.data))
|
|
|
|
durations = to_microseconds(data)
|
2018-01-29 21:45:21 +01:00
|
|
|
print(format_durations(durations))
|
2017-01-11 01:55:02 +01:00
|
|
|
if args.temperature:
|
2018-01-29 21:45:21 +01:00
|
|
|
print(dev.check_temperature())
|
2018-04-19 22:45:50 +02:00
|
|
|
if args.energy:
|
2018-11-25 23:21:37 +01:00
|
|
|
print(dev.get_energy())
|
2017-04-16 16:34:31 +02:00
|
|
|
if args.sensors:
|
|
|
|
try:
|
|
|
|
data = dev.check_sensors()
|
|
|
|
except:
|
|
|
|
data = {}
|
2019-10-02 08:25:27 +02:00
|
|
|
data['temperature'] = dev.check_temperature()
|
2017-04-16 16:34:31 +02:00
|
|
|
for key in data:
|
2018-01-29 21:45:21 +01:00
|
|
|
print("{} {}".format(key, data[key]))
|
2017-01-11 01:55:02 +01:00
|
|
|
if args.send:
|
2017-11-25 21:06:12 +01:00
|
|
|
data = durations_to_broadlink(parse_durations(' '.join(args.data))) \
|
|
|
|
if args.durations else bytearray.fromhex(''.join(args.data))
|
2017-01-11 01:55:02 +01:00
|
|
|
dev.send_data(data)
|
2019-05-23 15:13:14 +02:00
|
|
|
if args.learn or args.learnfile:
|
2017-01-11 01:55:02 +01:00
|
|
|
dev.enter_learning()
|
|
|
|
data = None
|
2018-01-29 21:45:21 +01:00
|
|
|
print("Learning...")
|
2017-01-11 01:55:02 +01:00
|
|
|
timeout = 30
|
|
|
|
while (data is None) and (timeout > 0):
|
|
|
|
time.sleep(2)
|
|
|
|
timeout -= 2
|
|
|
|
data = dev.check_data()
|
|
|
|
if data:
|
2017-11-25 21:06:12 +01:00
|
|
|
learned = format_durations(to_microseconds(bytearray(data))) \
|
|
|
|
if args.durations \
|
|
|
|
else ''.join(format(x, '02x') for x in bytearray(data))
|
2017-01-11 01:55:02 +01:00
|
|
|
if args.learn:
|
2018-01-29 21:45:21 +01:00
|
|
|
print(learned)
|
2019-05-23 15:13:14 +02:00
|
|
|
decode_hex = codecs.getdecoder("hex_codec")
|
|
|
|
print("Base64: " + str(base64.b64encode(decode_hex(learned)[0])))
|
2017-01-11 01:55:02 +01:00
|
|
|
if args.learnfile:
|
2018-01-29 21:45:21 +01:00
|
|
|
print("Saving to {}".format(args.learnfile))
|
2017-01-11 01:55:02 +01:00
|
|
|
with open(args.learnfile, "w") as text_file:
|
|
|
|
text_file.write(learned)
|
|
|
|
else:
|
2018-01-29 21:45:21 +01:00
|
|
|
print("No data received...")
|
2017-12-25 01:35:09 +01:00
|
|
|
if args.check:
|
|
|
|
if dev.check_power():
|
2018-11-25 23:21:37 +01:00
|
|
|
print('* ON *')
|
2017-12-25 01:35:09 +01:00
|
|
|
else:
|
2018-11-25 23:21:37 +01:00
|
|
|
print('* OFF *')
|
2018-03-18 23:03:46 +01:00
|
|
|
if args.checknl:
|
|
|
|
if dev.check_nightlight():
|
2018-11-25 23:21:37 +01:00
|
|
|
print('* ON *')
|
2018-03-18 23:03:46 +01:00
|
|
|
else:
|
2018-11-25 23:21:37 +01:00
|
|
|
print('* OFF *')
|
2017-12-25 01:35:09 +01:00
|
|
|
if args.turnon:
|
|
|
|
dev.set_power(True)
|
|
|
|
if dev.check_power():
|
2018-11-25 23:21:37 +01:00
|
|
|
print('== Turned * ON * ==')
|
2017-12-25 01:35:09 +01:00
|
|
|
else:
|
2018-11-25 23:21:37 +01:00
|
|
|
print('!! Still OFF !!')
|
2017-12-25 01:35:09 +01:00
|
|
|
if args.turnoff:
|
|
|
|
dev.set_power(False)
|
|
|
|
if dev.check_power():
|
2018-11-25 23:21:37 +01:00
|
|
|
print('!! Still ON !!')
|
2017-12-25 01:35:09 +01:00
|
|
|
else:
|
2018-11-25 23:21:37 +01:00
|
|
|
print('== Turned * OFF * ==')
|
2018-03-18 23:03:46 +01:00
|
|
|
if args.turnnlon:
|
|
|
|
dev.set_nightlight(True)
|
|
|
|
if dev.check_nightlight():
|
2018-11-25 23:21:37 +01:00
|
|
|
print('== Turned * ON * ==')
|
2018-03-18 23:03:46 +01:00
|
|
|
else:
|
2018-11-25 23:21:37 +01:00
|
|
|
print('!! Still OFF !!')
|
2018-03-18 23:03:46 +01:00
|
|
|
if args.turnnloff:
|
|
|
|
dev.set_nightlight(False)
|
|
|
|
if dev.check_nightlight():
|
2018-11-25 23:21:37 +01:00
|
|
|
print('!! Still ON !!')
|
2018-03-18 23:03:46 +01:00
|
|
|
else:
|
2018-11-25 23:21:37 +01:00
|
|
|
print('== Turned * OFF * ==')
|
2018-03-18 22:55:03 +01:00
|
|
|
if args.switch:
|
|
|
|
if dev.check_power():
|
|
|
|
dev.set_power(False)
|
2018-11-25 23:21:37 +01:00
|
|
|
print('* Switch to OFF *')
|
2018-03-18 22:55:03 +01:00
|
|
|
else:
|
|
|
|
dev.set_power(True)
|
2018-11-25 23:21:37 +01:00
|
|
|
print('* Switch to ON *')
|
2018-01-29 21:45:21 +01:00
|
|
|
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")
|
|
|
|
|
2018-11-25 22:58:33 +01:00
|
|
|
input("Press enter to continue...")
|
2018-01-29 21:45:21 +01:00
|
|
|
|
|
|
|
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))
|
2018-11-25 22:58:33 +01:00
|
|
|
if args.learnfile is None:
|
2018-01-29 21:45:21 +01:00
|
|
|
print(learned)
|
2019-10-09 15:44:40 +02:00
|
|
|
decode_hex = codecs.getdecoder("hex_codec")
|
2020-03-04 22:27:22 +01:00
|
|
|
print("Base64: {}".format(str(base64.b64encode(decode_hex(learned)[0]))))
|
2018-11-25 22:58:33 +01:00
|
|
|
if args.learnfile is not None:
|
2018-01-29 21:45:21 +01:00
|
|
|
print("Saving to {}".format(args.learnfile))
|
|
|
|
with open(args.learnfile, "w") as text_file:
|
|
|
|
text_file.write(learned)
|
|
|
|
else:
|
2018-11-25 23:21:37 +01:00
|
|
|
print("No data received...")
|