Skip to content

Commit

Permalink
[remove] to B logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutsuku committed Aug 19, 2021
1 parent 5136a7d commit eb32532
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 313 deletions.
4 changes: 2 additions & 2 deletions example/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from env import ENDPOINT, ACCESS_ID, ACCESS_KEY, USERNAME, PASSWORD
from tuya_iot import (
TuyaOpenAPI,
DevelopMethod,
AuthType,
TuyaOpenMQ,
TuyaDeviceManager,
TuyaHomeManager,
Expand All @@ -18,7 +18,7 @@

tuya_logger.setLevel(logging.DEBUG)
# Init
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, DevelopMethod.CUSTOM)
openapi = TuyaOpenAPI(ENDPOINT, ACCESS_ID, ACCESS_KEY, AuthType.CUSTOM)

openapi.connect(USERNAME, PASSWORD)
openmq = TuyaOpenMQ(openapi)
Expand Down
43 changes: 0 additions & 43 deletions example/to_b_connect.py

This file was deleted.

7 changes: 1 addition & 6 deletions tuya_iot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
from .openmq import TuyaOpenMQ
from .asset import TuyaAssetManager
from .device import TuyaDeviceManager, TuyaDevice, TuyaDeviceListener
from .tuya_enums import DevelopMethod, AuthType, TuyaCloudOpenAPIEndpoint, TuyaCloudPulsarWSEndpoint, TuyaCloudPulsarTopic
from .tuya_enums import AuthType, TuyaCloudOpenAPIEndpoint
from .home import TuyaHomeManager, TuyaScene
from .openpulsar import TuyaOpenPulsar
from .openlogging import TUYA_LOGGER
from .version import VERSION

Expand All @@ -18,14 +17,10 @@
"TuyaDeviceManager",
"TuyaDevice",
"TuyaDeviceListener",
"DevelopMethod",
"AuthType",
"TuyaCloudOpenAPIEndpoint",
"TuyaCloudPulsarWSEndpoint",
"TuyaCloudPulsarTopic",
"TuyaHomeManager",
"TuyaScene",
"TuyaOpenPulsar",
"TUYA_LOGGER"
]
__version__ = VERSION
11 changes: 5 additions & 6 deletions tuya_iot/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from .openapi import TuyaOpenAPI
from .openmq import TuyaOpenMQ
from .tuya_enums import DevelopMethod
from .tuya_enums import AuthType
from .openlogging import logger

PROTOCOL_DEVICE_REPORT = 4
Expand Down Expand Up @@ -153,11 +153,10 @@ def __init__(self, api: TuyaOpenAPI, mq: TuyaOpenMQ):
"""Tuya device manager init."""
self.api = api
self.mq = mq
self.device_manage = (
SmartHomeDeviceManage(api)
if (api.develop_method == DevelopMethod.SMART_HOME)
else IndustrySolutionDeviceManage(api)
)
if (api.auth_type == AuthType.SMART_HOME):
self.device_manage = SmartHomeDeviceManage(api)
else:
self.device_manage = IndustrySolutionDeviceManage(api)

mq.add_message_listener(self.on_message)
self.device_map: Dict[str, TuyaDevice] = {}
Expand Down
10 changes: 5 additions & 5 deletions tuya_iot/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from .openapi import TuyaOpenAPI
from .openmq import TuyaOpenMQ
from .tuya_enums import DevelopMethod
from .tuya_enums import AuthType
from .asset import TuyaAssetManager
from .device import TuyaDeviceManager

Expand Down Expand Up @@ -44,7 +44,7 @@ def __init__(
def update_device_cache(self):
"""Update home's devices cache."""
self.device_manager.device_map.clear()
if self.api.develop_method == DevelopMethod.CUSTOM:
if self.api.auth_type == AuthType.CUSTOM:
device_ids = []
asset_manager = TuyaAssetManager(self.api)

Expand All @@ -56,7 +56,7 @@ def update_device_cache(self):
# device_ids += asset_manager.get_device_list(asset_id)
if len(device_ids) > 0:
self.device_manager.update_device_caches(device_ids)
elif self.api.develop_method == DevelopMethod.SMART_HOME:
elif self.api.auth_type == AuthType.SMART_HOME:
self.device_manager.update_device_list_in_smart_home()

def __query_device_ids(
Expand All @@ -77,7 +77,7 @@ def __query_device_ids(

def query_scenes(self) -> list:
"""Query home scenes, only in SMART_HOME project type."""
if self.api.develop_method == DevelopMethod.CUSTOM:
if self.api.auth_type == AuthType.CUSTOM:
return []

response = self.api.get(f"/v1.0/users/{self.api.token_info.uid}/homes")
Expand All @@ -102,7 +102,7 @@ def trigger_scene(self,
home_id: str,
scene_id: str) -> Dict[str, Any]:
"""Trigger home scene"""
if self.api.develop_method == DevelopMethod.SMART_HOME:
if self.api.auth_type == AuthType.SMART_HOME:
return self.api.post(f"/v1.0/homes/{home_id}/scenes/{scene_id}/trigger")

return dict()
64 changes: 25 additions & 39 deletions tuya_iot/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@
import requests

from .openlogging import filter_logger, logger
from .tuya_enums import AuthType, DevelopMethod
from .tuya_enums import AuthType
from .version import VERSION

TUYA_ERROR_CODE_TOKEN_INVALID = 1010

TO_B_REFRESH_TOKEN_API = "/v1.0/token/{}"
TO_C_REFRESH_TOKEN_API = "/v1.0/iot-03/users/token/{}"

TO_B_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 @@ -64,8 +62,7 @@ def __init__(
endpoint: str,
access_id: str,
access_secret: str,
develop_method: DevelopMethod = DevelopMethod.SMART_HOME,
auth_type: AuthType = AuthType.TO_C,
auth_type: AuthType = AuthType.SMART_HOME,
lang: str = "en",
):
"""Init TuyaOpenAPI."""
Expand All @@ -77,8 +74,7 @@ def __init__(
self.lang = lang

self.auth_type = auth_type
self.develop_method = develop_method
if self.develop_method == DevelopMethod.CUSTOM:
if self.auth_type == AuthType.CUSTOM:
self.__login_path = TO_C_CUSTOM_TOKEN_API
else:
self.__login_path = TO_C_SMART_HOME_TOKEN_API
Expand Down Expand Up @@ -165,14 +161,9 @@ def __refresh_access_token_if_need(self, path: str):
return

self.token_info.access_token = ""
if self.auth_type == AuthType.TO_C:
response = self.post(
TO_C_REFRESH_TOKEN_API.format(self.token_info.refresh_token)
)
else:
response = self.post(
TO_B_REFRESH_TOKEN_API.format(self.token_info.refresh_token)
)
response = self.post(
TO_C_REFRESH_TOKEN_API.format(self.token_info.refresh_token)
)

self.token_info = TuyaTokenInfo(response)

Expand Down Expand Up @@ -203,31 +194,26 @@ def connect(
self.__country_code = country_code
self.__schema = schema

if self.auth_type == AuthType.TO_B:
# TO B connect.
response = self.get(TO_B_TOKEN_API, {"grant_type": 1})
if self.auth_type == AuthType.CUSTOM:
response = self.post(
TO_C_CUSTOM_TOKEN_API,
{
"username": username,
"password": hashlib.sha256(password.encode("utf8"))
.hexdigest()
.lower(),
},
)
else:
# To C connect.
if self.develop_method == DevelopMethod.CUSTOM:
response = self.post(
TO_C_CUSTOM_TOKEN_API,
{
"username": username,
"password": hashlib.sha256(password.encode("utf8"))
.hexdigest()
.lower(),
},
)
else:
response = self.post(
TO_C_SMART_HOME_TOKEN_API,
{
"username": username,
"password": hashlib.md5(password.encode("utf8")).hexdigest(),
"country_code": country_code,
"schema": schema,
},
)
response = self.post(
TO_C_SMART_HOME_TOKEN_API,
{
"username": username,
"password": hashlib.md5(password.encode("utf8")).hexdigest(),
"country_code": country_code,
"schema": schema,
},
)

if not response["success"]:
return response
Expand Down
6 changes: 3 additions & 3 deletions tuya_iot/openmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from Crypto.Cipher import AES

from .openapi import TuyaOpenAPI
from .tuya_enums import DevelopMethod
from .tuya_enums import AuthType
from .openlogging import logger

LINK_ID = "tuya-iot-app-sdk-python.{}".format(uuid.uuid1())
Expand Down Expand Up @@ -72,7 +72,7 @@ def _get_mqtt_config(self) -> TuyaMQConfig:
"link_type": "mqtt",
"topics": "device",
"msg_encrypted_version": "2.0"
if (self.api.develop_method == DevelopMethod.CUSTOM)
if (self.api.auth_type == AuthType.CUSTOM)
else "1.0",
},
)
Expand All @@ -87,7 +87,7 @@ def _decode_mq_message(
) -> Dict[str, Any]:
key = password[8:24]

if self.api.develop_method == DevelopMethod.SMART_HOME:
if self.api.auth_type == AuthType.SMART_HOME:
cipher = AES.new(key.encode("utf8"), AES.MODE_ECB)
msg = cipher.decrypt(base64.b64decode(b64msg))
padding_bytes = msg[-1]
Expand Down
Loading

0 comments on commit eb32532

Please sign in to comment.