From 755f41466d8036063b4b4365bb204cc2d3cf9f3e Mon Sep 17 00:00:00 2001 From: clach04 Date: Mon, 20 Aug 2018 22:11:42 -0700 Subject: [PATCH 1/6] Remove hard dependency on simplejson Use builtin library where possible. For Python pre 2.6 use a quick and dirty builtin. --- getBroadlinkSharedData.py | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) diff --git a/getBroadlinkSharedData.py b/getBroadlinkSharedData.py index c5763a4..e07e11e 100644 --- a/getBroadlinkSharedData.py +++ b/getBroadlinkSharedData.py @@ -21,9 +21,40 @@ ''' -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 + if len(sys.argv) > 1: MultipleCode = sys.argv[1] @@ -36,7 +67,7 @@ jsonSubIr = open("jsonSubIr").read() -jsonSubIrData = json.loads(jsonSubIr) +jsonSubIrData = load_json(jsonSubIr) for i in range(0, len(jsonSubIrData)): @@ -52,7 +83,7 @@ jsonButton = open("jsonButton").read() -jsonButtonData = json.loads(jsonButton) +jsonButtonData = load_json(jsonButton) for i in range(0, len(jsonButtonData)): @@ -62,7 +93,7 @@ jsonIrCode = open("jsonIrCode").read() -jsonIrCodeData = json.loads(jsonIrCode) +jsonIrCodeData = load_json(jsonIrCode) print "[+] Dumping codes to " + accessory_name + ".txt" From 5ec320383ec6ca8f4f760089bf9b837cd9b43aab Mon Sep 17 00:00:00 2001 From: clach04 Date: Mon, 20 Aug 2018 23:27:25 -0700 Subject: [PATCH 2/6] Optional Python3 support for getBroadlinkSharedData.py Still works with Python 2. Only difference in behavior is base64 encoded data no long line wraps in output. --- getBroadlinkSharedData.py | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/getBroadlinkSharedData.py b/getBroadlinkSharedData.py index e07e11e..9c12951 100644 --- a/getBroadlinkSharedData.py +++ b/getBroadlinkSharedData.py @@ -56,6 +56,15 @@ def load_json(x): 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] else: @@ -70,16 +79,18 @@ def load_json(x): 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() @@ -96,13 +107,15 @@ def load_json(x): jsonIrCodeData = load_json(jsonIrCode) -print "[+] Dumping codes to " + accessory_name + ".txt" +print("[+] Dumping codes to " + accessory_name + ".txt") codesFile = open(accessory_name + '.txt', 'w') 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) From 5b7e16063daf31f5d2526d5930a4b2873ef13087 Mon Sep 17 00:00:00 2001 From: clach04 Date: Tue, 21 Aug 2018 21:25:12 -0700 Subject: [PATCH 3/6] Python3 support for broadlink_to_home_assistant_encoder.py --- broadlink_to_home_assistant_encoder.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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) From f7057b940cb50f650b15c634d0ee7668c2a89fca Mon Sep 17 00:00:00 2001 From: clach04 Date: Fri, 12 Jun 2020 17:12:10 -0700 Subject: [PATCH 4/6] Dump utf8 text files Avoids error on write if non-ascii characters present in names. --- getBroadlinkSharedData.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getBroadlinkSharedData.py b/getBroadlinkSharedData.py index 9c12951..aab95b4 100644 --- a/getBroadlinkSharedData.py +++ b/getBroadlinkSharedData.py @@ -118,4 +118,4 @@ def hex2bin(x): 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.write(result) + codesFile.write(result.encode('utf-8')) From de1af50e15f80aecac2b3a6bd19a217ad634514b Mon Sep 17 00:00:00 2001 From: clach04 Date: Sat, 19 Mar 2022 09:57:39 -0700 Subject: [PATCH 5/6] Clarify in readme what this project is at the head Along with Python version information. --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 From 54640cc624b8f5f746e79c219cd6b2e79ad312e0 Mon Sep 17 00:00:00 2001 From: clach04 Date: Tue, 27 Sep 2022 17:04:47 -0700 Subject: [PATCH 6/6] Ensure binary mode for ouput Related to #1 --- getBroadlinkSharedData.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/getBroadlinkSharedData.py b/getBroadlinkSharedData.py index aab95b4..9a94443 100644 --- a/getBroadlinkSharedData.py +++ b/getBroadlinkSharedData.py @@ -108,7 +108,7 @@ def hex2bin(x): print("[+] Dumping codes to " + accessory_name + ".txt") -codesFile = open(accessory_name + '.txt', 'w') +codesFile = open(accessory_name + '.txt', 'wb') for i in range(0, len(jsonIrCodeData)): for j in range(0, len(buttonIDS)):