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 almost 2 years ago
TinyTuya [1.10.0]
Usage:
python -m tinytuya <command> [<max_time>] [-nocolor] [-force [192.168.0.0/24 192.168.1.0/24 ...]] [-h]
wizard Launch Setup Wizard to get Tuya Local KEYs.
scan Scan local network for Tuya devices.
devices Scan all devices listed in devices.json file.
snapshot Scan devices listed in snapshot.json file.
json Scan devices listed in snapshot.json file [JSON].
<max_time> Maximum time to find Tuya devices [Default=18]
-nocolor Disable color text output.
-force Force network scan for device IP addresses. Auto-detects network range if none provided.
-no-broadcasts Ignore broadcast packets when force scanning.
-h Show usage.
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.9.1...v1.10.0
Published by jasonacox almost 2 years ago
_gettoken()
to prevent extraneous output. #229Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.9.0...v1.9.1
Published by jasonacox almost 2 years ago
import tinytuya
# Zigbee Gateway support uses a parent/child model where a parent gateway device is
# connected and then one or more children are added.
# Configure the parent device
gw = tinytuya.Device( 'eb...4', address=None, local_key='aabbccddeeffgghh', persist=True, version=3.3 )
print( 'GW IP found:', gw.address )
# Configure one or more children. Every dev_id must be unique!
zigbee1 = tinytuya.OutletDevice( 'eb14...w', cid='0011223344556601', parent=gw )
zigbee2 = tinytuya.OutletDevice( 'eb04...l', cid='0011223344556689', parent=gw )
print(zigbee1.status())
print(zigbee2.status())
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.8.0...v1.9.0
Published by jasonacox almost 2 years ago
getdevicelog(id)
, make generic cloud request with custom URL and params cloudrequest(url, ...)
and fetch connection status getconnectstatus(id)
by @uzlonewolf in https://github.com/jasonacox/tinytuya/pull/219
import tinytuya
import json
c = tinytuya.Cloud()
r = c.getdevicelog( '00112233445566778899' )
print( json.dumps(r, indent=2) )
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.7.2...v1.8.0
Published by jasonacox almost 2 years ago
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.7.1...v1.7.2
Published by jasonacox about 2 years ago
import tinytuya
# Specify only Device ID and tinytuya will scan for IP
# and lookup Device Local KEY from devices.json
d = tinytuya.OutletDevice( '0123456789abcdef0123' )
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.7.0...v1.7.1
Published by jasonacox about 2 years ago
_send_receive()
- now takes care of the packing and encrypting so it can re-encode whenever the socket is closed and reopened, and _get_socket() now takes care of negotiating the session key (v3.4)d = tinytuya.OutletDevice(
dev_id='xxxxxxxxxxxxxxxxxxxxxxxx',
address='x.x.x.x',
local_key='xxxxxxxxxxxxxxxx',
version=3.4)
print(d.status())
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.6.6...v1.7.0
Published by jasonacox about 2 years ago
# Example usage of community contributed device modules
from tinytuya.Contrib import SocketDevice
socket = SocketDevice('abcdefghijklmnop123456', '172.28.321.475', '1234567890123abc', version=3.3)
print(socket.get_energy_consumption())
print(socket.get_state())
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.6.5...v1.6.6
Published by jasonacox about 2 years ago
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.6.4...v1.6.5
Published by jasonacox over 2 years ago
# Example usage of community contributed device modules
from tinytuya import Contrib
ir = Contrib.IRRemoteControlDevice( 'abcdefghijklmnop123456', '172.28.321.475', '1234567890123abc' )
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.6.2...v1.6.4
Published by jasonacox over 2 years ago
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.6.1...v1.6.2
Published by jasonacox over 2 years ago
getdevices()
to import device mac addresses (same as wizard)./offline
and web UI.# Example usage of community contributed device modules
from tinytuya import Contrib
thermo = Contrib.ThermostatDevice( 'abcdefghijklmnop123456', '172.28.321.475', '1234567890123abc' )
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.6.0...v1.6.1
Published by jasonacox over 2 years ago
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.5.0...v1.6.0
Published by jasonacox over 2 years ago
# Example use of nowait option
d.turn_on(nowait=True)
d.set_colour(r, g, b, nowait=True)
d.set_value(201, '9AEmAvQBJgL0ASYCQAYmAkAGJgJABiY', nowait=True) # send IR command
d.set_value(25, '010e0d0000000000000003e803e8', nowait=True) # set scene
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.4.0...v1.5.0
Published by jasonacox over 2 years ago
set_timer()
function (Issue #87)-force
option to perform network scan for device IP addresses (Issue #117)scanner.py
file.devices
- Display and poll all registered devices for status (using devices.json). This will force a network scan for IP address changes and will create snapshot.json.snapshot
- Display and poll all devices as listed snapshot.json. This assume IP address are the same as the last snapshot.json
- Same as snapshot but respond with a JSON payload.# Run wizard using brute force scan for IP addresses
python -m tinytuya wizard -force
# New Interactive Command Line Options
python -m tinytuya devices
python -m tinytuya snapshot
# Non-Interactive poll with JSON response
python -m tinytuya json
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.3.1...v1.4.0
Published by jasonacox over 2 years ago
Full Changelog: https://github.com/jasonacox/tinytuya/compare/v1.3.0...v1.3.1
Published by jasonacox almost 3 years ago
import tinytuya
c = tinytuya.Cloud(
apiRegion="us",
apiKey="xxxxxxxxxxxxxxxxxxxx",
apiSecret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
apiDeviceID="xxxxxxxxxxxxxxxxxxID")
# Display list of devices
devices = c.getdevices()
print("Device List: %r" % devices)
# Select a Device ID to Test
id = "xxxxxxxxxxxxxxxxxxID"
# Display DPS IDs of Device
result = c.getdps(id)
print("DPS IDs of device:\n", result)
# Display Status of Device
result = c.getstatus(id)
print("Status of device:\n", result)
# Send Command - This example assumes a basic switch
commands = {
'commands': [{
'code': 'switch_1',
'value': True
}, {
'code': 'countdown_1',
'value': 0
}]
}
print("Sending command...")
result = c.sendcommand(id,commands)
print("Results\n:", result)
Published by jasonacox almost 3 years ago
wizard
and scan
to honor value set by command line or default to a value based on the number of devices (if known):# Explicit value set via command line
python3 -m tinytuya wizard 50 # Set retry to 50
python3 -m tinytuya scan 50
# Use automatic computed value
python3 -m tinytuya wizard # Compute a default
python3 -m tinytuya scan
# Example output
TinyTuya (Tuya device scanner) [1.2.11]
[Loaded devices.json - 32 devices]
Scanning on UDP ports 6666 and 6667 for devices (47 retries)...
Published by jasonacox almost 3 years ago
d.disabledetect=True
.Code | Region | Endpoint |
---|---|---|
cn | China Data Center | https://openapi.tuyacn.com |
us | Western America Data Center | https://openapi.tuyaus.com |
us-e | Eastern America Data Center | https://openapi-ueaz.tuyaus.com |
eu | Central Europe Data Center | https://openapi.tuyaeu.com |
eu-w | Western Europe Data Center | https://openapi-weaz.tuyaeu.com |
in | India Data Center | https://openapi.tuyain.com |
Published by jasonacox about 3 years ago
_decode_payload()
to decode non-string type decrypted payload (Issue #67)