Broadlink RM-PRO and TC2 switch

@oliverdog

I tried to run the script but it returns this error…

$ python3 getBroadlinkSharedData.py
  File "getBroadlinkSharedData.py", line 32
    print "ID:", jsonSubIrData[i]['id'], "| Name:", jsonSubIrData[i]['name']
              ^
SyntaxError: Missing parentheses in call to 'print'

Please help.

Use python2

Thanks. But now I am receiving this error…

$ python2 getBroadlinkSharedData.py
Traceback (most recent call last):
  File "getBroadlinkSharedData.py", line 20, in <module>
    import simplejson as json
ImportError: No module named simplejson

FYI, I installed simplejson using pip3 install simplejson. When I tried to reinstall it using pip, it says…

$ pip install simplejson
Requirement already satisfied: simplejson in /usr/local/lib/python3.4dist-packages

I manage to install Python2 in my Windows PC and manage to generate the text file filed with the TC2 codes. Will try to use the codes in my configuration soon.

@Danielhiversen

I put the codes into my configuration…

switch:
  platform: broadlink
  host: 192.168.1.000
  mac: 'AA:AA:AA:AA:AA:AA'
  switches:
    carporch_light:
      command_on: 'e9144600df090916091616090916091616091609160916091609160916090916160916091609160909161609091609161609160916090916091609161609160916090916160909000148'
      command_off: 'e9144600df090916091609160916160909161609160909160916091609161609160916091609091616091609160916090916091609160916160916091609160909161609160909000148'

It is not working. Any idea why?

That looks like hex format,
You have to convert it to the correct format,
You can try:

I am sorry. How do I use this…?

from base64 import b64encode, b64decode
hex_data  = "e9144600df090916160916090916091616090916160916090916160916090916160909161609091616091609160916091609160916091609091609160916160909161609091609000148"
packet = hex_data.decode('hex')
b64encode(packet).decode('utf8')

OK. If I understand correctly, that is code is in python. So, I modified getBroadlinkSharedData.py by adding from base64 import b64encode, b64decode on line 21 and then…

below…

code = ''.join('%02x' % (i & 0xff) for i in jsonIrCodeData[i]['code'])

add this…

        packet = code.decode('hex')
        code = b64encode(packet).decode('utf8')

The generated codes will become something like this…

6RRGAN8JCRYJFgkWCRYWCQkWFgkWCQkWCRYJFgkWFgkWCRYJFgkJFhYJFgkWCRYJCRYJFgkWCRYWCRYJFgkWCQkWFgkWCQkAAUg=

However, it is still not working :sweat:

I notice this in the log ValueError: Input strings must be a multiple of 16 in length.

I can feel we are getting closer…

@masterkenobi That’s exactly the issue other users are experiencing as well, the code format you get from " getBroadlinkSharedData.py" should work directly with “python-broadlink” (for the TC2 rf codes I found that you need to duplicate the code so it will be in the expected length and if you send it should work), but…I don’t know how python-broadlink was integrated into home assistant, the issue is still with the AES encryption, I believe it because of missing padding.

So you say that the code can be used directly as data in the send_data function?

https://github.com/mjg59/python-broadlink/blob/master/broadlink/init.py#L452

Edit:
Solving this issue will help: https://github.com/NightRang3r/Broadlink-e-control-db-dump/issues/1

I do not have the RM-PRO device, so it is hard for me to test this

Btw, here is how we encode the code in hass: https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/switch/broadlink.py#L93

Exactly,

My script just dumps the codes from the e-Control app.

The first method for the rooted devices the codes stored in the rmt.db in binary, when you copy them in hex they work fine with the send_data function, you will still have the length issue (at least for TC2 switches) but if you duplicate the code and send it using the function it works great.

So for example if your code is: abcd you need to send abcdabcd

For the second script getBroadlinkSharedData.py the codes are stored in decimal format array so my script converts them to hex which should be the format to use with python-broadlink but you still need to send duplicate code.

#!/usr/bin/python

import broadlink
import time
import sys

device = broadlink.rm(host=(“IP ADDRESS”,80), mac=bytearray.fromhex(“MAC ADDRESS”))

device.auth()
time.sleep(1)
time.sleep(1)
device.host

myhex =“abcdabcd”

device.send_data(myhex.decode(‘hex’))
print “Code Sent…”

I will do some more tests later today

Btw: I don’t have home assistant installed yet, tried yesterday on a rpi with the one click install script but it failed, I’ll try to install the hassbian so I could see what’s going on

Hmm, then this should work:

hexcode = hexcode + hexcode
packet = hexcode.decode('hex')
code = b64encode(packet).decode('utf8')

Theoretically It should, but someone on another post mentioned he tried duplicating the code and it didn’t worked with HA, after I’ll install it I can do proper testing.

1 Like

@masterkenobi

Ok, Test 1, Dumped the codes using my “getBroadlinkSharedData.py” script, sending it using the following code and it’s working, as you can see you need to duplicate the code, I am installing HA and now and hopefully I will be able to see if there’s any problem:

import broadlink
import time
import sys

device = broadlink.rm(host=(“10.0.0.2”,80), mac=bytearray.fromhex(“b4430fffffff”))

print “Connecting to Broadlink device…”
device.auth()
time.sleep(1)
print “Connected…”
time.sleep(1)
device.host

hex1 = “e90a4200df0909160916091616091609091616091609091616091609160916091609091616090916091616090916091616090916091609161609091616091609160909161609”
hex2 = “e90a4200df0909160916091616091609091616091609091616091609160916091609091616090916091616090916091616090916091609161609091616091609160909161609”
myhex = hex1 + hex2

device.send_data(myhex.decode(‘hex’))
print “Code Sent…”

1 Like

There have been a lot of bug fixes in the Broadlink component since the last release. So I suggest that you wait for the next release or use the dev version: https://home-assistant.io/developers/development_environment

@masterkenobi Ok, all is working fine when using the library directly, i have tested with HA and having the same issue with length, It can be the version of the library or the way it was implemented int HA.

I guess we will have to wait for the updates and try again.

1 Like

How to convert hexcode to b64encode

or

Great to hear that. I guess we just need to wait for @Danielhiversen to make it works on the next release. By the way, I can help you to test it. I just need to know how. Thanks guys!