Control Broadlink TC2 through Python

Broadlink TC2 is a smart wall light switch with WiFi control and RF connectivity which let you control it through Broadlink RM Pro using its mobile app called E-Control.

However I don’t like the app, and as a developer I would like to able to control in my own app. Unfortunately, broadlink doesn’t provide their API access for us but luckly there are developers who intercept their protocol and write us a simple Python module to use. So let’s get started!

Requirements

  • Broadlink TC2, Broadlink RM Pro
  • Python 2.7
  • Android phone (emulator should work)

1. Make sure E-Control is setup on the same phone

Setup your TC2 and make sure that it works with your E-Control app.

You will need to install Broadlink RM Plugin Lite in your android phone. This app will allow you to easily extract the code that we will need to control the light.

After finished installing, open the app and enable HTTP Bridge by clicking on HTTP Bridge -> Enable HTTP Bridge

Open the URL that locate in document section and append /codes to the end of it, in my case it will be http://192.168.1.210:9876/codes

3. Getting the code

I have a 2 Gang TC2 so I will have total of 6 objects (if you have more don’t worry), The first 2 objects, probably will have displayName as TC2 1-Gang will be to open all the lights and close all the light respectively. Then you will have two objects for every switches of your light, one for open and one for close.

This is an example of an object, you should have array of it

{
    "repeat": 0,
    "order": 0,
    "sendUrl": "http://192.168.1.210:9876/send?deviceMac=MAC_ADDRESS&codeId=4",
    "displayName": "TC2 • 2-Gang",
    "code": "e9144600df091619111916090916160916091609091609160916160909160916160909160916160916090916091609161609091609160916160916091609160916090916160909000148",
    "learnedByMac": "MAC_ADDRESS",
    "remoteName": "TC2",
    "codeLength": 148,
    "id": "4",
    "name": "2-Gang",
    "index": 3,
    "remoteType": 15,
    "type": 0,
    "delay": 0
},

I recommend playing around and finding the right code by trigger the sendUrl. Mark the code that you want and move on to the next step.

4. Time for python

We will use a module create by mjg59/python-broadlink. To install the module clone the project and then run

python setup.py install

Using the module is easy, just import the module

import broadlink

and uses

broadlink.discover(timeout=5) 

to find out the devices on your network, if you know your broadlink IP address and its Mac Address you can use

broadlink.rm(host=("BROADLINK_IP",80), mac=bytearray.fromhex("MAC_ADDRESS"))

which is a lot faster than discover. There are multiple ways to optain this information. I use my router GUI to lookup the IP and Mac Address of my Broadlink RM Pro and force it to bind to that particular address.

After you have your devices instance, you’ll have to authenticate using

your_device_object.auth()

This step is a bit tricky because the module require you to send the code in multiple of 8 but the codes that you got from RM Plugin Lite may or may not be multiple of 8, therefore you have to extend it to be multiple of 8. You can use this code to do that by passing in the code that you obtain

def fixCodeLength(code):
    result = code
    while (len(result) % 8) != 0:
        rlen = len(result)
        finalLength = ((rlen / 8) * 8) + 8
        result += code
        result = result[:finalLength]
    return result

Finally after you pass the code into above function you can trigger the code in python using

your_device_object.send_data(your_code.decode('hex))

5. Voila!

Now can control your TC2 using python, see full example here

You can use this method to trigger other RF and IR devices that you have, you can learn more about functions of this module in its github pages.

I’ll write a follow up next week about how to integrate it with MQTT protocol, so you can have all in one app to control your smart home. Happy hacking!