by P. Andreas Schmidt, IVE
Version 3 (October 5, 2022)
Download (249 downloads)
Howto:
-First you need to request an access token (login to API) through http request. Since this part works with parameters (not JSON data) you have to put the info into the url, as in block 18. Just change those parts with server, username, password and country code (see the countries dictionary, or else after running the flow check the logs). You're not allowed to request more than one per 60 secs.
And even though it's run through https I could intercept with pcapdump and it's mitm add-on the requests and read username and password that were sent, so not as safe as Smart Life (which uses the cloud IoT service and at least with the same app couldn't get that info). So be careful where you use this, apart from storing your username and password into the flow.
-The JSON received apart from the access token has a refresh token and a time frame for the validity of the first (24h). Once the time is over you should request a refresh, see block 33.
Another possible security issue is that it doesn't matter whether you ask for refresh from a different IP, meaning that if someone got your token they could login w/o needing username or pwd.
-Then you can send a discovery request, as in block 19. No need to change anything there. The JSON contains (in theory) all devices as well as the scenes you defined (strangely there is a scenes element in the JSON but it's empty (all libraries I've seen ignore it), so they're included in the devices element).
In my case my one lamp does not show, only scenes, no idea why...
You get the name, id, the type of device (there are two, the dev_type, as well as a "ha_type", it seems for the home automation app? But other implementations don't seem to use it), and importantly a data element with the states (i.e. a lamp thus color brightness, a Aircon temperature, etc... Whether they're online,...)
-Then there is also a query request (see block 82) you can send to get more recent status of any device (not scenes, nothing to get there). This is limited to once per 180 seconds.
-Last but not least you can send control commands through http (see block 30) to turn on/off devices, execute scenes (so far almost implemented here, only turns on), change states (brightness, color, ...) (To be done yet, though I doubt I will)
At this moment only minimum error checks are done (without details), more could or should to be added.
Updates:
-corrected the added error checks
-added a status query http block
-added more info to description
-improved countries dictionary
-added some more error checking
-added check for expiration of token before requesting refresh
For your own study and experimentation check out:
In python but best sources:
https://github.com/milo526/homebridge-tuya-web/tree/master/src/api
https://pypi.org/project/tuyapy/#files
More basic but gives a good idea also how control a lamp eg:
https://github.com/ndg63276/smartlife/blob/master/functions.js
Website that uses that:
https://smartathome.co.uk/smartlife/index.html
5 stars | 2 | |
4 stars | 0 | |
3 stars | 0 | |
2 stars | 0 | |
1 star | 0 | |
Reports | 0 |
Rate and review within the app in the Community section.