Latest version
- Tuya Smart Life App Ios
- Tuya Smart Home
- Smart Life Devices
- Tuya Smart Life App Store
- Smart Life App Windows 10
Released:
Jun 10, 2020 That switch uses the smartlife app. There are many other inexpensive Chinese Wi-Fi devices that also use the same app. In May2020, SmartLife dropped their IFTTT channel due to increasing costs. Paul Hibbard, a tech blogger, says you can try the “brilliant smart“ app from Australia instead, as it has its own Ifttt channel. That company told him they recently renewed with Ifttt, so he says. Adding to the answer from Muddi, you can also try using the Tuya smart app that is identical to the Smart life app and has been known to work for some users before. Please have a look at the old forum thread linked below for more information about this.
Python module to interface with Tuya WiFi smart devices
Project description
Python module to interface with Tuya WiFi smart devices
Description
This python module controls and monitors Tuya compatible WiFi Smart Devices (Plugs, Switches, Lights, Window Covers, etc.) using the local area network (LAN). This is a compatible replacement for the pytuya
PyPi module.
Tuya devices are designed to communicate with the TuyaCloud but most also expose a local area network API, allowing us to directly control the devices without using the cloud. This python module provides a socket based way to poll status and issue commands to these devices.
NOTE: This module requires the devices to have already been activated by Smart Life App (or similar).
TinyTuya Setup
Install pip and python modules if you haven't already.
The PyPI module will attempt to install pycryptodome
if you don't have it. The modules pycrypto
, Crypto
or pyaes
could be used instead.
Tuya Device Preparation
Controlling and monitoring Tuya devices on your network requires the following:
- Address - The network address (IPv4) of the device e.g. 10.0.1.100
- Device ID - The unique identifier for the Tuya device
- Version - The Tuya protocol version used (3.1 or 3.3)
- Local_Key - The security key created to encrypt and decrypt communication. Devices running the latest protocol version 3.3 (e.g. Firmware 1.0.5 or above) will require a device Local_Key to read the status. Both 3.1 and 3.3 devices will require a device Local_Key to control the device.
Network Scanner
TinyTuya has a built in network scanner that can be used to find Tuya Devices on your local network. It will show Address, Device ID and Version for each device.
Setup Wizard
TinyTuya has a built in setup Wizard that uses the Tuya IoT Cloud Platform to generate a JSON list (devices.json) of all your registered devices. This includes the secret Local_Key as well as the Name of each device.
Follow the instructions below to get the Local_Key:
Download the 'Smart Life' App, available for iPhone or Android. Pair all of your Tuya devices (this is important as you cannot access a device that has not been paired).
Run the TinyTuya scan to get a list of Tuya devices on your network along with their device Address, Device ID and Version number (3.1 or 3.3):
NOTE: You will need to use one of the displayed Device IDs for step 4.
Set up a Tuya Account:
- Create a Tuya Developer account on iot.tuya.com and log in.
- Click on 'Cloud' icon -> Create a project (remember the Authorization Key: API ID and Secret for below)
- Click on 'Cloud' icon -> select your project -> Project Overview -> Linked Device -> Link devices by App Account (tab)
- Click 'Add App Account' and it will display a QR code. Scan the QR code with the Smart Life app on your Phone (see step 1 above) by going to the 'Me' tab in the Smart Life app and clicking on the QR code button [..] in the upper right hand corner of the app. When you scan the QR code, it will link all of the devices registered in your 'Smart Life' app into your Tuya IoT project.
- IMPORTANT Under 'API Management' -> 'API Products' and ensure the API groups have status 'Subscribed': Smart Home Devices Management, Authorization and Smart Home Family Management (see screenshot here) - Make sure you authorize your Project to use these 3 API groups:
- Click each of the API boxes
- Click 'Projects' tab
- Click 'New Authorization' button
- Select your Project from the dropdown and click OK (see screenshot here)
Run Setup Wizard:
- From your Linux/Mac/Win PC run the TinyTuya Setup Wizard to fetch the Local_Keys for all of your registered devices:
- The Wizard will prompt you for the API ID key, API Secret, API Region (us, eu, cn or in) from your Tuya IoT project noted above. It will also ask for a sample Device ID. Use one from step 2 above or found in the Device List on your Tuya IoT project.
- The Wizard will poll the Tuya IoT Cloud Platform and print a JSON list of all your registered devices with the 'name', 'id' and 'key' of your registered device(s). The 'key's in this list are the Devices' Local_Key you will use to access your device.
- In addition to displaying the list of devices, Wizard will create a local file
devices.json
. TinyTuya will use this file to provide additional details to scan results fromtinytuya.scanDevices()
or when runningpython -m tinytuya
to scan your local network. - The Wizard will ask if you want to poll all the devices. If you do, it will display the status of all devices on records and create a
snapshot.json
file with the results.
Notes:
- If you ever reset or re-pair your smart devices, the Local_Key will be reset and you will need to repeat the steps above.
- The TinyTuya Wizard was inspired by the TuyAPI CLI which is an alternative way to fetch the Local_Keys:
npm i @tuyapi/cli -g
and runtuya-cli wizard
- For a helpful video walk-through of getting the Local_Keys you can also watch this great Tech With Eddie YouTube tutorial: https://youtu.be/oq0JL_wicKg.
Programming with TinyTuya
After importing tinytuya, you create a device handle for the device you want to read or control. Here is an example for a Tuya smart switch or plug:
TinyTuya Module Classes and Functions
TinyTuya Error Codes
Starting with v1.2.0 TinyTuya functions will return error details in the JSON data responses instead of raising exceptions. The format for this response:
The 'Err' number will be one of these:
- 900 (ERR_JSON) - Invalid JSON Response from Device
- 901 (ERR_CONNECT) - Network Error: Unable to Connect
- 902 (ERR_TIMEOUT) - Timeout Waiting for Device
- 903 (ERR_RANGE) - Specified Value Out of Range
- 904 (ERR_PAYLOAD) - Unexpected Payload from Device
- 905 (ERR_OFFLINE) - Network Error: Device Unreachable
- 906 (ERR_STATE) - Device in Unknown State
- 907 (ERR_FUNCTION) - Function Not Supported by Device
Example Usage
See the sample python script test.py for an OutletDevice example or look in the examples directory for other scripts.
Encryption notes
These devices uses AES encryption which is not available in the Python standard library. There are three options:
- PyCryptodome (recommended)
- PyCrypto
- pyaes (note Python 2.x support requires https://github.com/ricmoo/pyaes/pull/13)
Scan Tool
The function tinytuya.scan()
will listen to your local network (UDP 6666 and 6667) and identify Tuya devices broadcasting their Address, Device ID, Product ID and Version and will print that and their stats to stdout. This can help you get a list of compatible devices on your network. The tinytuya.deviceScan()
function returns all found devices and their stats (via dictionary result).
You can run the scanner from the command line using this:
By default, the scan functions will retry 15 times to find new devices. If you are not seeing all your devices, you can increase max_retries by passing an optional arguments (eg. 50 retries):
Troubleshooting
- Tuya devices only allow one TCP connection at a time. Make sure you close the TuyaSmart or SmartLife app before using TinyTuya to connect.
- Some devices ship with older firmware that may not work with TinyTuya. If you're experiencing issues, please try updating the device's firmware in the official app.
- The LOCAL KEY for Tuya devices will change every time a device is removed and re-added to the TuyaSmart app. If you're getting decrypt errors, try getting the key again as it might have changed.
- Some devices with 22 character IDs will require additional setting to poll correctly - here is an example:
- Windows 10 Users - TinyTuya
wizard
andscan
interactive tools use ANSI color. This will work correctly in PowerShell but will show cryptic escape codes when run in WindowsCMD
. You can fix this by using the-nocolor
option on tinytuya, or by changing the WindowsCMD
console registry to process ANSI escape codes by doing something like this:
Tuya Data Points - DPS Table
The Tuya devices send back data points (DPS) also called device function points, in a json string. The DPS attributes define the state of the device. Each key in the DPS dictionary refers to key value pair, the key is the DP ID and its value is the dpValue. You can refer to the Tuya developer platform for definition of function points for the products.
The following table represents several of the standard Tuya DPS values and their properties. It represents data compiled from Tuya documentation and self-discovery. Devices may vary. Feedback or additional data would would be appreciated. Please submit a Issue or Pull Request if you have additional data that would be helpful for others.
Version 3.1 Devices
Version 3.1 - Plug or Switch Type
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
1 | Switch | bool | True/False | |
2 | Countdown? | integer | 0-86400 | s |
4 | Current | integer | 0-30000 | mA |
5 | Power | integer | 0-50000 | W |
6 | Voltage | integer | 0-5000 | V |
Version 3.1 - Light Type (RGB)
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
1 | Switch | bool | True/False | |
2 | Mode | enum | white,colour,scene,music | |
3 | Bright | integer | 10-1000* | |
4 | Color Temp | integer | 0-1000* | |
5 | Color | hexstring | r:0-255,g:0-255,b:0-255,h:0-360,s:0-255,v:0-255 | rgb+hsv |
Version 3.3 Devices
Version 3.3 - Plug, Switch, Power Strip Type
Tuya Smart Life App Ios
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
1 | Switch 1 | bool | True/False | |
2 | Switch 2 | bool | True/False | |
3 | Switch 3 | bool | True/False | |
4 | Switch 4 | bool | True/False | |
5 | Switch 5 | bool | True/False | |
6 | Switch 6 | bool | True/False | |
7 | Switch 7/usb | bool | True/False | |
9 | Countdown 1 | integer | 0-86400 | s |
10 | Countdown 2 | integer | 0-86400 | s |
11 | Countdown 3 | integer | 0-86400 | s |
12 | Countdown 4 | integer | 0-86400 | s |
13 | Countdown 5 | integer | 0-86400 | s |
14 | Countdown 6 | integer | 0-86400 | s |
15 | Countdown 7 | integer | 0-86400 | s |
17 | Add Electricity | integer | 0-50000 | kwh |
18 | Current | integer | 0-30000 | mA |
19 | Power | integer | 0-50000 | W |
20 | Voltage | integer | 0-5000 | V |
21 | Test Bit | integer | 0-5 | n/a |
22 | Voltage coe | integer | 0-1000000 | |
23 | Current coe | integer | 0-1000000 | |
24 | Power coe | integer | 0-1000000 | |
25 | Electricity coe | integer | 0-1000000 | |
26 | Fault | fault | ov_cr |
Version 3.3 - Dimmer Switch
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
1 | Switch | bool | True/False | |
2 | Brightness | integer | 10-1000* | |
3 | Minimum of Brightness | integer | 10-1000* | |
4 | Type of light source1 | enum | LED,incandescent,halogen | |
5 | Mode | enum | white |
Version 3.3 - Light Type (RGB)
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
20 | Switch | bool | True/False | |
21 | Mode | enum | white,colour,scene,music | |
22 | Bright | integer | 10-1000* | |
23 | Color Temp | integer | 0-1000 | |
24 | Color | hexstring | h:0-360,s:0-1000,v:0-1000 | hsv |
25 | Scene | string | n/a | |
26 | Left time | integer | 0-86400 | s |
27 | Music | string | n/a | |
28 | Debugger | string | n/a | |
29 | Debug | string | n/a |
Version 3.3 - Automated Curtain Type
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
1 | Curtain Switch 1 | enum | open, stop, close, continue | |
2 | Percent control 1 | integer | 0-100 | % |
3 | Accurate Calibration 1 | enum | start, end | |
4 | Curtain Switch 2 | enum | open, stop, close, continue | |
5 | Percent control 2 | integer | 0-100 | |
6 | Accurate Calibration 2 | enum | start, end | |
8 | Motor Steer 1 | enum | forward, back | |
9 | Motor steer 2 | enum | forward, back | |
10 | Quick Calibration 1 | integer | 1-180 | s |
11 | Quick Calibration 2 | integer | 1-180 | s |
12 | Motor Mode 1 | enum | strong_power, dry_contact | |
13 | Motor Mode 2 | enum | strong_power, dry_contact | |
14 | Light mode | enum | relay, pos, none |
Version 3.3 - Fan Switch Type
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
1 | Fan switch | bool | True/False | n/a |
2 | Fan countdown | integer | 0-86400 | s |
3 | Fan speed | enum | level_1, level_2, level_3, level_4, level_5 | |
4 | Fan speed | integer | 1-100 | % |
5 | Fan light switch | bool | True/False | |
6 | Brightness integer | integer | 10-1000 | |
7 | Fan light countdown | integer | 0-86400 | |
8 | Minimum brightness | integer | 10-1000 | |
9 | Maximum brightness | integer | 10-1000 | |
10 | Mode | enum | white | |
11 | Power-on state setting | enum | off, on, memory | |
12 | Indicator status setting | enum | none, relay, pos | |
13 | Backlight switch | bool | True/False |
Tuya Smart Home
Version 3.3 - Sensor Type
Important Note:Battery-powered Tuya sensors are usually designed to stay in sleep mode until a state change (eg.open or close alert). This means you will not be able to poll these devices except in the brief moment they awake, connect to the WiFi and send their state update payload the the Tuya Cloud. Keep in mind that if you manage to poll the device enough to keep it awake, you will likely quickly drain the battery.
DP ID | Function Point | Type | Range | Units |
---|---|---|---|---|
1 | Door Sensor | bool | True/False | |
2 | Battery level state | enum | low, middle, high | |
3 | Battery level | integer | 0-100 | % |
4 | Temper alarm | bool | True/False | |
5 | Flooding Detection State | enum | alarm, normal | |
6 | Luminance detection state | enum | low, middle, high, strong | |
7 | Current Luminance | integer | 0-100 | % |
8 | Current Temperature | integer | 400-2000 | |
9 | Current Humidity | integer | 0-100 | % |
10 | Shake State | enum | normal, vibration, drop, tilt | |
11 | Pressure State | enum | alarm, normal | |
12 | PIR state | enum | pir, none | |
13 | Smoke Detection State | enum | alarm, normal | |
14 | Smoke value | integer | 0-1000 | |
15 | Alarm Volume | enum | low, middle, high, mute | |
16 | Alarm Ringtone | enum | 1, 2, 3, 4, 5 | |
17 | Alarm Time | integer | 0-60 | s |
18 | Auto-Detect | bool | True/False | |
19 | Auto-Detect Result | enum | checking, check_success, check_failure, others | |
20 | Preheat | bool | True/False | |
21 | Fault Alarm | fault | fault, serious_fault, sensor_fault, probe_fault, power_fault | Barrier |
22 | Lifecycle | bool | True/False | |
23 | Alarm Switch | bool | True/False | |
24 | Silence | bool | True/False | |
25 | Gas Detection State | enum | alarm, normal | |
26 | Detected Gas | integer | 0-1000 | |
27 | CH4 Detection State | enum | alarm, normal | |
28 | CH4 value | integer | 0-1000 | |
29 | Alarm state | enum | alarm_sound, alarm_light, alarm_sound_light, normal | |
30 | VOC Detection State | enum | alarm, normal | |
31 | VOC value | integer | 0-999 | |
32 | PM2.5 state | enum | alarm, normal | |
33 | PM2.5 value | integer | 0-999 | |
34 | CO state | enum | alarm, normal | |
35 | CO value | integer | 0-1000 | |
36 | CO2 Detection State | enum | alarm, normal | |
37 | CO2 value | integer | 0-1000 | |
38 | Formaldehyde Detection State | enum | alarm, normal | |
39 | CH2O value | integer | 0-1000 | |
40 | Master mode | enum | disarmed, arm, home, sos | |
41 | Air quality index | enum | level_1, level_2, level_3, level_4, level_5, level_6 |
NOTE (*) - The range can vary depending on the device. As an example, for dimmers, it may be 10-1000 or 25-255.
Tuya References
- Tuya Hardware Development - Protocol: https://developer.tuya.com/en/docs/iot/device-development/embedded-software-development/mcu-development-access/wifi-mcu-sdk-solution/tuya-cloud-universal-serial-port-access-protocol?id=K9hhi0xxtn9cb
- TuyaMCU listing of Tuya DP IDs: https://tasmota.github.io/docs/TuyaMCU/#switches-or-plugspower-strips
Credits
- TuyaAPI https://github.com/codetheweb/tuyapi by codetheweb and blackrozesFor protocol reverse engineering, additional protocol reverse engineering from jepsonrob and clach04
- PyTuya https://github.com/clach04/python-tuya by clach04The origin of this python module (now abandoned), nijave pycryptodome support and testing, Exilit for unittests and docstrings, mike-gracia for improved Python version support, samuscherer for RGB Bulb support, magneticflux for improved Python version support, sean6541 for initial PyPi package and Home Assistant support https://github.com/sean6541/tuya-homeassistant, ziirish - for resolving a dependency problem related to version numbers at install time
- https://github.com/rospogrigio/localtuya-homeassistant by rospogrigioUpdated pytuya to support devices with Device IDs of 22 characters
Related Projects
- https://github.com/sean6541/tuyaapi Python API to the web api
- https://github.com/codetheweb/tuyapi node.js
- https://github.com/Marcus-L/m4rcus.TuyaCore - .NET
- https://github.com/SDNick484/rectec_status/ - RecTec pellet smokers control (with Alexa skill)
- https://github.com/TradeFace/tuyaface - Python Async Tuya API
Release historyRelease notifications | RSS feed
1.2.6
1.2.5
Smart Life Devices
1.2.4
1.2.3
1.2.2
1.2.1
1.2.0
1.1.4
Tuya Smart Life App Store
1.1.3
1.1.2
1.1.1
1.1.0
1.0.5
1.0.4
1.0.3
1.0.2
1.0.0
Download files
Smart Life App Windows 10
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Filename, size | File type | Python version | Upload date | Hashes |
---|---|---|---|---|
Filename, size tinytuya-1.2.6-py2.py3-none-any.whl (35.2 kB) | File type Wheel | Python version py2.py3 | Upload date | Hashes |
Hashes for tinytuya-1.2.6-py2.py3-none-any.whl
Algorithm | Hash digest |
---|---|
SHA256 | a7ddaa04de9c13c7ee49290ec1163414a6f9e52c52061b31a5ce95260d8fef32 |
MD5 | 877b08ddc12469104e34f28777fd1d32 |
BLAKE2-256 | 3350bc6570a718de4e33e2c557b862f36aa02ba694231259c65036e7075eef86 |