-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy path__init__.py
168 lines (144 loc) · 5.17 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
name = "growattServer"
import datetime
from enum import IntEnum
import hashlib
import json
import requests
import warnings
def hash_password(password):
"""
Normal MD5, except add c if a byte of the digest is less than 10.
"""
password_md5 = hashlib.md5(password.encode('utf-8')).hexdigest()
for i in range(0, len(password_md5), 2):
if password_md5[i] == '0':
password_md5 = password_md5[0:i] + 'c' + password_md5[i + 1:]
return password_md5
class Timespan(IntEnum):
day = 1
month = 2
class GrowattApi:
server_url = 'http://server.growatt.com/'
def __init__(self):
self.session = requests.Session()
def get_url(self, page):
"""
Simple helper function to get the page url/
"""
return self.server_url + page
def login(self, username, password):
"""
Log the user in.
"""
password_md5 = hash_password(password)
response = self.session.post(self.get_url('LoginAPI.do'), data={
'userName': username,
'password': password_md5
})
data = json.loads(response.content.decode('utf-8'))
return data['back']
def plant_list(self, user_id):
"""
Get a list of plants connected to this account.
"""
response = self.session.get(self.get_url('PlantListAPI.do'),
params={'userId': user_id},
allow_redirects=False)
if response.status_code != 200:
raise RuntimeError("Request failed: %s", response)
data = json.loads(response.content.decode('utf-8'))
return data['back']
def plant_detail(self, plant_id, timespan, date):
"""
Get plant details for specified timespan.
"""
assert timespan in Timespan
if timespan == Timespan.day:
date_str = date.strftime('%Y-%m-%d')
elif timespan == Timespan.month:
date_str = date.strftime('%Y-%m')
response = self.session.get(self.get_url('PlantDetailAPI.do'), params={
'plantId': plant_id,
'type': timespan.value,
'date': date_str
})
data = json.loads(response.content.decode('utf-8'))
return data['back']
def inverter_data(self, inverter_id, date):
"""
Get inverter data for specified date or today.
"""
if date is None:
date = datetime.date.today()
date_str = date.strftime('%Y-%m-%d')
response = self.session.get(self.get_url('newInverterAPI.do'), params={
'op': 'getInverterData',
'id': inverter_id,
'type': 1,
'date': date_str
})
data = json.loads(response.content.decode('utf-8'))
return data
def inverter_detail(self, inverter_id):
"""
Get "All parameters" from PV inverter.
"""
response = self.session.get(self.get_url('newInverterAPI.do'), params={
'op': 'getInverterDetailData_two',
'inverterId': inverter_id
})
data = json.loads(response.content.decode('utf-8'))
return data
def storage_detail(self, storage_id):
"""
Get "All parameters" from battery storage.
"""
response = self.session.get(self.get_url('newStorageAPI.do'), params={
'op': 'getStorageInfo_sacolar',
'storageId': storage_id
})
data = json.loads(response.content.decode('utf-8'))
return data
def storage_params(self, storage_id):
"""
Get much more detail from battery storage.
"""
response = self.session.get(self.get_url('newStorageAPI.do'), params={
'op': 'getStorageParams_sacolar',
'storageId': storage_id
})
data = json.loads(response.content.decode('utf-8'))
return data
def storage_energy_overview(self, plant_id, storage_id):
"""
Get some energy/generation overview data.
"""
response = self.session.post(self.get_url('newStorageAPI.do?op=getEnergyOverviewData_sacolar'), params={
'plantId': plant_id,
'storageSn': storage_id
})
data = json.loads(response.content.decode('utf-8'))
return data['obj']
def inverter_list(self, plant_id):
"""
Use device_list, it's more descriptive since the list contains more than inverters.
"""
warnings.warn("This function may be deprecated in the future because naming is not correct, use device_list instead", DeprecationWarning)
return self.device_list(plant_id)
def device_list(self, plant_id):
"""
Get a list of all devices connected to plant.
"""
return self.plant_info(plant_id)['deviceList']
def plant_info(self, plant_id):
"""
Get basic plant information with device list.
"""
response = self.session.get(self.get_url('newPlantAPI.do'), params={
'op': 'getAllDeviceListThree',
'plantId': plant_id,
'pageNum': 1,
'pageSize': 1
})
data = json.loads(response.content.decode('utf-8'))
return data