-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapitest.py
62 lines (48 loc) · 1.85 KB
/
apitest.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
import logging
import asyncio
import aiohttp
import sys
import datetime
from alphaess.alphaess import alphaess
if len(sys.argv) != 3:
username = input("username: ")
password = input("password: ")
else:
username = sys.argv[1]
password = sys.argv[2]
logger = logging.getLogger('')
logger.setLevel(logging.DEBUG)
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter('[%(asctime)s] %(levelname)s [%(filename)s.%(funcName)s:%(lineno)d] %(message)s', datefmt='%a, %d %b %Y %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
async def main():
logger.debug("instantiating Alpha ESS Client")
client: alphaess = alphaess()
logger.debug("Checking authentication")
try:
authenticated = await client.authenticate(username, password)
if authenticated:
data = await client.getdata()
print(f"all data: {data}")
if data:
if "sys_sn" in data[0]:
serial = data[0]['sys_sn']
print(f"serial: {serial}")
index = int(datetime.date.today().strftime("%d")) - 1
if "EDischarge" in data:
discharge = data[0]["system_statistics"]["EDischarge"]
print(f"discharge: {discharge[index]}")
if "ECharge" in data:
charge = data[0]["system_statistics"]["ECharge"]
print(f"charge: {charge[index]}")
await client.setbatterycharge(serial, False, "00:00", "00:00", "00:00", "00:00", 100)
await client.setbatterydischarge(serial, True, "08:00", "23:00", "00:00", "00:00", 15)
except aiohttp.ClientResponseError as e:
if e.status == 401:
logger.error("Authentication Error")
else:
logger.error(e)
except Exception as e:
logger.error(e)
asyncio.run(main())