Skip to content

Commit 797e531

Browse files
committed
Throw GrowattApiIncorrectPasswordException if the username / password is incorrect
Throw GrowattApiUserAgentBlockedException if User Agent is being blocked (HTTP 403 error) Throw requests.exceptions.HTTPError for other API HTTP errors Throw GrowattApiUnknownException for unknown JSON response errors
1 parent c117f1a commit 797e531

1 file changed

Lines changed: 38 additions & 6 deletions

File tree

growattServer/__init__.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ class Timespan(IntEnum):
2323
day = 1
2424
month = 2
2525

26+
27+
class GrowattApiUnknownException(Exception):
28+
pass
29+
30+
31+
class GrowattApiIncorrectPasswordException(Exception):
32+
pass
33+
34+
35+
class GrowattApiUserAgentBlockedException(Exception):
36+
pass
37+
38+
2639
class GrowattApi:
2740
server_url = 'https://server-api.growatt.com/'
2841
agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)"
@@ -129,12 +142,31 @@ def login(self, username, password, is_password_hashed=False):
129142
'userName': username,
130143
'password': password
131144
})
132-
data = json.loads(response.content.decode('utf-8'))['back']
133-
if data['success']:
134-
data.update({
135-
'userId': data['user']['id'],
136-
'userLevel': data['user']['rightlevel']
137-
})
145+
146+
if response.status_code == 403:
147+
# detect if the User Agent is being blocked by Growatt WAF
148+
raise GrowattApiUserAgentBlockedException()
149+
else:
150+
# handle any other HTTP errors
151+
response.raise_for_status()
152+
153+
json_doc = response.content.decode('utf-8')
154+
json_dict = json.loads(json_doc)
155+
156+
if 'back' not in json_dict:
157+
raise GrowattApiUnknownException()
158+
159+
data = json_dict['back']
160+
if 'success' not in data:
161+
raise GrowattApiUnknownException()
162+
163+
if not data['success']:
164+
raise GrowattApiIncorrectPasswordException()
165+
166+
data.update({
167+
'userId': data['user']['id'],
168+
'userLevel': data['user']['rightlevel']
169+
})
138170
return data
139171

140172
def plant_list(self, user_id):

0 commit comments

Comments
 (0)