Python API for Tuya WiFi smart devices using a direct local area network (LAN) connection or the cloud (TuyaCloud API).
MIT License
Published by jasonacox about 3 years ago
Published by jasonacox over 3 years ago
wizard
to support new Tuya IoT Cloud signature method (Issue #57)set_bulb_type(type)
(PR #54)tuya-raw.json
to record raw response from Tuya IoT PlatformPublished by jasonacox over 3 years ago
wizard
handling to capture and display Tuya API server error responses (PR #45)state()
function to not crash when dps values are missing in response (PR #46)send()
and receive()
Published by jasonacox over 3 years ago
send()
and receive()
function to allow direct control of payload transfers. Useful to monitor state changes via threads or continuous loops. This example opens a Tuya device and watches for state changes (e.g. switch going on and off):import tinytuya
d = tinytuya.OutletDevice('DEVICEID', 'DEVICEIP', 'DEVICEKEY')
d.set_version(3.3)
d.set_socketPersistent(True)
print(" > Send Initial Query for Status < ")
payload = d.generate_payload(tinytuya.DP_QUERY)
d.send(payload)
while(True):
# See if any data is available
data = d.receive()
print('Received Payload: %r' % data)
# Send a keyalive heartbeat ping
print(" > Send Heartbeat Ping < ")
payload = d.generate_payload(tinytuya.HEART_BEAT)
d.send(payload)
Published by jasonacox over 3 years ago
Published by jasonacox over 3 years ago
set_dimmer()
to OutletDevice class.set_hsv()
to BulbDevice class.set_brightness()
in BulbDevice to handle white and colour modes. Issue #30has_colour
, has_brightness
and has_colourtemp
to ignore requests that do not exist (returns error).Published by jasonacox over 3 years ago
Command line mode is still the same:
python3 -m tinytuya wizard
Import now requires additional import to run Wizard programmatically:
import tinytuya
import tinytuya.wizard
tinytuya.wizard.wizard()
Published by jasonacox over 3 years ago
Published by jasonacox over 3 years ago
set_colour(r, g, b)
to work with python2set_debug()
to toggle on debug logging (with color)device22
to automatically detect and set_dpsUsed()
with available DPS values.set_socketTimeout(s)
for adjustable connection timeout setting (defaults to 5s)set_sendWait(s)
for adjustable wait time after sending device commandsExample
import tinytuya
tinytuya.set_debug(toggle=False, color=True)
d = tinytuya.OutletDevice('<ID>','<IP>','<KEY>')
d.set_version(3.3)
d.status()
{u'Payload': None, u'Err': u'905', u'Error': u'Network Error: Device Unreachable'}
Published by jasonacox over 3 years ago
updatedps()
command 18 function to request device to update DPS values (Issue #8)set_debug()
function to activate debug loggingimport tinytuya
import time
tinytuya.set_debug(True)
d = tinytuya.OutletDevice('DEVICEID', 'IP', 'LOCALKEY')
d.set_version(3.3)
print(" > Fetch Status < ")
data = d.status()
time.sleep(5)
print(" > Request Update for DPS indexes 18, 19 and 20 < ")
result = d.updatedps([18, 19, 20])
print(" > Fetch Status Again < ")
data2 = d.status()
print("Before %r" % data)
print("After %r" % data2)
Published by jasonacox almost 4 years ago
import tinytuya
ID = "01234567890123456789"
IP = None
KEY = "0123456789012345"
d = tinytuya.OutletDevice(ID,IP,KEY)
d.status()
Published by jasonacox almost 4 years ago
hexdigest[8:][:16]
Published by jasonacox almost 4 years ago
set_white_percentage(brightness=100, colourtemp=0):
set_brightness_percentage(brightness=100):
set_colourtemp_percentage(colourtemp=100):
set_mode(mode='white'):
# white, colour, scene, musicPublished by jasonacox almost 4 years ago
-nocolor
) for interactive Wizard and Scan functions.python3 -m tinytuya wizard
Published by jasonacox almost 4 years ago
[8:][:16]
to [8:][:24]
set_socketPersistent(False/True) # False [default] or True
set_socketNODELAY(False/True) # False or True [default]
set_socketRetryLimit(integer) # retry count limit [default 5]
set_scene(scene): # 1=nature, 3=rave, 4=rainbow
Published by jasonacox almost 4 years ago
scan()
function to get a list of Tuya devices on your network along with their device IP, ID and VERSION number (3.1 or 3.3):python3 -m tinytuya
Published by jasonacox about 4 years ago
OutletDevice(dev_id, address, local_key=None, dev_type='default')
CoverDevice(dev_id, address, local_key=None, dev_type='default')
BulbDevice(dev_id, address, local_key=None, dev_type='default')
__init__
Published by jasonacox about 4 years ago
Initial Release v1.0.0