Skip to content
Open
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
46 changes: 39 additions & 7 deletions growattServer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ class Timespan(IntEnum):
day = 1
month = 2


class GrowattApiUnknownException(Exception):
pass


class GrowattApiIncorrectPasswordException(Exception):
pass


class GrowattApiUserAgentBlockedException(Exception):
pass


class GrowattApi:
server_url = 'https://server-api.growatt.com/'
server_url = 'https://server.growatt.com/'
agent_identifier = "Dalvik/2.1.0 (Linux; U; Android 12; https://github.com/indykoning/PyPi_GrowattServer)"

def __init__(self, add_random_user_id=False, agent_identifier=None):
Expand Down Expand Up @@ -132,12 +145,31 @@ def login(self, username, password, is_password_hashed=False):
'userName': username,
'password': password
})
data = json.loads(response.content.decode('utf-8'))['back']
if data['success']:
data.update({
'userId': data['user']['id'],
'userLevel': data['user']['rightlevel']
})

if response.status_code == 403:
# detect if the User Agent is being blocked by Growatt WAF
raise GrowattApiUserAgentBlockedException()
else:
# handle any other HTTP errors
response.raise_for_status()

json_doc = response.content.decode('utf-8')
json_dict = json.loads(json_doc)

if 'back' not in json_dict:
raise GrowattApiUnknownException()

data = json_dict['back']
if 'success' not in data:
raise GrowattApiUnknownException()

if not data['success']:
raise GrowattApiIncorrectPasswordException()

data.update({
'userId': data['user']['id'],
'userLevel': data['user']['rightlevel']
})
return data

def plant_list(self, user_id):
Expand Down