tuyapower


Nametuyapower JSON
Version 0.2.0 PyPI version JSON
download
home_pagehttps://github.com/jasonacox/tuyapower
SummaryPull power and state data from Tuya WiFi smart devices
upload_time2023-04-08 15:37:07
maintainer
docs_urlNone
authorJason Cox
requires_python
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # TuyaPower - PyPi Module

[![Build Status](https://travis-ci.org/jasonacox/tuyapower.svg?branch=master)](https://travis-ci.org/jasonacox/tuyapower)
[![PyPI version](https://badge.fury.io/py/tuyapower.svg)](https://badge.fury.io/py/tuyapower)

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: pycryptodome and tinytuya (replaces pytuya).

```bash
# Install required libraries
sudo apt-get install python-crypto python-pip		# for RPi, Linux
python3 -m pip install pycryptodome    # or pycrypto, pyaes or Crypto
python3 -m pip install tinytuya        # or pytuya
python3 -m pip install tuyapower       # this tuyapower module 
```

 For Windows 10 users or if you get errors related to Crypto, try installing the pycryptodome module:
 ```bash
 pip install pycryptodome
 ```
 
# Functions
* deviceInfo - Poll device and return on, w, mA, V and err data.
    ```python
    (on, w, mA, V, err) = tuyapower.deviceInfo(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
    ```
* deviceRaw - Poll device and return raw response data.
    ```python
    rawData = tuyapower.deviceRaw(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
    ```
* devicePrint - Poll device and print formatted output to stdout.
    ```python
    tuyapower.devicePrint(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)
    ```
* deviceJSON - Poll device and return JSON formatted details.
    ```python
    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.
    ```python
    verbose = False
    devices = 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. 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)
* max_retries = Number of times to retry scan of new devices (default is 15)
 
## Response Data: 
* on = Switch state - true or false
* w = Wattage 
* mA = milliamps 
* V = Voltage 
* err = Error message or OK
* rawData = Raw response from device
* 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:
```python

# 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:
```bash
python -m tuyapower
```

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 by passing an optional arguments (ex. 50 retries):

```bash
# command line
python -m tuyapower 50
```

```python
# invoke verbose interactive scan
tuyapower.scan(50)

# return payload of devices
devices = tuyapower.deviceScan(false, 50)
```


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jasonacox/tuyapower",
    "name": "tuyapower",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Jason Cox",
    "author_email": "jason@jasonacox.com",
    "download_url": "",
    "platform": null,
    "description": "# TuyaPower - PyPi Module\n\n[![Build Status](https://travis-ci.org/jasonacox/tuyapower.svg?branch=master)](https://travis-ci.org/jasonacox/tuyapower)\n[![PyPI version](https://badge.fury.io/py/tuyapower.svg)](https://badge.fury.io/py/tuyapower)\n\nAuthor: Jason A. Cox \nhttps://github.com/jasonacox/tuyapower\n\n# Description\nPython module to pull power and state data from Tuya WiFi smart devices.  _Tested on RaspberryPi, Linux, Windows 10 and MacOS._ \n\n# Preparation\nThis module requires: pycryptodome and tinytuya (replaces pytuya).\n\n```bash\n# Install required libraries\nsudo apt-get install python-crypto python-pip\t\t# for RPi, Linux\npython3 -m pip install pycryptodome    # or pycrypto, pyaes or Crypto\npython3 -m pip install tinytuya        # or pytuya\npython3 -m pip install tuyapower       # this tuyapower module \n```\n\n For Windows 10 users or if you get errors related to Crypto, try installing the pycryptodome module:\n ```bash\n pip install pycryptodome\n ```\n \n# Functions\n* deviceInfo - Poll device and return on, w, mA, V and err data.\n    ```python\n    (on, w, mA, V, err) = tuyapower.deviceInfo(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)\n    ```\n* deviceRaw - Poll device and return raw response data.\n    ```python\n    rawData = tuyapower.deviceRaw(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)\n    ```\n* devicePrint - Poll device and print formatted output to stdout.\n    ```python\n    tuyapower.devicePrint(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)\n    ```\n* deviceJSON - Poll device and return JSON formatted details.\n    ```python\n    dataJSON = tuyapower.deviceJSON(PLUGID, PLUGIP, PLUGKEY, PLUGVERS)\n    ```\n* deviceScan(verbose, max_retries=15) - Scans network for smart plug devices and return dictionary of devices and power data.\n    ```python\n    verbose = False\n    devices = tuyapower.deviceScan(verbose)\n    ```\n* 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.\n\n## Parameters:\n* PLUGID = Device ID e.g. 01234567891234567890\n* PLUGIP = Device IP Address e.g. 10.0.1.99\n* PLUGKEY = Device Key e.g. 0123456789abcdef\n* PLUGVERS = Version of Protocol 3.1 or 3.3\n* verbose = Print more details - True or False (default is False)\n* max_retries = Number of times to retry scan of new devices (default is 15)\n \n## Response Data: \n* on = Switch state - true or false\n* w = Wattage \n* mA = milliamps \n* V = Voltage \n* err = Error message or OK\n* rawData = Raw response from device\n* devices = Dictionary of all devices found with power data if available\n\nNote: If error occurs, on will be set to false, w, mA and V will be set to -99.0.\n\n# Example Usage:\n```python\n\n# Poll a Single Devices\nimport tuyapower\n\nPLUGID = '01234567891234567890'\nPLUGIP = '10.0.1.99'\nPLUGKEY = '0123456789abcdef'\nPLUGVERS = '3.1'\n\n(on, w, mA, V, err) = tuyapower.deviceInfo(PLUGID,PLUGIP,PLUGKEY,PLUGVERS)\n\n# Scan Network for All Devices\n# To see output on stdout set verbose True\ntuyapower.deviceScan(True)\nScanning on UDP port 6666 for devices...\n\nFOUND Device [Valid payload]: 10.0.1.100\n    ID = 01234567891234567890, Key = 0123456789abcdef, Version = 3.1\n    Stats: on=True, W=6.0, mA=54.0, V=121.1 [OK]\nFOUND Device [Valid payload]: 10.0.1.200\n    ID = 01234567891234567891, Key = 0123456789abcdea, Version = 3.1\n    Stats: on=True, W=-99, mA=-99, V=-99 [Power data unavailable]\n\nScan Complete!  Found 2 devices.\n\n# Scan the network and unpack the response \ndevices = tuyapower.deviceScan()\n    for ip in devices:\n        id = devices[ip]['gwId']\n        key = devices[ip]['productKey']\n        vers = devices[ip]['version']\n        (on, w, mA, V, err) = deviceInfo(id, ip, key, vers)\n        print(\"Device at %s: ID %s, state=%s, W=%s, mA=%s, V=%s [%s]\"%(ip,id,on,w,mA,V,err))\n```\n\n### Scan Tool \nThe 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).\n\nYou can run the scanner from the command line using this:\n```bash\npython -m tuyapower\n```\n\nBy 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):\n\n```bash\n# command line\npython -m tuyapower 50\n```\n\n```python\n# invoke verbose interactive scan\ntuyapower.scan(50)\n\n# return payload of devices\ndevices = tuyapower.deviceScan(false, 50)\n```\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "Pull power and state data from Tuya WiFi smart devices",
    "version": "0.2.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4a29a7f78243b68ff51e723272a1250dc8b58c0a937597d03d7b2acc646bd74d",
                "md5": "142a1de668007b461c6618011b457cb5",
                "sha256": "c65680a24cb09b440509b4a20a620e4d12019f05ad3174ee60b76f138ecc3731"
            },
            "downloads": -1,
            "filename": "tuyapower-0.2.0-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "142a1de668007b461c6618011b457cb5",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 8881,
            "upload_time": "2023-04-08T15:37:07",
            "upload_time_iso_8601": "2023-04-08T15:37:07.287023Z",
            "url": "https://files.pythonhosted.org/packages/4a/29/a7f78243b68ff51e723272a1250dc8b58c0a937597d03d7b2acc646bd74d/tuyapower-0.2.0-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-08 15:37:07",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "jasonacox",
    "github_project": "tuyapower",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "lcname": "tuyapower"
}
        
Elapsed time: 0.05897s