diff --git a/example/mq.py b/example/mq.py index 86e0ef4..f629c38 100644 --- a/example/mq.py +++ b/example/mq.py @@ -1,13 +1,14 @@ import logging -from tuya_iot import TuyaOpenAPI, TuyaOpenMQ, tuya_logger +from tuya_iot import TuyaOpenAPI, TuyaOpenMQ, TUYA_LOGGER from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD -tuya_logger.setLevel(logging.DEBUG) + +TUYA_LOGGER.setLevel(logging.DEBUG) # Init openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY) -openapi.connect(USERNAME, PASSWORD) +openapi.connect(USERNAME, PASSWORD, "86", 'tuyaSmart') # Receive device message diff --git a/tuya_iot/device.py b/tuya_iot/device.py index 74d193c..c38bffa 100644 --- a/tuya_iot/device.py +++ b/tuya_iot/device.py @@ -625,7 +625,7 @@ def get_category_functions(self, categoryId: str) -> dict[str, Any]: # https://developer.tuya.com/en/docs/cloud/device-control?id=K95zu01ksols7#title-27-Get%20the%20specifications%20and%20properties%20of%20the%20device%2C%20including%20the%20instruction%20set%20and%20status%20set def get_device_specification(self, device_id: str) -> dict[str, str]: - return self.api.get(f"/v1.0/devices/{device_id}/specifications") + return self.api.get(f"/v1.2/iot-03/devices/{device_id}/specification") def get_device_stream_allocate( self, device_id: str, stream_type: Literal["flv", "hls", "rtmp", "rtsp"] @@ -693,7 +693,7 @@ def get_category_functions(self, categoryId: str) -> dict[str, Any]: # https://developer.tuya.com/en/docs/cloud/device-control?id=K95zu01ksols7#title-27-Get%20the%20specifications%20and%20properties%20of%20the%20device%2C%20including%20the%20instruction%20set%20and%20status%20set def get_device_specification(self, device_id: str) -> dict[str, str]: - return self.api.get(f"/v1.0/iot-03/devices/{device_id}/specification") + return self.api.get(f"/v1.2/iot-03/devices/{device_id}/specification") def get_device_stream_allocate( self, device_id: str, stream_type: Literal["flv", "hls", "rtmp", "rtsp"] diff --git a/tuya_iot/openapi.py b/tuya_iot/openapi.py index 493f593..f136d5c 100644 --- a/tuya_iot/openapi.py +++ b/tuya_iot/openapi.py @@ -15,7 +15,8 @@ TUYA_ERROR_CODE_TOKEN_INVALID = 1010 -TO_C_REFRESH_TOKEN_API_PRE = "/v1.0/iot-03/users/token/" +TO_C_CUSTOM_REFRESH_TOKEN_API = "/v1.0/iot-03/users/token/" +TO_C_SMART_HOME_REFRESH_TOKEN_API = "v1.0/token" TO_C_CUSTOM_TOKEN_API = "/v1.0/iot-03/users/login" TO_C_SMART_HOME_TOKEN_API = "/v1.0/iot-01/associated-users/actions/authorized-login" @@ -154,9 +155,15 @@ def __refresh_access_token_if_need(self, path: str): return self.token_info.access_token = "" - response = self.post( - TO_C_REFRESH_TOKEN_API_PRE + self.token_info.refresh_token - ) + + if self.auth_type == AuthType.CUSTOM: + response = self.post( + TO_C_CUSTOM_REFRESH_TOKEN_API + self.token_info.refresh_token + ) + else: + response = self.post( + TO_C_SMART_HOME_REFRESH_TOKEN_API + self.token_info.refresh_token + ) self.token_info = TuyaTokenInfo(response) @@ -241,7 +248,9 @@ def __request( "lang": self.lang, } - if path == self.__login_path or path.startswith(TO_C_REFRESH_TOKEN_API_PRE): + if path == self.__login_path or \ + path.startswith(TO_C_CUSTOM_REFRESH_TOKEN_API) or\ + path.startswith(TO_C_SMART_HOME_REFRESH_TOKEN_API): headers["dev_lang"] = "python" headers["dev_version"] = VERSION headers["dev_channel"] = self.dev_channel diff --git a/tuya_iot/openmq.py b/tuya_iot/openmq.py index aaa2b69..2d6bbba 100644 --- a/tuya_iot/openmq.py +++ b/tuya_iot/openmq.py @@ -13,7 +13,7 @@ from Crypto.Cipher import AES from paho.mqtt import client as mqtt -from .openapi import TuyaOpenAPI +from .openapi import TO_C_SMART_HOME_REFRESH_TOKEN_API, TuyaOpenAPI from .openlogging import logger from .tuya_enums import AuthType @@ -21,6 +21,9 @@ GCM_TAG_LENGTH = 16 CONNECT_FAILED_NOT_AUTHORISED = 5 +TO_C_CUSTOM_MQTT_CONFIG_API = "/v1.0/iot-03/open-hub/access-config" +TO_C_SMART_HOME_MQTT_CONFIG_API = "/v1.0/open-hub/access/config" + class TuyaMQConfig: """Tuya mqtt config.""" @@ -57,7 +60,9 @@ def __init__(self, api: TuyaOpenAPI) -> None: def _get_mqtt_config(self) -> Optional[TuyaMQConfig]: response = self.api.post( - "/v1.0/iot-03/open-hub/access-config", + TO_C_CUSTOM_MQTT_CONFIG_API + if (self.api.auth_type == AuthType.CUSTOM) + else TO_C_SMART_HOME_MQTT_CONFIG_API, { "uid": self.api.token_info.uid, "link_id": LINK_ID, @@ -89,16 +94,16 @@ def _decode_mq_message(self, b64msg: str, password: str, t: str) -> dict[str, An # get iv buffer iv_length = int.from_bytes(buffer[0:4], byteorder="big") - iv_buffer = buffer[4 : iv_length + 4] + iv_buffer = buffer[4: iv_length + 4] # get data buffer - data_buffer = buffer[iv_length + 4 : len(buffer) - GCM_TAG_LENGTH] + data_buffer = buffer[iv_length + 4: len(buffer) - GCM_TAG_LENGTH] # aad aad_buffer = str(t).encode("utf8") # tag - tag_buffer = buffer[len(buffer) - GCM_TAG_LENGTH :] + tag_buffer = buffer[len(buffer) - GCM_TAG_LENGTH:] cipher = AES.new(key.encode("utf8"), AES.MODE_GCM, nonce=iv_buffer) cipher.update(aad_buffer) diff --git a/tuya_iot/version.py b/tuya_iot/version.py index 1b17130..df5fbdf 100644 --- a/tuya_iot/version.py +++ b/tuya_iot/version.py @@ -1,3 +1,4 @@ """tuya_iot version.""" -VERSION = "0.6.3" +VERSION = "0.6.4" +