Skip to content
This repository was archived by the owner on Oct 7, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions tuyaha/tuyaapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import time

import requests
from requests.exceptions import ConnectionError as RequestsConnectionError
from requests.exceptions import HTTPError as RequestsHTTPError

from tuyaha.devices.factory import get_tuya_device

Expand Down Expand Up @@ -45,16 +47,26 @@ def init(self, username, password, countryCode, bizType=""):
return SESSION.devices

def get_access_token(self):
response = requests.post(
(TUYACLOUDURL + "/homeassistant/auth.do").format(SESSION.region),
data={
"userName": SESSION.username,
"password": SESSION.password,
"countryCode": SESSION.countryCode,
"bizType": SESSION.bizType,
"from": "tuya",
},
)
try:
response = requests.post(
(TUYACLOUDURL + "/homeassistant/auth.do").format(SESSION.region),
data={
"userName": SESSION.username,
"password": SESSION.password,
"countryCode": SESSION.countryCode,
"bizType": SESSION.bizType,
"from": "tuya",
},
)
except RequestsConnectionError as ex:
raise TuyaNetException from ex

if response.status_code >= 500:
try:
response.raise_for_status()
except RequestsHTTPError as ex:
raise TuyaServerException from ex

response_json = response.json()
if response_json.get("responseStatus") == "error":
message = response_json.get("errorMsg")
Expand All @@ -77,7 +89,6 @@ def get_access_token(self):
def check_access_token(self):
if SESSION.username == "" or SESSION.password == "":
raise TuyaAPIException("can not find username or password")
return
Comment thread
PaulAnnekov marked this conversation as resolved.
if SESSION.accessToken == "" or SESSION.refreshToken == "":
self.get_access_token()
elif SESSION.expireTime <= REFRESHTIME + int(time.time()):
Expand Down Expand Up @@ -168,3 +179,11 @@ def _request(self, name, namespace, devId=None, payload={}):

class TuyaAPIException(Exception):
pass


class TuyaNetException(Exception):
pass


class TuyaServerException(Exception):
pass