forked from MatterVN/ModbusTCP2MQTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_generator.py
132 lines (112 loc) · 5.5 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
#!/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': "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"})
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': "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"})
options['exports'][0]['ha_sensors']= sensors
with open('config.sg', 'w') as yaml_file:
yaml.dump(options, yaml_file, default_flow_style=False)