Last active
August 5, 2024 01:41
-
-
Save eadmaster/5c36fd467cf57f5f4025ee6b8082a600 to your computer and use it in GitHub Desktop.
tasmotairjson2flipperir.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
# converts tasmota irsend commands to flipper ir files | |
# example usage: python tasmotairjson2flipperir.py <<< '{"Protocol":"NEC","Bits":32,"Data":"0x20DF10EF"}' | |
import sys | |
import json | |
def tasmotairjson2flipperir(data, name="??"): | |
# Extract parameters from JSON data | |
protocol = data.get("Protocol") | |
num_bits = data.get("Bits") | |
hex_data = data.get("Data") | |
hex_data_val = int(hex_data.strip(), 16) | |
if protocol is None or num_bits is None or hex_data is None: | |
print("Error: Missing required fields in JSON data.") | |
sys.exit(1) | |
#encoded_value = 0x20DF10EF # Example encoded value | |
print(hex_data_val); | |
address, command = decodeNEC(hex_data_val) | |
address_str = f"{address:02X} 00 00 00" | |
command_str = f"{command:02X} 00 00 00" | |
ir_file_content = ( | |
"Filetype: IR signals file\n" | |
"Version: 1\n" | |
"#\n" | |
"name: " + name + "\n" | |
"type: parsed\n" | |
"protocol: " + protocol + "\n" | |
"address: " + address_str + "\n" | |
"command: " + command_str + "\n" | |
"#\n" | |
) | |
return ir_file_content | |
def reverseBits(value, bits): | |
"""Reverse the bits of the given value with the specified number of bits.""" | |
reversed_value = 0 | |
for i in range(bits): | |
reversed_value = (reversed_value << 1) | (value & 1) | |
value >>= 1 | |
return reversed_value | |
def decodeNEC(encoded_value): | |
"""Decode a 32-bit NEC encoded value back into address and command.""" | |
# Extract the command part | |
encoded_command = encoded_value & 0xFFFF | |
command = (encoded_command >> 8) & 0xFF | |
command = reverseBits(command, 8) | |
# Determine if the address is extended or normal | |
high_byte_address = (encoded_value >> 24) & 0xFF | |
#if high_byte_address == reverseBits((encoded_value >> 16) & 0xFF, 8): | |
# # Normal NEC | |
# address = reverseBits(high_byte_address, 8) | |
#else: | |
# # Extended NEC | |
# encoded_address = (encoded_value >> 16) & 0xFFFF | |
# address = reverseBits(encoded_address, 16) | |
address = reverseBits(high_byte_address, 8) | |
return address, command | |
if __name__ == "__main__": | |
import argparse, sys | |
parser = argparse.ArgumentParser() | |
parser.add_argument('infile', nargs='?', default="-", help="input file, default to stdin if unspecified. Supports passing urls.") | |
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w', encoding='utf-8'), default=sys.stdout, help="output file, default to stdout if unspecified") | |
args = parser.parse_args() | |
if args.infile == "-": | |
infile = sys.stdin | |
sys.stderr.write("reading from stdin...\n") | |
elif args.infile.startswith(("http://", "ftp://", "https://")): # TODO: proper URL validation | |
from urllib.request import urlopen | |
infile = urlopen(args.infile) | |
# switch to text file mode | |
import codecs | |
infile = codecs.getreader("utf-8")(infile) | |
else: | |
infile = open(args.infile, encoding="utf-8") | |
# Read JSON data from stdin | |
try: | |
json_data = infile.read() | |
data = json.loads(json_data) | |
except json.JSONDecodeError: | |
print("Error: Failed to parse JSON data.") | |
sys.exit(1) | |
if infile == sys.stdin: | |
output = tasmotairjson2flipperir(data) | |
else: | |
output = tasmotairjson2flipperir(data, args.infile) | |
args.outfile.write(output) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment