-
Notifications
You must be signed in to change notification settings - Fork 8
/
config-entity-functions.js
executable file
·343 lines (310 loc) · 9.49 KB
/
config-entity-functions.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
import * as constants from "./constants.js";
const required_entries = ["inverter_count", "battery_soc", "battery_flow", "home_consumption", "grid_flow"];
export function buildConfig(config) {
function deepcopy(value) {
if (!(!!value && typeof value == "object")) {
return value;
}
if (Object.prototype.toString.call(value) == "[object Date]") {
return new Date(value.getTime());
}
if (Array.isArray(value)) {
return value.map(deepcopy);
}
const result = {};
Object.keys(value).forEach(function (key) {
result[key] = deepcopy(value[key]);
});
return result;
}
config = deepcopy(config);
const new_config = deepcopy(constants.base_config);
new_config.title = config.title;
// Check inverter count
let inverter_count = parseInt(config.inverter_count);
if (isNaN(inverter_count) || inverter_count <= 0) {
throw new Error(
"You don't have the 'inverter_count' config. This likely means that you haven't updated to the new config used from v0.5.0 and on. Please take a look at the project's README on GitHub for more info."
);
}
// Assign inverter count
new_config.inverter_count = inverter_count;
let config_entities = [
"battery_soc",
"battery_flow",
"home_consumption",
"grid_flow",
"pv_power",
"battery_voltage",
"backup_power",
"grid_voltage",
"update_time",
"status_codes",
"generator_voltage",
"generator_power",
];
for (let i = 0; i < config_entities.length; i++) {
importConfigEntities(config, new_config, inverter_count, config_entities[i]);
}
let config_values = ["lux_dongle", "inverter_alias"];
for (let i = 0; i < config_values.length; i++) {
importConfigValues(config, new_config, inverter_count, config_values[i]);
}
// Check last time on update time
if (new_config.update_time.is_used) {
if (config.update_time) {
new_config.update_time.show_last_update = config.update_time.show_last_update;
}
}
// Check grid indicator
if (config.grid_indicator) {
if (config.grid_indicator.hue) {
new_config.grid_indicator.hue = true;
}
if (config.grid_indicator.dot) {
new_config.grid_indicator.dot = true;
}
}
// Check refresh button
if (new_config.lux_dongle.is_used) {
if (config.refresh_button) {
switch (config.refresh_button) {
case "left":
new_config.refresh_button.left = true;
break;
case "right":
new_config.refresh_button.right = true;
break;
case "both":
new_config.refresh_button.left = true;
new_config.refresh_button.right = true;
break;
default:
break;
}
}
}
// Check on status with no grid
if (new_config.status_codes.is_used) {
if (config.status_codes && config.status_codes.no_grid_is_warning) {
new_config.status_codes.no_grid_is_warning = config.status_codes.no_grid_is_warning;
}
}
// Check allocated energy
if (config.energy_allocations && config.energy_allocations.entities) {
if (config.energy_allocations.entities.length > 0) {
new_config.energy_allocations.is_used = true;
new_config.energy_allocations.entities = config.energy_allocations.entities;
}
}
// Check parallel settings
if (config.parallel) {
if (config.parallel.average_voltage) {
new_config.parallel.average_voltage = true;
}
if (config.parallel.parallel_first) {
new_config.parallel.parallel_first = true;
} else {
new_config.parallel.parallel_first = false;
}
}
// Check individual solar inputs
if (config.pv_power.show_individual) {
new_config.pv_power.show_individual = true;
new_config.pv_power.individuals = {}
for (let i = 0; i < inverter_count; i++) {
let key_name = `inv_${(i + 1)}_entities`;
new_config.pv_power.individuals[key_name] = config.pv_power[key_name]
}
}
validateConfig(new_config);
return new_config;
}
function validateConfig(config) {
if (!config.battery_soc.is_used) {
throw new Error("You need to define entities for the battery SOC (Equal to the inverter count).");
}
if (!config.battery_flow.is_used) {
throw new Error("You need to define entities for the battery flow (Equal to the inverter count).");
}
if (!config.grid_flow.is_used) {
throw new Error("You need to define entities for the grid flow (Equal to the inverter count).");
}
if (!config.home_consumption.is_used) {
throw new Error("You need to define entities for the home consumption (Equal to the inverter count).");
}
if (config.inverter_count > 1) {
if (!config.lux_dongle.is_used && !config.inverter_alias.is_used) {
throw new Error("You need to define entities enough dongles or aliases for each inverter.");
}
if (config.lux_dongle.is_used && !config.inverter_alias.is_used) {
config.inverter_alias.is_used = true;
config.inverter_alias.values = config.lux_dongle.values;
}
}
}
function importConfigEntities(config, new_config, inverter_count, object_name) {
if (config[object_name]) {
if (config[object_name].entities) {
if (config[object_name].entities.length == inverter_count) {
new_config[object_name].is_used = true;
new_config[object_name].entities = config[object_name].entities;
}
}
}
}
function importConfigValues(config, new_config, inverter_count, object_name) {
if (config[object_name]) {
if (config[object_name].length == inverter_count) {
new_config[object_name].is_used = true;
new_config[object_name].values = config[object_name];
}
}
}
export function getEntitiesState(config, hass, config_entity, index) {
const entity_name = config[config_entity].entities[index];
try {
const entity = hass.states[entity_name];
if (entity.state) {
if (entity.state === "unavailable" || entity.state === "unknown") {
return "-";
} else {
return entity.state;
}
}
return "-";
} catch (error) {
handleEntityError(entity_name, config_entity);
}
}
export function getEntitiesNumState(config, hass, config_entity, index, is_int = true, is_avg = false) {
let value = 0;
if (index == -1) {
for (let i = 0; i < config.inverter_count; i++) {
value += parseFloat(getEntitiesState(config, hass, config_entity, i));
}
if (is_avg) {
value = value / config.inverter_count;
}
} else {
value = parseFloat(getEntitiesState(config, hass, config_entity, index));
}
if (is_int) {
return parseInt(value);
}
return Math.round(value * 100) / 100;
}
export function getIndividualPvValues(config, hass, index) {
const individual_values = [];
const pv_individuals = config.pv_power.individuals[`inv_${index + 1}_entities`]
for (let i = 0; i < pv_individuals.length; i++) {
const entity = hass.states[pv_individuals[i]];
if (entity.state) {
if (entity.state === "unavailable" || entity.state === "unknown") {
individual_values.push(`- ${entity.attributes.unit_of_measurement}`);
} else {
individual_values.push(`${entity.state} ${entity.attributes.unit_of_measurement}`);
}
}
}
return individual_values
}
export function getEntitiesAttribute(config, hass, config_entity, attribute_name, index) {
const entity_name = config[config_entity].entities[index];
try {
const entity = hass.states[entity_name];
if (entity.attributes && entity.attributes[attribute_name]) {
return entity.attributes[attribute_name];
} else {
return "-";
}
} catch (error) {
handleEntityError(entity_name, config_entity);
}
}
export function getEntitiesUnit(config, hass, config_entity, index) {
const entity_name = config[config_entity].entities[index];
try {
const entity = hass.states[config[config_entity].entities[index]];
if (entity.state) {
if (isNaN(entity.state)) return "-";
else return entity.attributes.unit_of_measurement ?? "";
}
return "";
} catch (error) {
handleEntityError(entity_name, config_entity);
}
}
const handleEntityError = (config_entry, entity_name) => {
if (required_entries.includes(config_entry)) {
throw new Error(`Invalid entity: ${entity_name} for config_entry`);
}
};
export function getStatusMessage(status_code, show_no_grid_as_warning) {
var status_level = 0;
var message = "Warning";
switch (status_code) {
case 0:
message = `Standby`;
status_level = 0;
break;
case 1:
message = `Error`;
status_level = 2;
break;
case 2:
message = `Inverting (Programming)`;
status_level = 1;
break;
case 4:
message = `Normal`;
status_level = 0;
break;
case 9:
message = `Normal (Selling)`;
status_level = 0;
break;
case 10:
case 12:
case 16:
case 20:
case 32:
case 40:
message = `Normal`;
status_level = 0;
break;
case 17:
message = `High temp`;
status_level = 2;
break;
case 64:
case 136:
case 192:
if (show_no_grid_as_warning) {
message = `No grid`;
status_level = 1;
} else {
message = `Normal`;
status_level = 0;
}
break;
default:
message = `Unknown code ${status_code}`;
status_level = 2;
break;
}
var indicator = "";
switch (status_level) {
case 0:
indicator = `🟢`;
break;
case 1:
indicator = `🟠`;
break;
case 2:
default:
indicator = `🔴`;
break;
}
return `${message} ${indicator}`;
}