-
Notifications
You must be signed in to change notification settings - Fork 52
/
config_generator.py
212 lines (172 loc) · 8.9 KB
/
config_generator.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#!/usr/bin/env python3
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#Test: config_generator.py --host=HOST --port=502 --model=model --mqtt_host=MQTT_HOST --mqtt_port=1883 --mqtt_user=MQTT_USER --mqtt_pass=MQTT_PASS --scan=10 --timeout=3 --connection=sungrow --meter=false --level=STANDARD --log_level=INFO
import getopt
import sys
import yaml
##Load options
options = {'inverter':{},'exports':[{}]}
sensors =[]
full_cmd_arguments = sys.argv
argument_list = full_cmd_arguments[1:]
short_options = 'h:p:o:t:s:c:m:l:L:M:P:U:A'
long_options = ['host=','port=','model=','timeout=','scan=','connection=','meter=','level=','log_level=',
'mqtt_host=', 'mqtt_port=','mqtt_user=', 'mqtt_pass=']
def level(l):
switcher={
'BASIC': 0,
'STANDARD': 1,
'DETAIL': 2,
'ALL': 3
}
return switcher.get(l, 1)
try:
arguments, values = getopt.getopt(
argument_list, short_options, long_options)
except getopt.error as e:
raise ValueError('Invalid parameters!')
for current_argument, current_value in arguments:
if current_value == 'null' or len(current_value) == 0 or current_value.isspace():
pass
elif current_argument in ("-h", "--host"):
options['inverter']['host'] = current_value
elif current_argument in ("-p", "--port"):
options['inverter']['port'] = int(current_value)
elif current_argument in ("-o", "--model"):
options['inverter']['model'] = current_value
elif current_argument in ("-t", "--timeout"):
options['inverter']['timeout'] = int(current_value)
elif current_argument in ("-s", "--scan"):
options['inverter']['scan_interval'] = int(current_value)
elif current_argument in ("-c", "--connection"):
options['inverter']['connection'] = current_value.lower()
elif current_argument in ("-m", "--meter"):
options['inverter']['smart_meter'] = (current_value).lower() in ("yes","true","1")
elif current_argument in ("-l", "--level"):
options['inverter']['level'] = level(current_value)
elif current_argument in ("-L", "--log_level"):
options['inverter']['log_console'] = current_value.upper()
elif current_argument in ("-M", "--mqtt_host"):
options['exports'][0]['host'] = current_value
elif current_argument in ("-P", "--mqtt_port"):
options['exports'][0]['port'] = int(current_value)
elif current_argument in ("-U", "--mqtt_user"):
options['exports'][0]['username'] = current_value
elif current_argument in ("-A", "--mqtt_pass"):
options['exports'][0]['password'] = current_value
options['exports'][0]['name'] = "mqtt"
options['exports'][0]['enabled'] = True
options['exports'][0]['homeassistant']= True
#Basic Level sensor
sensors.append({'name': "Power State", 'sensor_type': "binary_sensor",
'register': "run_state",'dev_class': "running",
'payload_on': "ON",'payload_off': "OFF"})
sensors.append({'name': "Daily Generation", 'sensor_type': "sensor",
'register': "daily_power_yields", 'dev_class': "energy",
'state_class': "total_increasing"})
sensors.append({'name': "Active Power", 'sensor_type': "sensor",
'register': "total_active_power", 'dev_class': "power",
'state_class': "measurement"})
#Standard Level
if int(options['inverter']['level']) >= 1:
sensors.append({'name': "Total DC Power", 'sensor_type': "sensor",
'register': "total_dc_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Load Power Hybrid", 'sensor_type': "sensor",
'register': "load_power_hybrid", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Export Power Hybrid", 'sensor_type': "sensor",
'register': "export_power_hybrid", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Load Power", 'sensor_type': "sensor",
'register': "load_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Phase A Voltage", 'sensor_type': "sensor",
'register': "phase_a_voltage", 'dev_class': "voltage",
'state_class': "measurement"})
sensors.append({'name': "Phase B Voltage", 'sensor_type': "sensor",
'register': "phase_b_voltage", 'dev_class': "voltage",
'state_class': "measurement"})
sensors.append({'name': "Phase C Voltage", 'sensor_type': "sensor",
'register': "phase_c_voltage", 'dev_class': "voltage",
'state_class': "measurement"})
sensors.append({'name': "Phase A Current", 'sensor_type': "sensor",
'register': "phase_a_current", 'dev_class': "current",
'state_class': "measurement"})
sensors.append({'name': "Phase B Current", 'sensor_type': "sensor",
'register': "phase_b_current", 'dev_class': "current",
'state_class': "measurement"})
sensors.append({'name': "Phase C Current", 'sensor_type': "sensor",
'register': "phase_c_current", 'dev_class': "current",
'state_class': "measurement"})
sensors.append({'name': "Battery Power", 'sensor_type': "sensor",
'register': "battery_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Battery Power", 'sensor_type': "sensor",
'register': "battery_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Battery SOC", 'sensor_type': "sensor",
'register': "battery_level", 'dev_class': "percent",
'state_class': "measurement"})
sensors.append({'name': "Battery Health", 'sensor_type': "sensor",
'register': "battery_state_of_healthy", 'dev_class': "percent",
'state_class': "measurement"})
if options['inverter']['smart_meter'] == True:
sensors.append({'name': "Meter Power", 'sensor_type': "sensor",
'register': "meter_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Meter A phase Power", 'sensor_type': "sensor",
'register': "meter_a_phase_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Meter B phase Power", 'sensor_type': "sensor",
'register': "meter_b_phase_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Meter C phase Power", 'sensor_type': "sensor",
'register': "meter_c_phase_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Export Power", 'sensor_type': "sensor",
'register': "export_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Power Meter", 'sensor_type': "sensor",
'register': "power_meter", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Import from Grid", 'sensor_type': "sensor",
'register': "import_from_grid", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Export to Grid", 'sensor_type': "sensor",
'register': "export_to_grid", 'dev_class': "power",
'state_class': "measurement"})
#Detail Level
if options['inverter']['level'] >= 2:
sensors.append({'name': "Temperature", 'sensor_type': "sensor",
'register': "internal_temperature", 'dev_class': "temperature",
'state_class': "measurement"})
sensors.append({'name': "Battery Temperature", 'sensor_type': "sensor",
'register': "battery_temperature", 'dev_class': "temperature",
'state_class': "measurement"})
sensors.append({'name': "Total Active Power", 'sensor_type': "sensor",
'register': "total_active_power", 'dev_class': "power",
'state_class': "measurement"})
sensors.append({'name': "Total Reactive Power", 'sensor_type': "sensor",
'register': "total_reactive_power", 'dev_class': "power",
'state_class': "measurement"})
options['exports'][0]['ha_sensors']= sensors
with open('config.sg', 'w') as yaml_file:
yaml.dump(options, yaml_file, default_flow_style=False)