Tuya Smart Life App

Latest version

Smart life, smart living. Remotely control home appliances from anywhere. Add and control multiple devices at once with one App. Voice control via Amazon Echo and Google Home. Interworking of multiple smart devices. Devices automatically start/stop working based on temperature. Otherwise download the app: Smart Life by tuya technology. 2) I opened the app on my phone. 3) In order to 'register' the device, it will try to scan for the Smart Socket. It couldn't find mine. I finally got this to work by clicking on the 'Electrical outlet' option at the top of the list. Nov 28, 2020 Tuyasmart app now supports security cameras, you're able to use it to watch live video through smartphone and tablets. The Tuyasmart team is developing the PC software that helps users to manage up to 64 channels of video feed on Windows and Mac computers. A highly secure and reliable smart control hub across protocols and categories. Energy-saving Build multi-category energy PaaS solutions to create an efficient and green life.

Released:

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

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.

Homekit

Follow the instructions below to get the Local_Key:

  1. 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).

  2. 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.

  3. 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)
  4. 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 from tinytuya.scanDevices() or when running python -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 run tuya-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

Life

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

Smart

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:

Tuya Smart Life App

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:

  1. PyCryptodome (recommended)
  2. PyCrypto
  3. 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 and scan interactive tools use ANSI color. This will work correctly in PowerShell but will show cryptic escape codes when run in Windows CMD. You can fix this by using the -nocolor option on tinytuya, or by changing the Windows CMD 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 IDFunction PointTypeRangeUnits
1SwitchboolTrue/False
2Countdown?integer0-86400s
4Currentinteger0-30000mA
5Powerinteger0-50000W
6Voltageinteger0-5000V

Version 3.1 - Light Type (RGB)

DP IDFunction PointTypeRangeUnits
1SwitchboolTrue/False
2Modeenumwhite,colour,scene,music
3Brightinteger10-1000*
4Color Tempinteger0-1000*
5Colorhexstringr:0-255,g:0-255,b:0-255,h:0-360,s:0-255,v:0-255rgb+hsv

Version 3.3 Devices

Version 3.3 - Plug, Switch, Power Strip Type

DP IDFunction PointTypeRangeUnits
1Switch 1boolTrue/False
2Switch 2boolTrue/False
3Switch 3boolTrue/False
4Switch 4boolTrue/False
5Switch 5boolTrue/False
6Switch 6boolTrue/False
7Switch 7/usbboolTrue/False
9Countdown 1integer0-86400s
10Countdown 2integer0-86400s
11Countdown 3integer0-86400s
12Countdown 4integer0-86400s
13Countdown 5integer0-86400s
14Countdown 6integer0-86400s
15Countdown 7integer0-86400s
17Add Electricityinteger0-50000kwh
18Currentinteger0-30000mA
19Powerinteger0-50000W
20Voltageinteger0-5000V
21Test Bitinteger0-5n/a
22Voltage coeinteger0-1000000
23Current coeinteger0-1000000
24Power coeinteger0-1000000
25Electricity coeinteger0-1000000
26Faultfaultov_cr

Version 3.3 - Dimmer Switch

DP IDFunction PointTypeRangeUnits
1SwitchboolTrue/False
2Brightnessinteger10-1000*
3Minimum of Brightnessinteger10-1000*
4Type of light source1enumLED,incandescent,halogen
5Modeenumwhite

Version 3.3 - Light Type (RGB)

DP IDFunction PointTypeRangeUnits
20SwitchboolTrue/False
21Modeenumwhite,colour,scene,music
22Brightinteger10-1000*
23Color Tempinteger0-1000
24Colorhexstringh:0-360,s:0-1000,v:0-1000hsv
25Scenestringn/a
26Left timeinteger0-86400s
27Musicstringn/a
28Debuggerstringn/a
29Debugstringn/a

Version 3.3 - Automated Curtain Type

DP IDFunction PointTypeRangeUnits
1Curtain Switch 1enumopen, stop, close, continue
2Percent control 1integer0-100%
3Accurate Calibration 1enumstart, end
4Curtain Switch 2enumopen, stop, close, continue
5Percent control 2integer0-100
6Accurate Calibration 2enumstart, end
8Motor Steer 1enumforward, back
9Motor steer 2enumforward, back
10Quick Calibration 1integer1-180s
11Quick Calibration 2integer1-180s
12Motor Mode 1enumstrong_power, dry_contact
13Motor Mode 2enumstrong_power, dry_contact
14Light modeenumrelay, pos, none

Version 3.3 - Fan Switch Type

DP IDFunction PointTypeRangeUnits
1Fan switchboolTrue/Falsen/a
2Fan countdowninteger0-86400s
3Fan speedenumlevel_1, level_2, level_3, level_4, level_5
4Fan speedinteger1-100%
5Fan light switchboolTrue/False
6Brightness integerinteger10-1000
7Fan light countdowninteger0-86400
8Minimum brightnessinteger10-1000
9Maximum brightnessinteger10-1000
10Modeenumwhite
11Power-on state settingenumoff, on, memory
12Indicator status settingenumnone, relay, pos
13Backlight switchboolTrue/False

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 IDFunction PointTypeRangeUnits
1Door SensorboolTrue/False
2Battery level stateenumlow, middle, high
3Battery levelinteger0-100%
4Temper alarmboolTrue/False
5Flooding Detection Stateenumalarm, normal
6Luminance detection stateenumlow, middle, high, strong
7Current Luminanceinteger0-100%
8Current Temperatureinteger400-2000
9Current Humidityinteger0-100%
10Shake Stateenumnormal, vibration, drop, tilt
11Pressure Stateenumalarm, normal
12PIR stateenumpir, none
13Smoke Detection Stateenumalarm, normal
14Smoke valueinteger0-1000
15Alarm Volumeenumlow, middle, high, mute
16Alarm Ringtoneenum1, 2, 3, 4, 5
17Alarm Timeinteger0-60s
18Auto-DetectboolTrue/False
19Auto-Detect Resultenumchecking, check_success, check_failure, others
20PreheatboolTrue/False
21Fault Alarmfaultfault, serious_fault, sensor_fault, probe_fault, power_faultBarrier
22LifecycleboolTrue/False
23Alarm SwitchboolTrue/False
24SilenceboolTrue/False
25Gas Detection Stateenumalarm, normal
26Detected Gasinteger0-1000
27CH4 Detection Stateenumalarm, normal
28CH4 valueinteger0-1000
29Alarm stateenumalarm_sound, alarm_light, alarm_sound_light, normal
30VOC Detection Stateenumalarm, normal
31VOC valueinteger0-999
32PM2.5 stateenumalarm, normal
33PM2.5 valueinteger0-999
34CO stateenumalarm, normal
35CO valueinteger0-1000
36CO2 Detection Stateenumalarm, normal
37CO2 valueinteger0-1000
38Formaldehyde Detection Stateenumalarm, normal
39CH2O valueinteger0-1000
40Master modeenumdisarmed, arm, home, sos
41Air quality indexenumlevel_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

Tuya Camera App

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

1.2.4

1.2.3

1.2.2

1.2.1

1.2.0

1.1.4

Smart Life App Download

1.1.3

1.1.2

1.1.1

1.1.0

1.0.5

Tuya Smart Life App

1.0.4

1.0.3

1.0.2

1.0.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Smart Life Login

Files for tinytuya, version 1.2.6
Filename, sizeFile typePython versionUpload dateHashes
Filename, size tinytuya-1.2.6-py2.py3-none-any.whl (35.2 kB) File type Wheel Python version py2.py3 Upload dateHashes

Tuya Smart Life App User Manual

Close

Tuya Smart App For Windows

Hashes for tinytuya-1.2.6-py2.py3-none-any.whl

Tuya Smart App Download

Hashes for tinytuya-1.2.6-py2.py3-none-any.whl
AlgorithmHash digest
SHA256a7ddaa04de9c13c7ee49290ec1163414a6f9e52c52061b31a5ce95260d8fef32
MD5877b08ddc12469104e34f28777fd1d32
BLAKE2-2563350bc6570a718de4e33e2c557b862f36aa02ba694231259c65036e7075eef86