-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfometric.py
60 lines (51 loc) · 1.69 KB
/
infometric.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
from aiohttp import ClientSession
from dataclasses import dataclass
class InfometricException(Exception):
pass
@dataclass
class InfometricMeter(object):
id: str
name: str
label: str
average: str
prognosis: str
last_values: list
class InfometricClient(object):
url = None
username = None
password = None
session: ClientSession = None
def __init__(self, url, username, password):
self.url = url
self.username = username
self.password = password
async def authenticate(self, session):
self.session = session
try:
resp = await self.session.post(
url=self.url,
data=f"UserName={self.username}&Password={self.password}&RememberMe=true&commit=Logga+in",
headers={"content-type": "application/x-www-form-urlencoded"},
)
return resp.ok
except Exception as e:
raise InfometricException(e)
async def get_meters(self):
ll = lambda x: {"series": x["SeriesId"], "time": x["Date"], "value": x["Value"]}
try:
resp = await self.session.post(
url=f"{self.url}/Consumption/GetMeasureTypes"
)
return [
InfometricMeter(
id=m["UnitId"],
label=m["UnitLabel"],
name=m["Name"],
average=m["AverageConsumption"],
prognosis=m["PrognosConsumption"],
last_values=[ll(x) for x in m["LastValues"]],
)
for m in await resp.json()
]
except Exception as e:
raise InfometricException(e)