Skip to main content

Pull power and state data from Tuya WiFi smart devices

Project description

TuyaPower - PyPi Module

Author: Jason A. Cox https://github.com/jasonacox/tuyapower

Description

Python module to pull power and state data from Tuya WiFi smart devices. Tested on RaspberryPi, Linux, Windows 10 and MacOS.

Preparation

This module requires: pycrypto, pytuya, Crypto and pyaes.

 sudo apt-get install python-crypto python-pip		
 pip install pycrypto
 pip install pytuya
 # pip install Crypto		
 # pip install pyaes		

Functions

  • deviceInfo - Poll device and return on, w, mA, V and err data.
    (on, w, mA, V, err) = tuyapower.deviceInfo(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
    
  • devicePrint - Poll device and print formatted output to stdout.
    tuyapower.devicePrint(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
    
  • deviceJSON - Poll device and return JSON formatted details.
    dataJSON = tuyapower.deviceJSON(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
    
  • deviceScan(verbose) - Scans network for smart plug devices and return dictionary of devices and power data.
    verbose = False
    devices = tuyapower.deviceScan(verbose)
    
  • scan() - This is a shortcut for deviceScan() that prints formatted output to stdout for UDP ports 6666 and 6667

Parameters:

  • PLUGID = Device ID e.g. 01234567891234567890
  • PLUGIP = Device IP Address e.g. 10.0.1.99
  • PLUGKEY = Device Key e.g. 0123456789abcdef
  • PLUGVERS = Version of Protocol 3.1 or 3.3
  • verbose = Print more details - True or False (default is False)

Response Data:

  • on = Switch state - true or false
  • w = Wattage
  • mA = milliamps
  • V = Voltage
  • err = Error message or OK
  • devices = Dictionary of all devices found with power data if available

Note: If error occurs, on will be set to false, w, mA and V will be set to -99.0.

Example Usage:

# Poll a Single Devices
import tuyapower

PLUGID = '01234567891234567890'
PLUGIP = '10.0.1.99'
PLUGKEY = '0123456789abcdef'
PLUGVERS = '3.1'

(on, w, mA, V, err) = tuyapower.deviceInfo(PLUGID,PLUGIP,PLUGKEY,PLUGVERS)

# Scan Network for All Devices
# To see output on stdout set verbose True
tuyapower.deviceScan(True)
Scanning on UDP port 6666 for devices...

FOUND Device [Valid payload]: 10.0.1.100
    ID = 01234567891234567890, Key = 0123456789abcdef, Version = 3.1
    Stats: on=True, W=6.0, mA=54.0, V=121.1 [OK]
FOUND Device [Valid payload]: 10.0.1.200
    ID = 01234567891234567891, Key = 0123456789abcdea, Version = 3.1
    Stats: on=True, W=-99, mA=-99, V=-99 [Power data unavailable]

Scan Complete!  Found 2 devices.

# Scan the network and unpack the response 
devices = tuyapower.deviceScan()
    for ip in devices:
        id = devices[ip]['gwId']
        key = devices[ip]['productKey']
        vers = devices[ip]['version']
        (on, w, mA, V, err) = deviceInfo(id, ip, key, vers)
        print("Device at %s: ID %s, state=%s, W=%s, mA=%s, V=%s [%s]"%(ip,id,on,w,mA,V,err))

Scan Tool

The function tuyapower.scan() will listen to your local network (UDP 6666 and 6667) and identify Tuya devices broadcasting their IP, Device ID, productKey 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 tuyapower.deviceScan() function returns all found devices and their stats (via dictionary result).

You can run the scanner from the command line using this:

python -m tuyapower

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

tuyapower-0.0.17-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file tuyapower-0.0.17-py2.py3-none-any.whl.

File metadata

  • Download URL: tuyapower-0.0.17-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.1

File hashes

Hashes for tuyapower-0.0.17-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 63d5d36ef198b8fbe420869ed376aa44d2d027fb22864a04b8412e1b4ec82b76
MD5 3691975a8d22347c1ca9cdd8c42121b9
BLAKE2b-256 49c87b45f2d4919955808e504156da7d8cd537123f9777a7b8c935a469951ab7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page