-
Notifications
You must be signed in to change notification settings - Fork 1
/
hoymiles_data.py
144 lines (119 loc) · 4.42 KB
/
hoymiles_data.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
import sys
import argparse
from ipaddress import ip_address
from hoymiles_wifi.dtu import DTU
import json
import asyncio
from flask import Flask, render_template_string
import plotly.graph_objects as go
from jinja2 import Template
#####################
# ***** ARGS ****** #
#####################
parser = argparse.ArgumentParser(
prog = 'hoymiles_data.py',
description = 'Get data from Hoymiles inverter'
)
parser.add_argument('--dtu_ip_address', default = '192.168.178.123', type=ip_address, required=True, help = "where DTU-IP-ADDRESS has the format aaa.bbb.ccc.ddd")
parser.add_argument('--max', default = '100', type=int , required=True, help = "Max power your gauge should show")
parser.add_argument('--test', action = "store_true", default=False, required=False, help = "use a test dataset")
args = parser.parse_args()
####################
# ***** VARS ***** #
####################
testDataFile = './response_test_data.json'
app = Flask(__name__)
####################
# ***** DEFS ***** #
####################
def createGaugeGraphic(power, energy_total, energy_daily):
fig = go.Figure(go.Indicator(
mode="gauge+number",
value=power,
number={'suffix': " W",'valueformat': '.1f', 'font': {'size': 24}}, # Format the number to one decimal place
gauge={'axis': {'range': [0, args.max], 'tickcolor': 'white', 'tickfont': {'color': 'white'}},
'bar': {'color': 'white'},
'bgcolor': 'black',
'borderwidth': 2,
'bordercolor': 'white'},
domain={'x': [0, 1], 'y': [0.8, 1]} # Adjust vertical positioning
))
# Add additional trace for energy daily
fig.add_trace(go.Indicator(
mode="number",
value=energy_daily,
title={'text': "Today", 'font': {'color': 'white', 'size': 16}},
number={'suffix': " Wh", 'font': {'size': 12}},
domain={'x': [0.2, 0.4], 'y': [0.65, 0.75]} # Adjust vertical positioning
))
# Add additional trace for energy total
fig.add_trace(go.Indicator(
mode="number",
value=energy_total,
title={'text': "Total", 'font': {'color': 'white', 'size': 16}},
number={'suffix': " Wh", 'font': {'size': 12}},
domain={'x': [0.6, 0.8], 'y': [0.65, 0.75]} # Adjust vertical positioning
))
# Update layout
fig.update_layout(
paper_bgcolor="rgba(0,0,0,0)",
plot_bgcolor="rgba(0,0,0,0)",
font={'color': "white", 'family': "Arial"},
margin=dict(t=50, b=0, l=0, r=0) # Small margins around the plot
)
# Save the gauge graphic as an HTML div
gauge_html = fig.to_html(full_html=False)
# HTML template
template = Template("""
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DTU Data</title>
</head>
<body">
<div>
{{ gauge_html | safe }}
</div>
</body>
</html>
""")
return (template, gauge_html)
async def get_dtu_data():
response = None
if not args.test:
print(f"Getting data from DTU with IP address: {args.dtu_ip_address}")
dtu = DTU(str(args.dtu_ip_address)) # Convert IPv4Address to string
response = await dtu.async_get_real_data_new()
if args.test:
try:
with open(testDataFile, 'r') as file:
response = json.load(file)
except (FileNotFoundError, PermissionError, OSError):
print('ERROR: Cannot open file response_test_data.json')
return None
print(f"Response: {response}")
power = 0
energy_total = 0
energy_daily = 0
if response :
pv_data = response.pv_data
power = response.dtu_power / 10.0
energy_total = pv_data[0].energy_total
energy_daily = response.dtu_daily_energy
(template, gauge_html) = createGaugeGraphic(power, energy_total, energy_daily)
# Render the HTML with the gauge graphic and energy total
html_content = template.render(gauge_html=gauge_html, energy_total=energy_total, energy_daily=energy_daily)
return html_content
@app.route('/')
def index():
html_content = asyncio.run(get_dtu_data())
return render_template_string(html_content)
####################
# ***** MAIN ***** #
####################
if __name__ == '__main__':
html_content = app.run()
if html_content != 0:
sys.exit("Exiting program due to error(s)")