clach04 / python-tuya

Python interface to ESP8266MOD WiFi smart devices from Shenzhen Xenon. NOTE I'm not using any devices with this library so I can't test :-(
MIT License
238 stars 56 forks source link

python-tuya

Build Status

NOTE I'm no longer using this library (I flashed my devices with Tasmota instead - it's a way more reliable firmware, even without any WiFi) so this is not actively maintained.

Similar active projects:

An alternative is to reflash the devices (e.g with Tasmota, ESPurna, etc.) using https://github.com/ct-Open-Source/tuya-convert/ or if OTA is not an option over serial, see https://github.com/arendst/Tasmota - these are much better firmwre options. Recommended reading:


Python 2.7 and Python 3.6.1 interface to ESP8266MOD WiFi smart devices from Shenzhen Xenon. If you are using the Jinvoo Smart App, this allows local control over the LAN. NOTE requires the devices to have already been activated by Jinvoo Smart App (or similar).

Key extraction

https://github.com/clach04/python-tuya/wiki has background information for how to get device id and local key. (the device id can be seen in Jinvoo Smart App, under "Device Info").

Known to work with:

Demo:

import pytuya

d = pytuya.OutletDevice('DEVICE_ID_HERE', 'IP_ADDRESS_HERE', 'LOCAL_KEY_HERE')
data = d.status()  # NOTE this does NOT require a valid key
print('Dictionary %r' % data)
print('state (bool, true is ON) %r' % data['dps']['1'])  # Show status of first controlled switch on device

# Toggle switch state
switch_state = data['dps']['1']
data = d.set_status(not switch_state)  # This requires a valid key
if data:
    print('set_status() result %r' % data)

# on a switch that has 4 controllable ports, turn the fourth OFF (1 is the first)
data = d.set_status(False, 4)
if data:
    print('set_status() result %r' % data)
    print('set_status() extrat %r' % data[20:-8])

TODO demo timer (with comment not all devices support this, one way to check, is to check Jinvoo Smart App and see if there is a clock icon that is not dimmed out).

Encryption notes

These devices uses AES encryption, this is not available in Python standard library, there are three options:

1) PyCrypto 2) PyCryptodome 3) pyaes (note Python 2.x support requires https://github.com/ricmoo/pyaes/pull/13)

Related Projects

Publishing cheat sheet

pip install twine pycrypto
python setup.py sdist --formats=zip
twine upload dist/*
# version bump ready for next release

Acknowledgements