Skip to main content

Python interface to Tuya WiFi smart devices.

Project description

TuyaFace logo

Tuya client that allows you to locally communicate with tuya devices without the tuya-cloud.

Installation

pip install tuyaface

Module tuyaface {#tuyaface}

Functionality for communicating with a Tuya device.

Sub-modules

Functions

Function set_state {#tuyaface.set_state}

def set_state(device: dict, value, idx: int = 1)

Send status update request for one dps value to the tuya device.

returns bool

Function set_status {#tuyaface.set_status}

def set_status(device: dict, dps: dict)

Send state update request to the tuya device and waits for response.

returns bool

Function status {#tuyaface.status}

def status(device: dict)

Request status of the tuya device.

returns dict

Module tuyaface.aescipher {#tuyaface.aescipher}

Helpers for AES crypto.

Functions

Function decrypt {#tuyaface.aescipher.decrypt}

def decrypt(key, enc, use_base64=True)

Optionally base64-decode and decrypt.

Function encrypt {#tuyaface.aescipher.encrypt}

def encrypt(key, raw, use_base64=True)

Encrypt and optionally base64-encode.

Module tuyaface.const {#tuyaface.const}

Tuya constants.

Module tuyaface.helper {#tuyaface.helper}

Helpers.

Functions

Function bytes2hex {#tuyaface.helper.bytes2hex}

def bytes2hex(data: bytes, pretty: bool = False)

Render hexstring from bytes.

Function hex2bytes {#tuyaface.helper.hex2bytes}

def hex2bytes(data: str)

Parse hexstring to bytes.

Module tuyaface.tuyaclient {#tuyaface.tuyaclient}

Helper to maintain a connection to and serialize access to a Tuya device.

Classes

Class TuyaClient {#tuyaface.tuyaclient.TuyaClient}

class TuyaClient(device: dict, on_status: <built-in function callable> = None, on_connection: <built-in function callable> = None)

Helper class to maintain a connection to and serialize access to a Tuya device.

Initialize the Tuya client.

Ancestors (in MRO)

Methods

Method run {#tuyaface.tuyaclient.TuyaClient.run}

def run(self)

Tuya client main loop.

Method set_state {#tuyaface.tuyaclient.TuyaClient.set_state}

def set_state(self, value, idx: int = 1)

Set state.

Method status {#tuyaface.tuyaclient.TuyaClient.status}

def status(self)

Request status.

Method stop_client {#tuyaface.tuyaclient.TuyaClient.stop_client}

def stop_client(self)

Close the connection and stop the worker thread.

example

from tuyaface.tuyaclient import TuyaClient

def on_status(data: dict):
    print(data)

def on_connection(value: bool):
    print(value)

device = {
    'protocol': '3.3', # 3.1 | 3.3
    'deviceid': '34280100600194d17c96',
    'localkey': 'e7e9339aa82abe61',
    'ip': '192.168.1.101',            
}

client = TuyaClient(device, on_status, on_connection)
client.start()

data = client.status()
client.set_state(!data['dps']['1'], 1) #toggle
client.stop_client()

Data structure

Device dict

device = {
    'protocol': '3.3', # 3.1 | 3.3
    'deviceid': '34280100600194d17c96',
    'localkey': 'e7e9339aa82abe61',
    'ip': '192.168.1.101',
    'pref_status_cmd': 10 #optional, default 10
}

TuyaFace will automatically add tuyaface dict to the device with data to support implementations without the TuyaClient class.

tuyaface = {
    "sequence_nr": 0, # Request counter
    "connection": None, # Holds the connection 
    "availability": False, # If the device could be reached
    "pref_status_cmd": 10, # Preferred status command 
    "status": None, # Reply to the last status request
}

DPS dict

dps = {
    '1': True,
    '2': False,
    '101': 255,
    '102': 128,
    ...etc...
}

Improvements

Do you have ideas how we can make this package even better? Or would you like to contribute in another way? Drop a line in the issue section, all help is welcome.

Acknowledgements

Implementations

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

tuyaface-1.3.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

tuyaface-1.3.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file tuyaface-1.3.1.tar.gz.

File metadata

  • Download URL: tuyaface-1.3.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for tuyaface-1.3.1.tar.gz
Algorithm Hash digest
SHA256 523a0603e7fd0bcbad85c32844c401babbe5f307eb4ddb8ff14ce08ae7352da8
MD5 019fecd8dcf030264e7c4d18a34a03a5
BLAKE2b-256 593b100fe6594552752a2ef3e4cfd48586686116e52a4a64651606a8746c36cb

See more details on using hashes here.

File details

Details for the file tuyaface-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: tuyaface-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for tuyaface-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6def8743e187b17616a481465fe50aa77cd659dc8345e67d48a4a60e239c3b28
MD5 0793fb46a3211632ad945606b9ebfba5
BLAKE2b-256 1256f91d195de1fd3cd180f0d43143ee1404e3f46e0d657a4de1520d3e4bb308

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