Package: tuyapower
TuyaPower - PyPi ModuleAuthor: Jason A. Coxhttps://github.com/jasonacox/tuyapowerDescriptionPython module to pull power and state data from Tuya WiFi smart devices.Tested on RaspberryPi, Linux, Windows 10 and MacOS.PreparationThis module requires: pycryptodome and tinytuya (replaces pytuya).# Install required librariessudoapt-getinstallpython-cryptopython-pip# for RPi, Linuxpython3-mpipinstallpycryptodome# or pycrypto, pyaes or Cryptopython3-mpipinstalltinytuya# or pytuyapython3-mpipinstalltuyapower# this tuyapower moduleFor Windows 10 users or if you get errors related to Crypto, try installing the pycryptodome module:pipinstallpycryptodomeFunctionsdeviceInfo - Poll device and return on, w, mA, V and err data.(on,w,mA,V,err)=tuyapower.deviceInfo(PLUGID,PLUGIP,PLUGKEY,PLUGVERS)deviceRaw - Poll device and return raw response data.rawData=tuyapower.deviceRaw(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, max_retries=15) - Scans network for smart plug devices and return dictionary of devices and power data.verbose=Falsedevices=tuyapower.deviceScan(verbose)scan(max_retries=15) - This is a shortcut for deviceScan() that prints formatted output to stdout for UDP ports 6666 and 6667. 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.Parameters:PLUGID = Device ID e.g. 01234567891234567890PLUGIP = Device IP Address e.g. 10.0.1.99PLUGKEY = Device Key e.g. 0123456789abcdefPLUGVERS = Version of Protocol 3.1 or 3.3verbose = Print more details - True or False (default is False)max_retries = Number of times to retry scan of new devices (default is 15)Response Data:on = Switch state - true or falsew = WattagemA = milliampsV = Voltageerr = Error message or OKrawData = Raw response from devicedevices = Dictionary of all devices found with power data if availableNote: If error occurs, on will be set to false, w, mA and V will be set to -99.0.Example Usage:# Poll a Single DevicesimporttuyapowerPLUGID='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 Truetuyapower.deviceScan(True)ScanningonUDPport6666fordevices...FOUNDDevice[Validpayload]:10.0.1.100ID=01234567891234567890,Key=0123456789abcdef,Version=3.1Stats:on=True,W=6.0,mA=54.0,V=121.1[OK]FOUNDDevice[Validpayload]:10.0.1.200ID=01234567891234567891,Key=0123456789abcdea,Version=3.1Stats:on=True,W=-99,mA=-99,V=-99[Powerdataunavailable]ScanComplete!Found2devices.# Scan the network and unpack the responsedevices=tuyapower.deviceScan()foripindevices: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 ToolThe functiontuyapower.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. Thetuyapower.deviceScan()function returns all found devices and their stats (via dictionary result).You can run the scanner from the command line using this:python-mtuyapowerBy 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 (ex. 50 retries):# command linepython-mtuyapower50# invoke verbose interactive scantuyapower.scan(50)# return payload of devicesdevices=tuyapower.deviceScan(false,50)