Skip to main content

Python library for interacting with Tuya-based devices using asyncio

Project description

Async Tuya LAN Control

Integration with TuyaCloud smart home devices using local LAN control and Python's asyncio library. This library was thrown together in a week to support some cheap bulbs that I bought expecting to use Tuya-Convert on, but got hit with the latest firmware version from Tuya. At the time, I couldn't find any Python Tuya libraries supporting asyncio for local push use in Home Assistant.

I only have tested Tuya lights with this library as that is the only device I own, I will include classes for the additional devices, but they will need to be tested to confirm usage.

Example Usage

This library requires the device ID and the local key for the device you want to control. I found it easiest to snag the local key from an older version of the Smart Life app's preferences.xml on a rooted Android phone. There are other methods you can read about here.

from aiotuyalan import TuyaLight
import asyncio

IP = '192.168.1.26'
LOCAL_KEY = 'fffff00000ffffff'
DEVICE_ID = 'ffff000fff00f0f0f000'

async def main():

    loop = asyncio.get_running_loop()
    device = TuyaLight(loop, IP, DEVICE_ID, LOCAL_KEY, version='3.3')

    async def on_update():
        print("Received device update.")
        await device.set_color_temp(40)
        await device.set_brightness(255)
        await device.disconnect()

    async def on_stop():
        print("Disconnected from device.")
        loop.stop()

    await device.set_on_update(on_update)
    await device.set_on_stop(on_stop)

    while True:
        try:
            await device.connect()
            break
        except Exception as err:
            print("Error occcured during connection: ", err)
            print("Trying again in  5 seconds...")
            await asyncio.sleep(5)

    print("Connected to device!")

loop = asyncio.get_event_loop()
try:
    asyncio.ensure_future(main())
    loop.run_forever()
except KeyboardInterrupt:
    pass
finally:
    loop.close()

Project details


Download files

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

Source Distribution

aiotuyalan-0.0.2.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

aiotuyalan-0.0.2-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file aiotuyalan-0.0.2.tar.gz.

File metadata

  • Download URL: aiotuyalan-0.0.2.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for aiotuyalan-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5c9f7a59c6ef14f1874c445599834145c40f8c09d807f5f81687202d7f18abf2
MD5 f4c5191853031b04e78066adea215abe
BLAKE2b-256 e65e7c8d34a01559a73262d6cd25a87c9c57c3188db191ad7c54cabd0f82aa77

See more details on using hashes here.

File details

Details for the file aiotuyalan-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: aiotuyalan-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.6

File hashes

Hashes for aiotuyalan-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 c8c5abb37cb095c0912f19274cda558a33f0592345b4856cf7e2eaf46ffe5ac3
MD5 1c75f2a9b7c159422aad31bc4cae13c1
BLAKE2b-256 fd8a9d2a5a5f7bc28a19cb4873d41ba0542183fe5d82a95b0cdd25e5a60c6e44

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