Skip to content

Instantly share code, notes, and snippets.

@svyatogor
Last active March 12, 2024 22:16
Show Gist options
  • Save svyatogor/7839d00303998a9fa37eb48494dd680f to your computer and use it in GitHub Desktop.
Save svyatogor/7839d00303998a9fa37eb48494dd680f to your computer and use it in GitHub Desktop.
Convert SmartIR Broadlink commands to Tuya
@svyatogor
Copy link
Author

Huge shoutout to @mildsunrise for their research on the Tuya's format

@BenJamesAndo
Copy link

Thanks so much for creating this! Very helpful indeed.
I replaced print(process_commands(sys.argv[1])) with

output_filename = sys.argv[1].replace(".json", "_converted.json")
with open(output_filename, 'w') as output_file:
	output_file.write(process_commands(sys.argv[1]))

this way it outputs a converted file instead of printing the conversion in the terminal window.

@leviustinov
Copy link

leviustinov commented Mar 7, 2024

The converted codes work - tried using the mqtt.publish service:

service: mqtt.publish
data:
  payload: >-
    {"ir_code_to_send":"CIAifRGeAiUCYiABBIACbQaeIAMCgAJDIAsCiwZiYAcFQwK9AugBgBcAi6AbACVgF0ALwCNAK8BHAp4CBmA/gENAAYAjwDsCYgKqYEcEYgJPBtsgS0A3QHuAdwHsTUBXQAGAF4BjgAOAW8BT4AN7gE/gA2+AV4DDQEHgAa/gAavgAZPgAQdAiwK4m9shF8BDgJtA+0C7AIsgswHoAeEBA+ABG+EBD+ABT4FXgAeAi4BLwM9BN8BPQFtBNwCeIRdANYCzwHcB7E3gA7uBY+AB++ADY8AjAtsCySCn4QUnwD+Av0GfgaPgBbPBr8AfgAMBgAI="}
  topic: zigbee2mqtt/Living room - aircon/set

However, the converted JSON will now work - z2m gives an error of "Invalid message 'null', skipping...".
By me I need to send the full payload JSON data and escape the double quotes.

So for instance, I tried this just on the off command and this works.
The "heat" -> "low" -> "16" does not (z2m gives an error of "Invalid message 'null', skipping...".)

"commands": {
    "off": "{\"ir_code_to_send\": \"CIAinBFiAkMCYiABA4ACbQZACwCAYAMBbQZAB+ABDwAlICECiwaeoCMAniAPQBPgASPgAwuAP4BTAENgAcAbgDeAG0BPgCtAfwBtIFtAYwIoTiWggwKeAgbgAAeAG0ABgHvAC+ADVwJiAqqgcwAl4AgjwJ/ACwCe4AK34AGLgAMHYgJvnDcjfRHgAc9Ap8DvAYsGgS2Af4AJQSuAy+ADb8Ej4AWDgEvgAacABuACp0B7QAMAquAAb8FvA4ACzU3A70EtwXfAR4CbQJHhA6/gA6PhAU/gATvA7+ABE0CFwJfgARtAJ0Ib\"}",
    "heat": {
      "low": {
        "16": "GmIi2RFiAr0CBgIlAoACqgYlAgUHgAJPBkMCniATAZ4CQAdAGQBiIAUAYiAHACVgFwCAYBEAQyADQAUAQ2A/QB1AH0ADAGIgDwFCB0A/ACVgQUApAIAgBYA/A8kBBQdARQBiIG9AE4BHAMhgCwMlAmVOQDFAWUArQF2AH4B3wC+AE0AHQCuAmwBDIE/AFUA7gB9AqYBt4Ak7QEOAs+ABOwDIIQMCrJy9YRcAgKEDAIsgGwDnIRNAG4EP4AE7wEuA24AbgR2AtwC9YQOAe0EHQOeBK+ABz4FrACUhA4ETgGeA50AvQSFBF+AB/4BfgCPhAStAL8BvgHsB6AGBqUAFgK2AswdDAs8D9ADnBuAFg4DDwM9BT0D7APpiD0C/",

This is my configuration YAML in HA:

smartir:

climate:
  - platform: smartir
    name: Living room - aircon
    unique_id: ac_living_room
    device_code: 1343
    controller_data: zigbee2mqtt/Living room - aircon/set

Is there a way to modify this script to add this kind of output?
Much appreciated =).

I am a sysadmin and can do PowerShell and not python =).

@svyatogor
Copy link
Author

@leviustinov You need to specify the MQTT topic as zigbee2mqtt/Living room - aircon/set/ir_code_to_send, it will force z2m to put the payload in the correct field.

@leviustinov
Copy link

@leviustinov You need to specify the MQTT topic as zigbee2mqtt/Living room - aircon/set/ir_code_to_send, it will force z2m to put the payload in the correct field.

Works great, thanks!

In the meantime, knowing 0 python I ChatGPTed the fix for adding the strings and that works as well. Will be using what you mentioned though.

def encode_ir(command: str) -> str:
    signal = filter(get_raw_from_broadlink(base64.b64decode(command).hex()))

    payload = b''.join(pack('<H', t) for t in signal)
    compress_out = io.BytesIO()
    compress(compress_out, payload, level=2)
    compressed_payload = compress_out.getvalue()

    encoded_payload = base64.encodebytes(compressed_payload).decode('ascii').replace('\n', '')
    return "{\"ir_code_to_send\": \"" + encoded_payload + "\"}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment