Integrating TechLife Pro light bulbs without opening or soldering

Hello everyone,

After much reading (notably here and here), I finally managed to hack together a solution that works quite well to get TechLife Pro light bulbs (such as these ones or these ones) to play nice with Home Assistant, without physically opening up the light bulbs or otherwise modifying their hardware or software.

This tutorial is still a work in progress, so please bear with me and let me know if something is incorrect or isn’t working for you.

0. Prerequisites

For the purposes of this guide, I will assume that you are running HA inside Docker. I also assume that you are familiar with the command line and with general tech speak.

Make sure that your HA installation is up and running, and has a working MQTT broker (such as the excellent core_mosquitto addon), both installed and running.

1. Create a working directory in the homeassistant container

Run the following command from inside the main HA Docker container:

mkdir -p /config/custom_components/TechLife

2. Copy necessary files from the mosquitto_core container to the homeassistant container

Copy the following files from the mosquitto_core container to homeassistant container, into the directory that you created in step 1:

  • libcares.so.2
  • libcares.so.2.3.0
  • libcrypto.so.1.0.0
  • libmosquitto.so.1
  • libssl.so.1.0.0
  • mosquitto_pub

You can try executing the mosquitto_pub binary from your working directory inside the homeassistant container. However, it should give an error about missing libraries at this point; we’ll take care of this later on.

3. Create handler script and add it to HA

We’re going to create a script that handles and sends event to the light bulbs. Save this file in the directory from step 1.

TechLife.sh:

#!/bin/bash

t="dev_sub_""$1"
t=$(echo $t | tr '[:upper:]' '[:lower:]')

cp /config/custom_components/TechLife/*.so* /usr/lib

if [ "$2" != "" ]; then
  if [ "$2" = "on" ]; then
    echo -en "\xfa\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x23\xfb" | /config/custom_components/TechLife/mosquitto_pub -t $t -s -h 10.0.1.11 -u "mqttusernamehere" -P "mqttpasswordhere"
  elif [ "$2" = "off" ]; then
    echo -en "\xfa\x24\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x24\xfb" | /config/custom_components/TechLife/mosquitto_pub -t $t -s -h 10.0.1.11 -u "mqttusernamehere" -P "mqttpasswordhere"
  elif [ "$2" = "dim" ]; then
    echo -en "\xfa\x23\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x23\xfb" | /config/custom_components/TechLife/mosquitto_pub -t $t -s -h 10.0.1.11 -u "mqttusernamehere" -P "mqttpasswordhere"
    d=$(expr $3 \* 100)
    prefix='28'
    oneToSix='000000000000'
    sevenToTen=$(printf %08X "$d" | grep -o .. | tac | tr "\n" "," | sed "s/,//g")
    elevenToThirteen='0000f0'
    oneToThirteen=$(echo $oneToSix$sevenToTen$elevenToThirteen)
    oneToThirteenMod=$(echo $oneToThirteen | sed 's/../0x&^/g' | sed 's/.$//')
    xor=$(python3 -c "print(hex($oneToThirteenMod)[2:].lower())")
    suffix='29'
    k=$(echo $prefix$oneToSix$sevenToTen$elevenToThirteen$xor$suffix | sed 's/../\\x&/g' | tr -d "\n")
    echo -en $k | /config/custom_components/TechLife/mosquitto_pub -t $t -s -h 10.0.1.11 -u "mqttusernamehere" -P "mqttpasswordhere"
  fi
else
  echo "Usage: "
  echo "$0 [MAC] on"
  echo "$0 [MAC] off"
  echo "$0 [MAC] dim [0-100]"
fi

Add the script to your configuration.yaml in order to expose it to HA.

shell_command:
  techlife: bash /config/custom_components/TechLife/TechLife.sh {{ mac }} {{ action }} {{ level }}

4. Connect the light bulb to your 2.4GHz Wifi network

There is no need to install the TechLife app to do this. The app is poorly designed and does not work most of the time. Instead, follow these simple steps.

  1. Modify the following variables in the excellent Python script included below (taken from here)

    • ssid : Your 2.4GHz Wifi network’s SSID
    • password : Your 2.4GHz Wifi network’s password
    • bssid : Your 2.4GHz Wifi network’s BSSID (MAC address) written as a byte array
  2. Screw in the light bulb and turn it on

  3. Connect to the Wifi network created by the light bulb

  4. Execute the Python script

At this point, the bulb will reboot, connect to your Wifi network, and automatically start listening for MQTT commands.

Take note of the output that the script spits out; this is your light bulb’s MAC address, which you’ll need for the next steps.

TechLifePro_Setup.py:

#!/usr/bin/env python
# 1. Modify the variables according to your setup: ssid, password, bssid, [email]
# 2. Connect the computer to AP-TechLife-xx-xx SSID
# 3. Run the script
import socket

# Variables to change
ssid = 'YOURSSID'
password = 'WIFIPASSWORD'
bssid = bytearray([0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa]) # Enter your WiFi router's WiFi interface MAC address in hex (eg. AA:AA:AA:AA:AA:AA)
email = '[email protected]' # not absolutely required

# The bulb's network details
TCP_IP = '192.168.66.1'
TCP_PORT = 8000
BUFFER_SIZE = 1024

# Initialize Payload
payload = bytearray(145)
payload[0x00] = 0xff
payload[0x69] = 0x01

# Add the SSID to the payload
ssid_start = 0x01
ssid_length = 0
for letter in ssid:
    payload[(ssid_start + ssid_length)] = ord(letter)
    ssid_length += 1

# Add the WiFi password to the payload
pass_start = 0x22
pass_length = 0
for letter in password:
    payload[(pass_start + pass_length)] = ord(letter)
    pass_length += 1

# Add the BSSID to the payload
bssid_start = 0x63
bssid_length = 0
for digit in bssid:
    payload[(bssid_start + bssid_length)] = digit
    bssid_length += 1

# Add the email to the payload
email_start = 0x6a
email_length = 0
for letter in email:
    payload[(email_start + email_length)] = ord(letter)
    email_length += 1

checksum = 0
j = 1
while j < 0x8f:
   checksum = (payload[j] ^ checksum)
   checksum = checksum & 0xff
   j += 1

payload[0x8e] = 0xf0
payload[0x8f] = checksum & 0xff
payload[0x90] = 0xef

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
s.send(payload)
data = s.recv(BUFFER_SIZE)
s.close()

print ("received data:", data)

5. Configure the bulbs to talk to your MQTT server instead of the TechLife MQTT server

The TechLife Pro light bulbs are configured to listen to a specific MQTT topic on one of two specific external domains. We therefore need to instruct the light bulbs to listen to your server instead.

Using a Python 2.7 compiler (either on your computer, or using an online service such as this one), run the following script, replacing the values in the first two lines with the appropriate values in your case.

mqttServerIp = '10.0.1.11'
bulbMacAddress = 'aa:bb:cc:dd:ee:ff'

###############################################

def calcChecksum(stream):
    checksum = 0
    for i in range(1, 14):
        checksum = checksum ^ stream[i]
    stream[14] = checksum & 255

    return bytearray(stream)


def changeIP (ipAddr, port):
    Command = bytearray.fromhex("AF 00 00 00 00 00 00 f0 00 00 00 00 00 00 00 b0")
    l = list(Command)
    idx = 1
    for ip in map(int,ipAddr.split('.')):
        l[idx] = ip
        idx = idx + 1
    l[5] = port & 0xff
    l[6] = port >> 8
    return calcChecksum(l)
    
command = '\\x' + '\\x'.join(format(x, '02x') for x in changeIP(mqttServerIp,1883))

print 'echo -en "' + command + '" | mosquitto_pub -t "dev_sub_' + bulbMacAddress.lower() + '" -h "cloud.qh-tek.com" -s'
print 'echo -en "' + command + '" | mosquitto_pub -t "dev_sub_' + bulbMacAddress.lower() + '" -h "cloud.hq-tek.com" -s'

The result should be something like this:

echo -en "\xaf\x0a\x00\x01\x0b\x5b\x07\xf0\x00\x00\x00\x00\x00\x00\xac\xb0" | mosquitto_pub -t "dev_sub_aa:bb:cc:dd:ee:ff" -h "cloud.qh-tek.com" -s
echo -en "\xaf\x0a\x00\x01\x0b\x5b\x07\xf0\x00\x00\x00\x00\x00\x00\xac\xb0" | mosquitto_pub -t "dev_sub_aa:bb:cc:dd:ee:ff" -h "cloud.hq-tek.com" -s

Copy/paste these two lines on the command line and execute them; at least one of them should be successful. This tells the overseas MQTT server to instruct the bulb to use your local MQTT server from now on.

The bulb should flash once after a few seconds to confirm that it has received the command.

6. Configure HA

Create the following user in HA with the (terrible) username and password combination that is hardcoded into the light bulbs, which will enable the MQTT auth to go through seamlessly.

  • Name: TechLife MQTT
  • Username: testuser
  • Password: testpassword

Additionally, add the following to your configuration.yaml.
Replace the MAC address with the one that was output by the script in step 4.

light:
  - platform: template
    lights:
      bedroom:
        friendly_name: "Light's friendly name"
        turn_on:
          service: shell_command.techlife
          data:
            action: "on"
            mac: "aa:bb:cc:dd:ee:ff"
        turn_off:
          service: shell_command.techlife
          data:
            action: "off"
            mac: "aa:bb:cc:dd:ee:ff"
        set_level:
          service: shell_command.techlife
          data_template:
            action: "dim"
            level: "{{ (brightness / 255 * 100) | round }}"
            mac: "aa:bb:cc:dd:ee:ff"

7. Restart HA

Restart HA and try out your newly created light entity! Everything should just work at this point.

Conclusion

If ever you want to add additional light bulbs, simply repeat steps 4 to 7.

As I mentioned earlier, this tutorial is a WIP, so let me know if you encounter any hiccups or if you have any questions. So far I have 4 TechLife Pro light bulbs, all working smoothly.

Enjoy!

8 Likes

“Hello! It’s not clear where to get mosquito_pub?”

Understood!). Thanks for the work! I bow to you! in the sh file it is necessary to register not the username, but testuser, respectively!
Hassio does not need a username and password, it must be entered in mqtt brouker.

Good afternoon. I use HA CORE (not HASSIO)

Step 2
Container --> eclipse-mosquitto
Putty—> docker ps -a = ID_CONTAINER --> docker exec -it ID_CONTAINER /bin/sh

  • libcares.so.2 ----> I don’t find
  • libcares.so.2.3.0 ----> I don’t find
  • libcrypto.so.1.0.0 ----> libcrypto.so.43.0.1
  • libmosquitto.so.1 ----> OK
  • libssl.so.1.0.0 ----> libssl.so.45.0.1
  • mosquitto_pub----> OK

Were can i find the other files?

EDIT: Could you upload the mosquitto_core files?

EDIT2: Could your method be used with other bulbs (Magic Home smart-home) that use the same chip as Techlife?

The “Magic home” could not be started according to the same instructions.
in wifi network is not detected, how it works is not clear.
appears on the network after synchronization with the native program

Thank you. I’ll be watching to see if it can be integrated into Home Assistant

It’s not a guarantee that you need exactly those files; to know for sure which files are missing for your version of mosquitto_pub to run, you can try running the mosquitto_pub binary that you copied into your HA container/environment and seeing if it complains of missing libraries, which could then be copied from your eclipse_mosquitto container into your working directory.

As for Magic Home bulbs, this method would only work if they used the same communication method (MQTT). From what I’ve seen so far, Magic Home bulbs tend to use Tuya servers rather than MQTT commands, but I might be wrong. If they do in fact use MQTT commands, it should be trivial to find out what server they are listening to in order to DNS-masquerade it. If it uses the Tuya or Smart Life apps, and is not compatible with tuya-convert, then you’re pretty much without any recourse for now, other than setting up the Tuya integration in HA.

1 Like

Thank you. I have made a script in Python that I run from sh (docker HA exec -it … and then sh) and it works fine (on, off and dim), but I don’t know how to integrate it into HA.

Would you know how to use mqtt_light even if it’s ON and OFF only?

As for the bulbs (Magic Home-Smart Home) they do not use esp so they cannot be flashed with tuya-convert (I have already tried it, they have another chip).

My idea is to redirect the host (captured by pihole) to my mqtt 1883 and see what I can capture)

I have downloaded the app and disassembled it to see if I can find any command for ON OFF and DIM and if there is a user and password

1 Like

Hi all, I have these bulbs and foolishly didn’t even think about how to Integrate to HA

I got the ones that you Magic Home-Smart Him and the broadlinkprov chip.

Any updates on a workaround or do I bit the bullet and get tuya compatible bulbs?

Nothing to do with Magic Home-Smart Home…

Hi all, I’ve got 4 of this cheap (but tricky) bulbs. As far as I understand, my router (ZTE F680) doesn’t redirect DNS requests, and even worst, I can’t find the mosquito_pub file! (I’ve got mosquito-conf, mosquitto.db, mosquito_pub.1 and even mosquito_pub.1.meta… but I tried 4 times on the mosquitto_core container and i couldn’t find “mosquitto_pub”.

So, I would like to ask 2 questions:

1.- Can I use a mosquito_pub file from someone else? Or it has to be exactly the one on my cointainer? Or perhaps there’s another trick to get my own file.

2.- Maybe I can’t redirect DNS on my router, but… what about if I do it on my laptop? I have a laptop running Lubuntu 19 with docker and Home Assistant, and a WIFI USB so I can use it as an access point (actually now is working: I can connect but only locally, I can see HA but no Google)… is there a way to connect all the bulbs to this local hotspot and somehow redirect it to Home Assistant?

It could be a workaround…mosquitto

Just in case, I’ve modified the /etc/hosts in my Lubuntu, trying to redirect TechLife DNS requests’

Here is how it looks the etc/hosts

cloud.qh-tek.com 127.0.0.1
cloud.hq-tek.com 127.0.0.1

I also changed /etc/NetworkManager/dnsmasq.d/hosts.conf like this:

address=/cloud.qh-tek.com/10.0.1.11
address=/cloud.hq-tek.com/10.0.1.11

I’ve got the mosquitto_pub file installing mosquitto with sudo snap install mosquitto and copy it from /snap/mosquitto/269/usr/bin

Then, I tried to change the python script, first I didn’t understand the syntax of the BSSD (the access point I did with a wifi USB) inside the phyton file, but now I think it’s like this:

bssid = bytearray([0x11, 0x11, 0x11, 0x11, 0x11, 0x11]) ...if it's 11:11:11:11:11

And when I connect my laptop to the bulb… I’ve got an answer!

python3 TechLifePro_Setup.py

received data: b'HW:\x01\xf1MAC:XXXXXXXXXX'

So I replaced the MAC in the example above, pasted into configuration.yaml in Home Assistant, and restarted… but it doesn’t work. :frowning:

Maybe the redirection that I tried on hosts and dnsmasq is wrong. Or the mosquitto_pub file that I got in the snap installation is not the right one .

Any ideas? I guess the error is in the redirection or in mosquitto_pub.

Update 1: When I manually turn on the bulb, I get this in the MQTT brocker inside Home Assistant:

1587996758: New connection from 10.0.60.xx on port 1883.
[INFO] found testuser on Home Assistant
1587996758: New client connected from 10.0.60.xx as clientid[Bulb’s MAC] (p2, c1, k30, u’testuser’).
So, it’s recognized by HA, but somehow, it doesn’t work

Update 2:
When I run TechLife.sh on the command line I’ve got this:
syntax error near element elif’ líne: elif [ "$2" = "off" ]; then

I use

shell_command:
techlife_on: python3 /config/custom_components/TechLife/techlifebulb.py AA:73:EE:____________ on
techlife_off: python3 /config/custom_components/TechLife/techlifebulb.py AA:73_______________ off

I would like to try… Where do I get the code for ‘techlifebulb.py’ ?

Get the code from here https://gist.github.com/csabavirag/334d9fa4028c17a69e3af4ea22838381#file-techlifepro_setup-py

Also you may follow the OpenHAB thread as well if you want to see the MQTT messages for inserting the local MQTT ip address (so no DNS redirection is required) as well as the way to generate color MQTT message)
https://community.openhab.org/t/hacking-techlife-pro-bulbs/85940

1 Like

Hey there, I am trying to understand your tutorial. I thought, I could use the mosquitto Addon.
But after the installation of the Addon, I am not able to find the listed files.
(I am running Hassio (supervised) on Raspbian.)

Am I correct that I have to install eclipse_mosquitto (in docker) additionally and pull the files from there?
Does the MQTT Broker has to run always or do I only have to copy the files from it?

Well the tutorial is from a different person @csabavirag - so the credit goes to him.
You can use any MQTT broker to route the TechLife Pro bulbs, however, you would need to connect to cloud.qh-tek.com (assuming that you have already followed the initial installation steps) and issue a payload to have your device connect to your MQTT broker. Once you have successfully redirected, than you can run the various MQTT commands I have listed in the post to control the lights

Thanks for your reply. I guess I figured most of this out. MQTT was absolutely new for me.

Does anybody know, if the MQTT Light template allows custom commands/payload?
Would be awesome, but I am quite new to home assistant and everything and haven’t found anything specific on this topic on the documentation page.
At least nothing that I was able to understand quickly :stuck_out_tongue:
Or are the payloads for on/off etc. always the same?

Hi Everyone,
i’ve got stuck at the end of this setup and probably am missing something obvious but can’t come up with what that may be on my own.
My led stripe registers to my MQQT broker properly, but i have problem with sh script.
IF i will use mosquito_pub instead of providing paths to it in custom_components folder, i am able to run the script and turn on and off my led stripe but it won’t work from hassio after setup. I decided to fall back to what was written in the instruction so copied my mosquitto_pub and all necessary files into Techlife folder to check whether it changes anything but unfortunately in both versions my script ends up with

Last logged: 11:13:51 AM

Error running command: `bash custom_components/TechLife/TechLife.sh {{ mac }} {{ action }} {{ level }}`, return code: 127

NoneType: None

Anyone could point me into right direction ? I’m afraid i’m stuck for good :frowning:

I had similar problems. I ended up writing a AppDaemon script. If you want to go in that route let me know. Will cleanup my code and share