Skip to content

Commit f46f7ee

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 9ae4856 commit f46f7ee

File tree

1 file changed

+38
-6
lines changed

1 file changed

+38
-6
lines changed

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)"
@@ -132,12 +145,31 @@ def login(self, username, password, is_password_hashed=False):
132145
'userName': username,
133146
'password': password
134147
})
135-
data = json.loads(response.content.decode('utf-8'))['back']
136-
if data['success']:
137-
data.update({
138-
'userId': data['user']['id'],
139-
'userLevel': data['user']['rightlevel']
140-
})
148+
149+
if response.status_code == 403:
150+
# detect if the User Agent is being blocked by Growatt WAF
151+
raise GrowattApiUserAgentBlockedException()
152+
else:
153+
# handle any other HTTP errors
154+
response.raise_for_status()
155+
156+
json_doc = response.content.decode('utf-8')
157+
json_dict = json.loads(json_doc)
158+
159+
if 'back' not in json_dict:
160+
raise GrowattApiUnknownException()
161+
162+
data = json_dict['back']
163+
if 'success' not in data:
164+
raise GrowattApiUnknownException()
165+
166+
if not data['success']:
167+
raise GrowattApiIncorrectPasswordException()
168+
169+
data.update({
170+
'userId': data['user']['id'],
171+
'userLevel': data['user']['rightlevel']
172+
})
141173
return data
142174

143175
def plant_list(self, user_id):

0 commit comments

Comments
 (0)