Smart Life / Tuya Gateway

Maxim Mityutko
3 min readJan 11, 2021

Why?

Imagine you have a modest smart home setup, maybe a few light bulbs and a few power sockets. Of course you can manage those with an app or voice assistant (if you are not to paranoid), but sometimes it’s much handier to use a switch or a button to interact with those things.

Plus your loved ones will stop bugging you about the need to carry the phone all the time, and stop randomly switching the power off on light bulbs, which is an absolute win!

So I got myself a set of Flic buttons to deal with the task. Each button can handle 3 actions: single click, double click and hold. But then I discovered that Flic does not support direct integration with a Smart Life app, which is generally not a problem, because they support IFTTT.

But… IFTTT recently cut down their free plan and now only 3 flows are allowed per account. Ok, so either you need to pay to get a proper subscription and capabilities, or create a free account for each button.

As an alternative, there is a number of open source tools that allow to interact with Tuya devices locally, one can even install Home Assistant and setup integrations in there, but in my opinion, using HASS is an overkill, unless you have an extensive smart home setup.

Luckily Flic cal also interact with Web APIs and of course Smart Life / Tuya has one. But you need some kind of a gateway to handle the authentication and, maybe, simplify a bit interaction with the API.

How?

Project requirements:

  • send commands to Tuya devices
  • handle Tuya authentication process
  • expose gateway API in local network
  • deploy to Docker on Raspberry PI

Tuya Setup

  1. Register at https://iot.tuya.com/
  2. Go to CloudProjectCreate
  3. Set your “Project Name”, “Description” and appropriate “Industry”
  4. Go to CloudAPI Group and apply the following groups: Authorization Management, Device Control, Device Management
  5. Go to CloudLinked DeviceLinked Devices by App AccountAdd App Account
  6. Scan the QR code with you Smart Life app (Meupper right corner) and confirm link
  7. Go to newly created project page, where “Client ID” and “Client Secret” are available

After that, all you need is the device_id that can be found on Device List tab (make sure to select correct region) or in the device details in Smart Life app.

Gateway Development

I chose connexion to quickly setup an API endpoint:

And expose three different methods:

  • (GET) /device/status — get general device information
  • (GET) /device/functions — get functions available for the specific device
  • (POST) /device/commands— send JSON payload with commands to specific device

When called, the method will trigger corresponding Python function that will handle the authentication and path the payload to Tuya API.

In order to handle the authentication, I took the code from tinytuyaby Jason A. Cox and modified it a little bit.

The most important part is generating correct headers for the request, they are used during initial authentication and subsequent interactions with the API:

And the method implementation:

Environment variables are used to store TUYA_CLIENT_ID and TUYA_CLIENT_SECRET that can be obtained during the setup phase, and TUYA_REGION depends on in what region the project was created on Tuya platform.

Deployment

The deployment is done to Raspberry PI 4 Model B that runs Docker. When the container is up and running, the endpoint is exposed on port 65080.

For instance to get the list of available functions:

In this case switch_led allows to turn device on and off and bright_value — control bulb brightness.

And to turn the device on:

The whole project is available here: https://github.com/maxim-mityutko/tuya-gateway

Just go through “Tuya Setup” part to obtain client id and secret, clone repository and replace the placeholders in secrets.env file, then run docker-compose up.

--

--