Prerequisite: device ID, IP address, local key and protocol version (see the item on getting local keys).
pip install tinytuya
import tinytuya
plug = tinytuya.OutletDevice(
dev_id="YOUR_DEVICE_ID",
address="192.168.1.50", # fixed IP via DHCP reservation
local_key="YOUR_LOCAL_KEY",
version=3.3, # 3.1, 3.2, 3.3, 3.4 or 3.5: must match the device
)
status = plug.status()
print(status) # e.g. {'dps': {'1': True, ...}, ...}
plug.turn_on()
plug.turn_off()
status() returns the device's data points (DPS). On many plugs DPS 1 is the main switch; other numbers carry power measurements, timers or settings, and they differ per device (see the DPS item).
Finding the right values
python -m tinytuya scanlists Tuya devices on the LAN and their protocol versions.- The wizard (
python -m tinytuya wizard) fetches IDs and local keys for your account.
Troubleshooting
| Symptom | Likely cause |
|---|---|
| Connection refused or times out, although the device is online | another client holds the single local TCP connection: close the Smart Life / Tuya app, stop other integrations polling the device |
| Decrypt errors or garbled responses | wrong local key (changed after re-pairing) or wrong protocol version |
| Works, then stops after a router restart | IP address changed: use a DHCP reservation |
Keep scripts from polling too aggressively; a status call every few seconds per device is plenty for most automations.