Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutsuku authored Dec 24, 2021
2 parents f6835d1 + d07c511 commit 58956b2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 16 deletions.
7 changes: 4 additions & 3 deletions example/mq.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions tuya_iot/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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"]
Expand Down
19 changes: 14 additions & 5 deletions tuya_iot/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions tuya_iot/openmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@
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

LINK_ID = f"tuya-iot-app-sdk-python.{uuid.uuid1()}"
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."""
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion tuya_iot/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""tuya_iot version."""

VERSION = "0.6.3"
VERSION = "0.6.4"

0 comments on commit 58956b2

Please sign in to comment.