bestlab_platform.tuya

class bestlab_platform.tuya.TuyaOpenAPI(endpoint, access_id, access_secret, lang='en', auto_connect=True)[source]

Bases: object

Open Api.

Typical usage example:

openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY)

Init TuyaOpenAPI.

__init__(endpoint, access_id, access_secret, lang='en', auto_connect=True)[source]

Init TuyaOpenAPI.

connect()[source]

Connect to Tuya Cloud.

Returns

connect response

Return type

response

is_connect()[source]

Whether we have an access token. Note: will return true even if the access token is expired. Token refreshing is handled internally.

Return type

bool

get(path, params=None)[source]

Http Get.

Requests the server to return specified resources.

Parameters
  • path (str) – api path

  • params (map) – request parameter

Returns

response body

Return type

response

post(path, body=None)[source]

Http Post.

Requests the server to update specified resources.

Parameters
  • path (str) – api path

  • body (map) – request body

Returns

response body

Return type

response

put(path, body=None)[source]

Http Put.

Requires the server to perform specified operations.

Parameters
  • path (str) – api path

  • body (map) – request body

Returns

response body

Return type

response

delete(path, params=None)[source]

Http Delete.

Requires the server to delete specified resources.

Parameters
  • path (str) – api path

  • params (map) – request param

Returns

response body

Return type

response

class bestlab_platform.tuya.TuyaTokenInfo(token_response)[source]

Bases: object

Tuya token info.

access_token

Access token.

expire_time

Valid period in seconds.

refresh_token

Refresh token.

uid

Tuya user ID.

# platform_url

user region platform url

Init TuyaTokenInfo.

__init__(token_response)[source]

Init TuyaTokenInfo.

class bestlab_platform.tuya.TuyaDeviceManager(api, device_map=None, device_list=None)[source]

Bases: object

Manages multiple devices and provides functions to call APIs for all devices in batch Note: This is different from upstream Tuya SDK.

__init__(api, device_map=None, device_list=None)[source]
get_device_status_in_batch()[source]

Get device status for all devices in this instance in batch

Returns

API response in a dictionary.

get_device_log_in_batch(start_timestamp, end_timestamp, warn_on_empty_data=False, type_=7)[source]

Get device log stored on the Tuya platform. Note that free version of Tuya Platform only stores 7 days’ data.

Parameters
  • start_timestamp (int | float | str) – Start timestamp for log to be queried. Must be an 10 digit or 13 digit unix timestamp. Note that free version of Tuya only keeps one week’s data.

  • end_timestamp (int | float | str) – End timestamp for log to be queried. Must be an 10 digit or 13 digit unix timestamp Note that free version of Tuya only keeps one week’s data.

  • warn_on_empty_data (bool) – If True, print a warning message to the logger an empty page or empty final result is detected. Default: False.

  • type (int) – Usually this field should be 7 (“the actual data” from the device), unless you want something else. See https://developer.tuya.com/en/docs/cloud/device-management?id=K9g6rfntdz78a#sjlx1

Returns

Map of device name -> device log.

get_device_info_in_batch(include_device_status=True)[source]

Get device info in batch

Parameters

include_device_status (bool) – Include device status in the return fields. Default: True

Returns

API response in a dictionary.

get_factory_info_in_batch()[source]

“Query the factory information of the devices. Possible return fields are: id, uuid, sn, mac.

Returns

API response in a dictionary.

send_command_in_batch(commands)[source]

Issue standard instructions to control equipments.

Parameters

commands (list) – issue commands.

Returns

API response in a dictionary.

class bestlab_platform.tuya.SmartHomeDeviceAPI(api)[source]

Bases: object

Tuya Smart Home Device API. See https://developer.tuya.com/en/docs/cloud/device-management?id=K9g6rfntdz78a for the list of APIs.

Example

tuya_api = tuya_api = TuyaOpenAPI(”https://openapi.tuyaus.com”, CLIENT_ID, CLIENT_SECRET) device_api = SmartHomeDeviceAPI(tuya_api) print(device_api.get_device_status(“YOUR_DEVICE_ID_HERE”))

__init__(api)[source]
get_device_info(device_id, include_device_status=True)[source]

Get device details, including properties and the latest status of the device.

Parameters
  • device_id (str) – Device ID

  • include_device_status (bool) – Whether device status field should be included. Default: True

Returns

API Response in a dictionary.

get_device_list_info(device_ids, include_device_status=True)[source]

Get device info for a list of devices.

Parameters
  • device_ids (list[str]) – a list of device ids.

  • include_device_status – Include device status in the return fields. Default: True

Returns

API Response in a dictionary.

get_device_status(device_id)[source]

Get device status

Parameters

device_id (str) – Device ID

Returns

API Response in a dictionary.

get_device_list_status(device_ids)[source]

Get device status for a list of devices.

Parameters

device_ids (list[str]) – List of Device IDs.

Returns

API Response in a dictionary.

get_factory_info(device_ids)[source]

Query the factory information of the device. Possible return fields are: id, uuid, sn, mac.

Parameters

device_ids (list[str]) – List of Device IDs.

Returns

API Response in a dictionary.

get_device_functions(device_id)[source]

Get the instruction set supported by the device, and the obtained instructions can be used to issue control.

Parameters

device_id (str) – Device ID.

Returns

API Response in a dictionary.

get_category_functions(category_id)[source]

Query the instruction set supported by Tuya Platform in the given category. You should not need this unless you are a platform developer.

See also: https://iot.tuya.com/cloud/explorer?id=p1622082860767nqhjxa&groupId=group-home&interfaceId=470224763027539

Parameters

category_id (str) – Product category.

Returns

API Response in a dictionary.

get_device_specification(device_id)[source]

Acquire the instruction set and status set supported by the device according to the device ID.

Parameters

device_id (str) – Device ID.

Returns

API Response in a dictionary.

send_commands(device_id, commands)[source]

Issue standard instructions to control equipment

Parameters
  • device_id (str) – Device ID.

  • commands – issue commands.

Returns

API Response in a dictionary.

get_device_log(device_id, start_timestamp, end_timestamp, device_name=None, warn_on_empty_data=False, type_=7)[source]

Get device log stored on the Tuya platform. Note that free version of Tuya Platform only stores 7 days’ data.

Parameters
  • device_id (str) – Device ID.

  • start_timestamp (int | float | str) – Start timestamp for log to be queried. Must be an 10 digit or 13 digit unix timestamp. Note that free version of Tuya only keeps one week’s data.

  • end_timestamp (int | float | str) – End timestamp for log to be queried. Must be an 10 digit or 13 digit unix timestamp Note that free version of Tuya only keeps one week’s data.

  • device_name (str) – User friendly name for your convenience. It can be any string you like, such as “PIR3”

  • warn_on_empty_data (bool) – If True, print a warning message to the logger an empty page or empty final result is detected. Default: False.

  • type (int) – Usually this field should be 7 (“the actual data” from the device), unless you want something else. See https://developer.tuya.com/en/docs/cloud/device-management?id=K9g6rfntdz78a#sjlx1

Returns

A list of device logs. Note that the return type is not a dictionary and is not the raw response, because multiple page is expected.