diff --git a/README.md b/README.md index 70c22af..2ead8a0 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,10 @@ # Description -# * All Scripts are written using python 2.7 +https://github.com/clach04/Broadlink-e-control-db-dump + +Python 3.x and 2.7 scripts for processing data for BroadLink InfraRed / IR devices like the BroadLink New RM Mini3 IR Control Hub ,Smart Home Wi-Fi Enabled Infrared Universal Remote Control, One for All Control + +Friendly fork of https://github.com/NightRang3r/Broadlink-e-control-db-dump - pending merges of PRs # econtrol-db-dump.py diff --git a/broadlink_to_home_assistant_encoder.py b/broadlink_to_home_assistant_encoder.py index d9cfb50..750790d 100644 --- a/broadlink_to_home_assistant_encoder.py +++ b/broadlink_to_home_assistant_encoder.py @@ -4,4 +4,4 @@ packet = binascii.unhexlify(packet) packet = base64.b64encode(packet).decode('utf8') -print packet \ No newline at end of file +print(packet) diff --git a/getBroadlinkSharedData.py b/getBroadlinkSharedData.py index c5763a4..9a94443 100644 --- a/getBroadlinkSharedData.py +++ b/getBroadlinkSharedData.py @@ -21,9 +21,49 @@ ''' -import simplejson as json import base64, sys +try: + # Python 2.6+ + import json +except ImportError: + try: + # from http://code.google.com/p/simplejson + import simplejson as json + except ImportError: + json = None + +if json is None: + + def dump_json(x, indent=None): + """dumb not safe! + Works for the purposes of this specific script as quotes never + appear in data set. + + Parameter indent ignored""" + if indent: + result = pprint.pformat(x, indent) + else: + result = repr(x).replace("'", '"') + return result + + def load_json(x): + """dumb not safe! Works for the purposes of this specific script""" + x = x.replace('\r', '') + return eval(x) +else: + dump_json = json.dumps + load_json = json.loads + + +IS_PY2 = sys.version_info[0] == 2 + +def hex2bin(x): + if IS_PY2: + return x.decode('hex') + else: + return bytes.fromhex(x) + if len(sys.argv) > 1: MultipleCode = sys.argv[1] @@ -36,23 +76,25 @@ jsonSubIr = open("jsonSubIr").read() -jsonSubIrData = json.loads(jsonSubIr) +jsonSubIrData = load_json(jsonSubIr) + for i in range(0, len(jsonSubIrData)): - print "ID:", jsonSubIrData[i]['id'], "| Name:", jsonSubIrData[i]['name'] + print("ID:", jsonSubIrData[i]['id'], "| Name:", jsonSubIrData[i]['name']) choice = input("Select accessory ID: ") +choice = int(choice) for i in range(0, len(jsonSubIrData)): if jsonSubIrData[i]['id'] == choice: accessory_name = jsonSubIrData[i]['name'] - print "[+] You selected: ", accessory_name + print("[+] You selected: ", accessory_name) jsonButton = open("jsonButton").read() -jsonButtonData = json.loads(jsonButton) +jsonButtonData = load_json(jsonButton) for i in range(0, len(jsonButtonData)): @@ -62,16 +104,18 @@ jsonIrCode = open("jsonIrCode").read() -jsonIrCodeData = json.loads(jsonIrCode) +jsonIrCodeData = load_json(jsonIrCode) -print "[+] Dumping codes to " + accessory_name + ".txt" -codesFile = open(accessory_name + '.txt', 'w') +print("[+] Dumping codes to " + accessory_name + ".txt") +codesFile = open(accessory_name + '.txt', 'wb') for i in range(0, len(jsonIrCodeData)): for j in range(0, len(buttonIDS)): if jsonIrCodeData[i]['buttonId'] == buttonIDS[j]: code = ''.join('%02x' % (i & 0xff) for i in jsonIrCodeData[i]['code']) * int(MultipleCode) - code_base64 = code.decode("hex").encode("base64") + code_bytes = hex2bin(code) + code_base64 = base64.b64encode(code_bytes) + code_base64 = code_base64.decode('utf-8') result = "Button Name: " + buttonNames[j] + "\r\n" + "Button ID: " + str(jsonIrCodeData[i]['buttonId']) + "\r\n" + "Code: " + code + "\r\n" + "Base64: " + "\r\n" + code_base64 + "\r\n" - codesFile.writelines(result.encode('utf-8')) \ No newline at end of file + codesFile.write(result.encode('utf-8'))