tinytuya module

Python module to interface with Tuya WiFi smart devices

Author: Jason A. Cox For more information see https://github.com/jasonacox/tinytuya

Functions

assign_dp_mappings(tuyadevices, mappings)

Adds DP mappings based on the Product ID

decrypt(msg, key)

decrypt_udp(msg[, key])

deviceScan([verbose, maxretry, color, poll, ...])

Scans your network for Tuya devices and returns dictionary of devices discovered

device_info(dev_id)

Searches the DEVICEFILE file for devices with ID == dev_id

encrypt(msg, key)

error_json([number, payload])

Return error details in JSON

find_device([dev_id, address])

Scans network for Tuya devices with either ID = dev_id or IP = address

merge_dps_results(dest, src)

Merge multiple receive() responses into a single dict

scan([maxretry, color, forcescan])

Scans your network for Tuya devices with output to stdout

set_debug([toggle, color])

Enable tinytuya verbose logging

Classes

AESCipher(key)

Wrapper for the imported crypto library

BulbDevice(*args, **kwargs)

Represents a Tuya based Smart Light/Bulb.

Cloud([apiRegion, apiKey, apiSecret, ...])

Interface with Tuya's Cloud IoT Platform

CoverDevice(*args, **kwargs)

Represents a Tuya based Smart Window Cover.

Device(dev_id[, address, local_key, ...])

MessagePayload(cmd, payload)

OutletDevice(dev_id[, address, local_key, ...])

Represents a Tuya based Smart Plug or Switch.

TuyaHeader(prefix, seqno, cmd, length, ...)

TuyaMessage(seqno, cmd, retcode, payload, ...)

XenonDevice(dev_id[, address, local_key, ...])

Low-level functions for devices.

Exceptions

DecodeError

Extras

const

Global Network and File Settings

crypto_helper

error_helper

Error messages and formatting

class tinytuya.AESCipher(key)

Bases: _AESCipher_pyca

Wrapper for the imported crypto library

CRYPTOLIB

Name of the crypto library being used.

Current possible values: pyca/cryptography, PyCrypto[dome[x]], or pyaes

Type:

str

CRYPTOLIB_HAS_GCM

Crypto library can do AES in GCM mode

Type:

bool

CRYPTOLIB_VER

The version string of the crypto library in use

Type:

str

exception tinytuya.DecodeError

Bases: Exception

class tinytuya.MessagePayload(cmd, payload)

Bases: tuple

cmd

Alias for field number 0

payload

Alias for field number 1

class tinytuya.TuyaHeader(prefix, seqno, cmd, length, total_length)

Bases: tuple

cmd

Alias for field number 2

length

Alias for field number 3

prefix

Alias for field number 0

seqno

Alias for field number 1

total_length

Alias for field number 4

class tinytuya.TuyaMessage(seqno, cmd, retcode, payload, crc, crc_good, prefix, iv)

Bases: tuple

cmd

Alias for field number 1

crc

Alias for field number 4

crc_good

Alias for field number 5

iv

Alias for field number 7

payload

Alias for field number 3

prefix

Alias for field number 6

retcode

Alias for field number 2

seqno

Alias for field number 0

tinytuya.assign_dp_mappings(tuyadevices, mappings)

Adds DP mappings based on the Product ID

The tuyadevices list is searched and all devices with a Product ID in the mappings dict get those mappings assigned.

Parameters:
  • tuyadevices (list or tuple) – list of devices

  • mappings (dict)

Returns:

Nothing, modifies tuyadevices in place

Return type:

None

tinytuya.decrypt(msg, key)
tinytuya.decrypt_udp(msg, key=None)
tinytuya.deviceScan(verbose=False, maxretry=None, color=True, poll=True, forcescan=False, byID=False)

Scans your network for Tuya devices and returns dictionary of devices discovered

Deprecated since version v0.x: Use the scanner module instead

Parameters:
  • verbose (bool) – Print formatted output to stdout [Default: False]

  • maxretry (float or int or None) – When a number, stop scanning after that many seconds

  • color (bool) – Display output in color if color support is detected

  • poll (bool) – True or False, poll dps status for devices

  • forcescan (bool) – Brute force scan the DEFAULT_NETWORK network

  • byID (bool) – Returned dict keys are the Device ID (True) or IP address (False)

Returns:

Dictionary of all devices found, by DevID or IP

Return type:

devices (dict)

To unpack the returned data, you can do something like this:

devices = tinytuya.deviceScan()
for ip in devices:
    devid = devices[ip]['id']
    key = devices[ip]['name']
    key = devices[ip]['key']
    vers = devices[ip]['version']
    dps = devices[ip]['dps']
tinytuya.device_info(dev_id)

Searches the DEVICEFILE file for devices with ID == dev_id

Parameters:

dev_id (str) – The specific Device ID you are looking for

Returns:

Device dict containing the the device info, or None if not found

Return type:

dict or None

tinytuya.encrypt(msg, key)
tinytuya.error_json(number=None, payload=None)

Return error details in JSON

tinytuya.find_device(dev_id=None, address=None)

Scans network for Tuya devices with either ID = dev_id or IP = address

Parameters:
  • dev_id (str, optional) – The specific Device ID you are looking for

  • address (str, optional) – The IP address you are tring to find the Device ID for

Returns:

{‘ip’:<ip>, ‘version’:<version>, ‘id’:<id>, ‘product_id’:<product_id>, ‘data’:<broadcast data>}

Return type:

dict or None

tinytuya.merge_dps_results(dest, src)

Merge multiple receive() responses into a single dict

src will be combined with and merged into dest

Parameters:
  • dest (dict) – Destination dict to merge into

  • src (dict) – Source dict to merge from

Returns:

Nothing, dest dict is modified in-place

Return type:

None

tinytuya.scan(maxretry=None, color=True, forcescan=False)

Scans your network for Tuya devices with output to stdout

Deprecated since version v0.x: Use the scanner module instead

Parameters:
  • maxretry (float or int or None) – When a number, stop scanning after that many seconds

  • color (bool) – Display output in color if color support is detected

  • forcescan (bool) – Brute force scan the DEFAULT_NETWORK network

Returns:

Nothing, the scan result is displayed

Return type:

None

tinytuya.set_debug(toggle=True, color=True)

Enable tinytuya verbose logging

Parameters:
  • toggle (bool) – Enable debug logging

  • color (bool) – Output terminal control codes for color