-
Notifications
You must be signed in to change notification settings - Fork 0
/
pw_mode.py
75 lines (58 loc) · 1.92 KB
/
pw_mode.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
import logging
import sys
import time
import requests
import constants as C
from common import apicall
from teslatoken import Token
logging.basicConfig(
filename="powerwall.log",
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(message)s",
)
def productlists():
token = Token()
response = apicall(
C.PRODUCTS_ENDPOINT,
"GET",
headers={"Authorization": "Bearer " + token.tokenstr},
)
if response.status_code != 200:
logging.error("Couldn't get products. Reason: %s" % (str(response)))
raise Exception("Couldn't get products. Reason: %s" % (str(response)))
return response.json().get("response", [])
def getsolarproduct(productlist):
if not isinstance(productlist, list):
productlist = []
energysites = {
product["energy_site_id"]: product
for product in productlist
if "energy_site_id" in product
}
return energysites
def updatemode(siteid, mode):
if mode not in C.ENERGY_MODES:
logging.error("Mode not valid. Mode = %s." % mode)
raise Exception("Mode not valid. Mode = %s." % mode)
if not isinstance(siteid, int):
siteid = ""
if siteid:
token = Token()
params = {"default_real_mode": mode}
response = apicall(
C.OPERATION_ENDPOINT.format(siteid),
"POST",
headers={"Authorization": "Bearer " + token.tokenstr},
params=params,
)
if response.status_code != 200:
logging.error("Couldn't change energy mode. Reason: %s" % (str(response)))
raise Exception("Couldn't change energy mode. Reason: %s" % (str(response)))
# logging.info(response.json())
if len(sys.argv) == 2:
mode = sys.argv[1]
energy = getsolarproduct(productlists())
if energy:
siteid = list(energy.keys())[0]
updatemode(siteid, mode)
logging.info("Changed mode to: " + mode)