forked from JasperE84/PyFusionSolarDataRelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pvfusionsolar.py
99 lines (82 loc) · 3.78 KB
/
pvfusionsolar.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
import requests
import json
import html
from pvconf import PvConf
class PvFusionSolar:
def __init__(self, conf: PvConf, logger):
self.conf = conf
self.logger = logger
self.lastCumulativeEnergy = 0
self.logger.debug("PvFusionSolar class instantiated")
def fetch_fusionsolar_status(self):
self.logger.info("Requesting data from FusionSolar Kiosk API...")
try:
response = requests.get(
f"{self.conf.fusionsolarurl}{self.conf.fusionsolarkkid}",
verify=False
)
except Exception as e:
raise Exception(
"Error fetching data from FusionSolar Kiosk API: '{}'".format(str(e))
)
try:
response_json = response.json()
except Exception as e:
raise Exception(
"Error while parsing JSON response from Kiosk API: '{}'".format(str(e))
)
if not "data" in response_json:
raise Exception(
f"FusionSolar Kiosk API response does not contain data key. Response: {response_json}"
)
try:
response_json_data_decoded = html.unescape(response_json["data"])
response_json_data = json.loads(response_json_data_decoded)
except Exception as e:
raise Exception(
"Error while parsing JSON response data element from FusionSolar Kiosk API: '{}'".format(
str(e)
)
)
# Checking required realKpi elements and transforming kW(h) to W(h)
if not "realKpi" in response_json_data_decoded:
raise Exception(
"Element realKpi is missing in FusionSolar Kiosk API response data"
)
floatKeys = {"realTimePower", "cumulativeEnergy", "monthEnergy", "dailyEnergy", "yearEnergy"}
for floatKey in floatKeys:
if floatKey in response_json_data["realKpi"]:
response_json_data["realKpi"][floatKey] = float(
response_json_data["realKpi"][floatKey]
) * float(1000)
# Set this to fix fusionsolar quirk at midnight where cumulativeEnergy will decrease with the days amount of solar production
if floatKey == "cumulativeEnergy":
if self.lastCumulativeEnergy != 0 and response_json_data["realKpi"][floatKey] < self.lastCumulativeEnergy:
response_json_data["realKpi"][floatKey] = self.lastCumulativeEnergy
else:
self.lastCumulativeEnergy = response_json_data["realKpi"][floatKey]
else:
raise Exception(
f"FusionSolar API data realKpi response element does cot contain key {floatKey}."
)
# Checking required powerCurve elements and transforming kW(h) to W(h)
if not "powerCurve" in response_json_data_decoded:
raise Exception(
"Element powerCurve is missing in FusionSolar Kiosk API response data"
)
floatKeys = {"currentPower"}
for floatKey in floatKeys:
if floatKey in response_json_data["powerCurve"]:
response_json_data["powerCurve"][floatKey] = float(
response_json_data["powerCurve"][floatKey]
) * float(1000)
else:
raise Exception(
f"FusionSolar API data powerCurve response element does cot contain key {floatKey}."
)
#test = {}
#for idx, x in enumerate(response_json_data["powerCurve"]["xAxis"]):
# test[idx] = x
#self.logger.info(str(test))
self.logger.debug(f'FusionSolar API data: {response_json_data["realKpi"]}')
return response_json_data