TinyTuya: Tuya Smart Device Interface

1.17.6 · active · verified Sun Apr 12

TinyTuya is a Python library that enables local and cloud-based control and monitoring of Tuya WiFi smart devices such as plugs, switches, and lights. It provides an API for direct communication over a local area network (LAN), supporting Tuya protocols from 3.1 to 3.5, and can also integrate with the Tuya Cloud API. Currently at version 1.17.6, the project is actively maintained with frequent updates and a focus on community contributions. [1, 3, 6, 9]

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish local control over a Tuya smart device using TinyTuya. It initializes an `OutletDevice` with device credentials (ID, IP, local key, and protocol version), retrieves its current status, and then toggles the main switch (Data Point 1). Device credentials should ideally be supplied via environment variables or a configuration file. Remember to obtain your device's `LOCAL_KEY` and `DEVICE_ID` using the `tinytuya wizard` CLI tool or the Tuya IoT Platform. [3, 5, 6, 8]

import os
import tinytuya

# --- Configuration (replace with your device details or environment variables) ---
DEVICE_ID = os.environ.get('TUYA_DEVICE_ID', 'YOUR_DEVICE_ID')
DEVICE_IP = os.environ.get('TUYA_DEVICE_IP', 'YOUR_DEVICE_IP') # Can be 'Auto' if devices.json is present
LOCAL_KEY = os.environ.get('TUYA_LOCAL_KEY', 'YOUR_LOCAL_KEY')
DEVICE_VERSION = float(os.environ.get('TUYA_DEVICE_VERSION', '3.3')) # e.g., 3.1, 3.3, 3.5

# --- Local Control Example (OutletDevice) ---
print(f"Connecting to device {DEVICE_ID} at {DEVICE_IP} (version {DEVICE_VERSION})...")

try:
    # Initialize the device object
    d = tinytuya.OutletDevice(DEVICE_ID, DEVICE_IP, LOCAL_KEY, version=DEVICE_VERSION)
    d.set_version(DEVICE_VERSION) # Ensure the correct protocol version is set

    # Get current status
    data = d.status()
    if data and 'dps' in data:
        print(f"Device status: {data['dps']}")
        # Assuming DP '1' controls the main switch (common for outlets)
        current_state = data['dps'].get('1')
        print(f"Current switch state (DP 1): {'ON' if current_state else 'OFF'}")

        # Toggle the power state
        new_state = not current_state
        print(f"Setting switch state (DP 1) to: {'ON' if new_state else 'OFF'}")
        d.set_status(new_state)
        print("Command sent. Waiting for update...")

        # Re-check status after a short delay (optional, for verification)
        import time
        time.sleep(2)
        updated_data = d.status()
        if updated_data and 'dps' in updated_data:
            print(f"Updated switch state (DP 1): {'ON' if updated_data['dps'].get('1') else 'OFF'}")
    else:
        print("Failed to get device status or 'dps' key not found.")

except Exception as e:
    print(f"An error occurred: {e}")
    print("Troubleshooting tips:")
    print("- Ensure your device ID, IP, and local key are correct.")
    print("- Confirm the device is on the same local network as the script.")
    print("- Close any official Tuya/Smart Life apps to free the device's TCP connection. [11]")
    print("- Check your firewall for UDP (6666, 6667, 7000) and TCP (6668) port access. [3]")

view raw JSON →