-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
203 lines (175 loc) · 8.68 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import enum
from aiohttp import ClientSession, FormData, ClientResponse
from pydantic import BaseModel
from datetime import datetime
class WrongLogin(Exception):
pass
class UnknownError(Exception):
def __init__(self, http_code: int, code: int | None, body: dict | None):
self.code = code
self.http_code = http_code
self.body = body
super().__init__(f"UnknownError - HTTP Code: {http_code} Code: {code}, Body: {body}")
class DeviceOffline(Exception):
pass
class TokenExpired(Exception):
pass
async def _process_response(resp: ClientResponse) -> dict:
if not resp.ok:
raise UnknownError(resp.status, None, None)
data = await resp.json()
if data["code"] == 2006:
raise WrongLogin()
elif data["code"] == 1001:
raise DeviceOffline()
elif data["code"] == 3003:
raise TokenExpired()
elif data["code"] != 0:
raise UnknownError(resp.status, data["code"], data)
return data["data"]
class Api:
refresh_token: str = None
access_token: str = None
user_id: str = None
base_url: str = "https://app.api.apsystemsema.com:9223"
language: str = "de_DE"
app_id: str = "4029817264d4821d0164d4821dd80015"
app_secret: str = "EZAd2023"
@classmethod
async def init(cls, username: str | None = None, password: str | None = None, access_token: str | None = None,
refresh_token: str | None = None):
self = cls()
if username is not None and password is not None:
await self._log_user_in(username=username, password=password)
elif access_token is not None and refresh_token is not None:
# Initialization code for the second case
self.access_token = access_token
self.refresh_token = refresh_token
else:
raise ValueError("Invalid parameters provided")
return self
async def _log_user_in(self, username: str, password: str):
formData = FormData(
{"language": self.language, "password": password, "app_id": self.app_id, "app_secret": self.app_secret,
"username": username})
async with ClientSession() as c, c.post(
f"{self.base_url}/api/token/generateToken/user/login?language={self.language}",
data=formData, timeout=5) as resp:
data = await _process_response(resp)
self.access_token = data["access_token"]
self.refresh_token = data["refresh_token"]
self.user_id = data["user_id"]
return None
class _ListInvertersResponse(BaseModel):
device_name: str | None
communicationStatus: int
runningStatus: int
system_id: str
inverter_dev_id: str
type: str
async def list_inverters(self) -> list[_ListInvertersResponse]:
async with ClientSession() as c, c.get(
f"{self.base_url}/aps-api-web/api/v2/data/device/ezInverter/list/{self.user_id}?language={self.language}&systemId={self.user_id}",
headers={"Authorization": f"Bearer {self.access_token}"}, timeout=5) as resp:
data = await _process_response(resp)
return_list = []
for i in data["inverter"]:
return_list.append(self._ListInvertersResponse.parse_obj(i))
return return_list
class _InverterStatus(BaseModel):
communicationStatus: int
communicationDelayStatus: int
async def get_inverter_status(self, inverter: str) -> _InverterStatus:
async with ClientSession() as c, c.get(
f"{self.base_url}/aps-api-web/api/v2/data/device/ezInverter/status/{inverter}?language={self.language}",
headers={"Authorization": f"Bearer {self.access_token}"}, timeout=5) as resp:
data = await _process_response(resp)
return self._InverterStatus.parse_obj(data)
class _InverterStatistics(BaseModel):
lastReportDatetime: datetime
lastCommunicationStatus: int
lastPower: str
runningDuration: int
monthEnergy: float
lastRunningStatus: int | None
lifetimeCo2: float
lifetimeEnergy: float
todayCo2: float
todayEnergy: float
monthCo2: float
async def get_inverter_statistics(self, inverter: str):
async with ClientSession() as c, c.get(
f"{self.base_url}/aps-api-web/api/v2/data/device/ezInverter/statistic/{inverter}?language={self.language}",
headers={"Authorization": f"Bearer {self.access_token}"}, timeout=5) as resp:
i_data = await _process_response(resp)
date_format = "%Y-%m-%d %H:%M:%S"
return self._InverterStatistics(
lastReportDatetime=datetime.strptime(i_data["lastReportDatetime"], date_format),
lastCommunicationStatus=i_data["lastCommunicationStatus"], lastPower=i_data["lastPower"],
runningDuration=i_data["runningDuration"], monthEnergy=float(i_data["monthEnergy"]),
lastRunningStatus=i_data.get("lastRunningStatus", None), lifetimeCo2=float(i_data["lifetimeCo2"]),
lifetimeEnergy=float(i_data["lifetimeEnergy"]), todayCo2=float(i_data["todayCo2"]),
todayEnergy=float(i_data["todayEnergy"]), monthCo2=float(i_data["monthCo2"]))
class _InverterRealtime(BaseModel):
communicationStatus: int
runningDuration: int
runningStatus: int
inverter_dev_id: str
power: int
type: str
energy: float
async def get_inverter_realtime(self, inverter: str):
async with ClientSession() as c, c.get(
f"{self.base_url}/aps-api-web/api/v2/data/device/ezInverter/realTime/{inverter}?language={self.language}",
headers={"Authorization": f"Bearer {self.access_token}"}, timeout=5) as resp:
d = await _process_response(resp)
return self._InverterRealtime(communicationStatus=d["communicationStatus"],
runningDuration=d["runningDuration"], runningStatus=d["runningStatus"],
inverter_dev_id=d["inverter_dev_id"],
power=d["power"], type=d["type"], energy=float(d["energy"]))
class _Graph(BaseModel):
peakPower: str
totalEnergy: float
power: list[str]
time: list[str]
energy: list[str]
async def get_graph(self, inverter: str, year: int, month: int | None = None,
day: int | None = None):
d_range = ""
date_str = str(year)
if day is not None and month is None:
raise ValueError("Day can't be set if month is unset")
if day is None and month is None:
d_range = "year"
elif day is None and month is not None:
d_range = "month"
date_str += str(month)
elif day is not None and month is not None:
d_range = "day"
date_str += str(month) + str(day)
async with ClientSession() as c, c.get(
f"{self.base_url}/aps-api-web/api/v2/data/device/ezInverter/{d_range}/{inverter}/{date_str}?language={self.language}",
headers={"Authorization": f"Bearer {self.access_token}"}, timeout=5) as resp:
d = await _process_response(resp)
return self._Graph(peakPower=d.get("peakPower", None), totalEnergy=float(d["totalEnergy"]),
power=d["power"],
time=d["time"], energy=d["energy"])
class _LifetimeGraph(BaseModel):
year: list[str]
totalEnergy: float
averageEnergy: float
energy: list[str]
async def get_lifetime_graph(self, inverter: str):
async with ClientSession() as c, c.get(
f"{self.base_url}/aps-api-web/api/v2/data/device/ezInverter/lifetime/{inverter}?language={self.language}",
headers={"Authorization": f"Bearer {self.access_token}"}, timeout=5) as resp:
d = await _process_response(resp)
return self._LifetimeGraph(year=d["year"], totalEnergy=float(d["totalEnergy"]),
averageEnergy=float(d["averageEnergy"]), energy=d["energy"])
async def refresh_login(self):
formData = FormData({"language": self.language, "refresh_token": self.refresh_token})
async with ClientSession() as c, c.post(f"{self.base_url}/api/token/refreshToken?language={self.language}",
headers={"Authorization": f"Bearer {self.access_token}"}, data=formData,
timeout=5) as resp:
d = await _process_response(resp)
self.access_token = d["access_token"]