Background
I have a hybrid home automation system that includes some old X10 devices, Philips hue devices, wemo devices, and tp-link devices. Part of the system is some software that emulates a Philips hue bridge (ha-bridge) that lets me control devices by running linux programs and scripts. This lets me use old X10 radio frequency controllers and motion detectors in my home automation. So far this has worked great.
I found an article on re-flashing tuya devices using tuya-convert and thought that it would be a neat idea to try. So I went to amazon and bought some inexpensive smart plugs that were tuya based.
Unfortunately, the tuya convert programs failed on my systems. After some time spent trying to debug it I threw up my hands and said “screw it.” I then went looking for an existing command line interface that would control the plugs. I found several, but again, none seemed to work simply and easily. So once again I threw up my hands and said “screw it.”
Solution
As part of the above efforts I went to the tuya IoT developers web site to get authorization keys. So I went back and found an demo showing the control of a device, written in python. You can see the page here: https://developer.tuya.com/en/demo/python-iot-development-practice
This page clearly documented the steps needed, and the python code. I downloaded the python code and opened it in an editor.
Lines 3 and 4 required replacing the demo client id and secret with the ones I got from tuya.
Line 13 needed to be changed to point to the us region.
Line 85 needed to be edited replacing the device id with the id of my device.
At that point I ran the code and it found my device, but it failed to control it. While I was tempted to throw up my hands and say “screw it”, I looked closely at the output, which showed the instruction set the smart plug used:
device instruction set
{
"result": {
"category": "cz",
"functions": [
{
"code": "switch_1",
"desc": "switch 1",
"name": "switch 1",
"type": "Boolean",
"values": "{}"
},
{
"code": "countdown_1",
"desc": "countdown 1",
"name": "countdown 1",
"type": "Integer",
"values": "{\"unit\":\"s\",\"min\":0,\"max\":86400,\"scale\":0,\"step\":1}"
}
]
}
Seeing that the “code” was named “switch_1”, I looked at the python code and saw that the string being sent to the device was:
d = {“commands”:[{“code”:”switch”,”value”:s},]}
I changed the “switch” to “switch_1” and it worked!
So now I had a template to use. I am modifying the python demo so that I can use a json configuration file to hold the keys and device id’s and names. With that I can simply call from the command line the command: python smartplug.py name on|off.
There is no python code on that page !?
Instructions for getting the Python code are at the link in the post: https://developer.tuya.com/en/demo/python-iot-development-practice.
Look for the line “Import code: pip3 install tuya-connector-python”. That is the pip command to download the code.
Bob