Skip to content

Commit 11f04bf

Browse files
committed
Throw requests.exceptions.HTTPError if the UserAgent is being blocked by the Growatt WAF
Throw GrowattApiLoginException if the username / password is incorrect
1 parent 4b332e5 commit 11f04bf

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

growattServer/__init__.py

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

26+
27+
class GrowattApiLoginException(Exception):
28+
pass
29+
30+
2631
class GrowattApi:
2732
server_url = 'https://server-api.growatt.com/'
2833
agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)"
@@ -130,20 +135,22 @@ def login(self, username, password, is_password_hashed=False):
130135
'password': password
131136
})
132137

133-
if response.status_code == 200:
134-
json_data = response.content.decode('utf-8')
135-
data = json.loads(json_data)['back']
136-
if data['success']:
138+
# throw requests.exceptions.HTTPError if appropriate - occurs if the UserAgent is being blocked by the Growatt WAF
139+
response.raise_for_status()
140+
141+
json_doc = response.content.decode('utf-8')
142+
json_dict = json.loads(json_doc)
143+
144+
if 'back' in json_dict:
145+
data = json_dict['back']
146+
if 'success' in data and data['success']:
137147
data.update({
138148
'userId': data['user']['id'],
139149
'userLevel': data['user']['rightlevel']
140150
})
141-
return data
151+
return data
142152

143-
elif response.status_code == 403:
144-
raise RuntimeError("Login failed")
145-
else:
146-
raise RuntimeError("Unknown login error")
153+
raise GrowattApiLoginException()
147154

148155
def plant_list(self, user_id):
149156
"""

0 commit comments

Comments
 (0)