tinytuya.Cloud
- class tinytuya.Cloud(apiRegion='cn', apiKey=None, apiSecret=None, apiDeviceID=None, new_sign_algorithm=True, initial_token=None, configFile='tinytuya.json', **extrakw)
Bases:
objectInterface with Tuya’s Cloud IoT Platform
Methods provided by this class
__init__([apiRegion, apiKey, apiSecret, ...])Tuya Cloud IoT Platform Access
cloudrequest(url[, action, post, query])Make a generic cloud request and return the results.
filter_devices(devs[, ip_list])Filters extraneous keys out of device dicts
getconnectstatus([deviceid])Get the device Cloud connect status.
getdevicelog([deviceid, start, end, evtype, ...])Get the logs for a device.
getdevices([verbose, oldlist, include_map])Return dictionary of all devices.
getdps([deviceid])Get the specifications including DPS IDs of the device.
getfunctions([deviceid])Get the functions of the device.
getmapping(productid[, deviceid])Returns a cached mapping for the given product id, or downloads it from the Cloud using a device id
getmappings(devices)Returns the cache for all known Product IDs
getproperties([deviceid])Get the properties of the device.
getstatus([deviceid])Get the status of the device.
sendcommand([deviceid, commands, uri])Send a command to the device
setmappings(mappings)Sets initial mapping cache so we do not need to download everything.
setregion([apiRegion])Change the region of an existing Cloud object
Methods inherited from parent class
Attributes
List of API Regions
List of API Region aliases
- __init__(apiRegion='cn', apiKey=None, apiSecret=None, apiDeviceID=None, new_sign_algorithm=True, initial_token=None, configFile='tinytuya.json', **extrakw)
Tuya Cloud IoT Platform Access
- Parameters:
apiRegion (str, optional) – Tuya API region code (e.g., ‘us’, ‘eu’, ‘cn’, ‘in’).
apiKey (str, optional) – Tuya Cloud API key.
apiSecret (str, optional) – Tuya Cloud API secret.
apiDeviceID (str, optional) – Device ID for initial API calls.
new_sign_algorithm (bool, optional) – Use new sign algorithm for API requests. Default: True.
initial_token (str, optional) – The auth token from a previous run. It will be refreshed if expired.
configFile (str, optional) – Path to the config file to use for credentials. Default: ‘tinytuya.json’.
**extrakw – Additional keyword arguments for future compatibility.
If apiKey or apiSecret are not provided, credentials will be loaded from the
tinytuya.CONFIGFILEconfig file.
- cloudrequest(url, action=None, post=None, query=None)
Make a generic cloud request and return the results.
- Parameters:
url (str) – The URL to fetch, i.e. “/v1.0/devices/0011223344556677/logs”
action (str, optional) – GET, POST, DELETE, or PUT. Defaults to GET, unless POST data is supplied.
post (any, optional) – POST body data. Will be fed into json.dumps() before posting.
query (dict, optional) – A dict containing query string key/value pairs.
- Returns:
Returns server response as a dict on success, or an error_json error dict or None on error
- Return type:
dict or None
- filter_devices(devs, ip_list=None)
Filters extraneous keys out of device dicts
- Parameters:
devs (list of dicts) – list of device dicts to filter
ip_list (dict, optional) – dict containing MAC Address -> IP Address mappings to add to ‘ip’ key
- Returns:
filtered device dict list
- Return type:
list of dicts
- getconnectstatus(deviceid=None)
Get the device Cloud connect status.
- Parameters:
deviceid (str) – Device ID
- Returns:
Server response
- Return type:
dict
- getdevicelog(deviceid=None, start=None, end=None, evtype=None, size=0, max_fetches=50, start_row_key=None, params=None)
Get the logs for a device. https://developer.tuya.com/en/docs/cloud/0a30fc557f?id=Ka7kjybdo0jse
- Note: The cloud only returns logs for DPs in the “official” DPS list.
If the device specifications are wrong then not all logs will be returned! This is a limitation of Tuya’s servers and there is nothing we can do about it.
- Parameters:
devid (str) – Device ID
start (int, optional) – Get logs starting from this time. Defaults to yesterday. Negative is number of days ago, positive should be a UNIX timestamp.
end (int, optional) – Get logs until this time. Defaults to the current time. Negative is number of days ago, positive should be a UNIX timestamp.
evtype (int or str or bytes or list or tuple, optional) – Limit to events of this type. 1 = Online, 7 = DP Reports. Defaults to all events. Can be an int (single event), string (comma-separated list of int`s), or `list/tuple.
size (int, optional) – Target number of log entries to return. Defaults to 0 (all, up to max_fetches*100). Actual number of log entries returned will be between “0” and “size * 2 - 1”
max_fetches (int, optional) – Maximum number of queries to send to the server. Tuya’s server has a hard limit of 100 records per query, so the maximum number of logs returned will be “max_fetches * 100”
start_row_key (any, optional) – The “next_row_key” from a previous run.
params (dict, optional) – Additional values to include in the query string. Defaults to an empty dict.
- Returns:
Response from server
- Return type:
dict
- getdevices(verbose=False, oldlist=[], include_map=False)
Return dictionary of all devices.
- Parameters:
verbose (bool) – Returns raw JSON data from Tuya Cloud when True
oldlist (list or None) – List of devices from previous run
include_map (bool) – Include the DPS mapping in the device list
- Returns:
raw server response if verbose, err_json if error, or list of devices
- Return type:
dict
- getdps(deviceid=None)
Get the specifications including DPS IDs of the device.
- Parameters:
deviceid (str) – Device ID to get the DPs for
- Returns:
Server response
- Return type:
dict
- getfunctions(deviceid=None)
Get the functions of the device.
- Parameters:
deviceid (str) – Device ID to get the function list for
- Returns:
Server response
- Return type:
dict
- getmapping(productid, deviceid=None)
Returns a cached mapping for the given product id, or downloads it from the Cloud using a device id
- Parameters:
productid (str) – Product ID mapping to return
deviceid (str, optional) – Device ID to download the mapping for if Product ID is not cached.
- Returns:
None on failure, or a dict on success (may be an empty dict if device does not have DPs)
- Return type:
dict or None
- getmappings(devices)
Returns the cache for all known Product IDs
- Parameters:
devices (list of dicts) – List of all devices to read Product IDs from
- Returns:
A dict with Product IDs as keys and their mappings as values
- Return type:
dict
- getproperties(deviceid=None)
Get the properties of the device.
- Parameters:
deviceid (str) – Device ID to get the properties for
- Returns:
Server response
- Return type:
dict
- getstatus(deviceid=None)
Get the status of the device.
- Parameters:
deviceid (str) – Device ID to get the status of
- Returns:
Server response
- Return type:
dict
- sendcommand(deviceid=None, commands=None, uri='iot-03/devices/')
Send a command to the device
- Parameters:
deviceid (str) – Device ID to send the command to
commands (any) – Commands to send (Will be fed into json.dumps() before posting)
uri (str, optional) – Endpoint URI fragment to post command to
- Returns:
Server response
- Return type:
dict
- setmappings(mappings)
Sets initial mapping cache so we do not need to download everything.
- Parameters:
mappings (dict) – Initial mapping cache to use
- setregion(apiRegion=None)
Change the region of an existing Cloud object
Sets the hostname based on the given apiRegion
- Parameters:
apiRegion (str) – The region code of the new region as found in
API_REGIONS- Raises:
KeyError – Region code not found in
API_REGIONS
- API_REGIONS = {}
List of API Regions
API Regions:
cn - ‘China Data Center’
us - ‘Western America Data Center’
az - ‘Alias for us’
us-e - ‘Eastern America Data Center’
ue - ‘Alias for us-e’
eu - ‘Central Europe Data Center’
eu-w - ‘Western Europe Data Center’
we - ‘Alias for eu-w’
in - ‘India Datacenter’
sg - ‘Singapore Data Center’
- API_REGION_ALIASES = {}
List of API Region aliases
Aliases:
az - ‘us’
ue - ‘us-e’
we - ‘eu-w’