forked from tbnobody/OpenDTU
-
Notifications
You must be signed in to change notification settings - Fork 67
/
MqttHandlePowerLimiterHass.cpp
253 lines (205 loc) · 7.87 KB
/
MqttHandlePowerLimiterHass.cpp
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
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2022 Thomas Basler and others
*/
#include "MqttHandlePowerLimiterHass.h"
#include "MqttHandleHass.h"
#include "Configuration.h"
#include "MqttSettings.h"
#include "NetworkSettings.h"
#include "MessageOutput.h"
#include "Utils.h"
#include "PowerLimiter.h"
#include "__compiled_constants.h"
MqttHandlePowerLimiterHassClass MqttHandlePowerLimiterHass;
void MqttHandlePowerLimiterHassClass::init(Scheduler& scheduler)
{
scheduler.addTask(_loopTask);
_loopTask.setCallback(std::bind(&MqttHandlePowerLimiterHassClass::loop, this));
_loopTask.setIterations(TASK_FOREVER);
_loopTask.enable();
}
void MqttHandlePowerLimiterHassClass::loop()
{
if (!Configuration.get().PowerLimiter.Enabled) {
return;
}
if (_updateForced) {
publishConfig();
_updateForced = false;
}
if (MqttSettings.getConnected() && !_wasConnected) {
// Connection established
_wasConnected = true;
publishConfig();
} else if (!MqttSettings.getConnected() && _wasConnected) {
// Connection lost
_wasConnected = false;
}
}
void MqttHandlePowerLimiterHassClass::forceUpdate()
{
_updateForced = true;
}
void MqttHandlePowerLimiterHassClass::publishConfig()
{
auto const& config = Configuration.get();
if (!config.Mqtt.Hass.Enabled) {
return;
}
if (!MqttSettings.getConnected()) {
return;
}
if (!config.PowerLimiter.Enabled) {
return;
}
publishSelect("DPL Mode", "mdi:gauge", "config", "mode", "mode");
if (!PowerLimiter.usesBatteryPoweredInverter()) {
return;
}
// as this project revolves around Hoymiles inverters, 16 - 60 V is a reasonable voltage range
publishNumber("DPL battery voltage start threshold", "mdi:battery-charging",
"config", "threshold/voltage/start", "threshold/voltage/start", "V", 16, 60, 0.1);
publishNumber("DPL battery voltage stop threshold", "mdi:battery-charging",
"config", "threshold/voltage/stop", "threshold/voltage/stop", "V", 16, 60, 0.1);
if (config.Vedirect.Enabled) {
publishBinarySensor("full solar passthrough active",
"mdi:transmission-tower-import",
"full_solar_passthrough_active", "1", "0");
publishNumber("DPL full solar passthrough start voltage",
"mdi:transmission-tower-import", "config",
"threshold/voltage/full_solar_passthrough_start",
"threshold/voltage/full_solar_passthrough_start", "V", 16, 60, 0.1);
publishNumber("DPL full solar passthrough stop voltage",
"mdi:transmission-tower-import", "config",
"threshold/voltage/full_solar_passthrough_stop",
"threshold/voltage/full_solar_passthrough_stop", "V", 16, 60, 0.1);
}
if (config.Battery.Enabled && !config.PowerLimiter.IgnoreSoc) {
publishNumber("DPL battery SoC start threshold", "mdi:battery-charging",
"config", "threshold/soc/start", "threshold/soc/start", "%", 0, 100, 1.0);
publishNumber("DPL battery SoC stop threshold", "mdi:battery-charging",
"config", "threshold/soc/stop", "threshold/soc/stop", "%", 0, 100, 1.0);
if (config.Vedirect.Enabled) {
publishNumber("DPL full solar passthrough SoC",
"mdi:transmission-tower-import", "config",
"threshold/soc/full_solar_passthrough",
"threshold/soc/full_solar_passthrough", "%", 0, 100, 1.0);
}
}
}
void MqttHandlePowerLimiterHassClass::publishSelect(
const char* caption, const char* icon, const char* category,
const char* commandTopic, const char* stateTopic)
{
String selectId = caption;
selectId.replace(" ", "_");
selectId.toLowerCase();
const String configTopic = "select/" + MqttHandleHass.getDtuUniqueId() + "/" + selectId + "/config";
const String cmdTopic = MqttSettings.getPrefix() + "powerlimiter/cmd/" + commandTopic;
const String statTopic = MqttSettings.getPrefix() + "powerlimiter/status/" + stateTopic;
JsonDocument root;
root["name"] = caption;
root["uniq_id"] = MqttHandleHass.getDtuUniqueId() + "_" + selectId;
if (strcmp(icon, "")) {
root["ic"] = icon;
}
root["ent_cat"] = category;
root["cmd_t"] = cmdTopic;
root["stat_t"] = statTopic;
JsonArray options = root["options"].to<JsonArray>();
options.add("0");
options.add("1");
options.add("2");
createDeviceInfo(root);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}
String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
}
void MqttHandlePowerLimiterHassClass::publishNumber(
const char* caption, const char* icon, const char* category,
const char* commandTopic, const char* stateTopic, const char* unitOfMeasure,
const int16_t min, const int16_t max, const float step)
{
String numberId = caption;
numberId.replace(" ", "_");
numberId.toLowerCase();
const String configTopic = "number/" + MqttHandleHass.getDtuUniqueId() + "/" + numberId + "/config";
const String cmdTopic = MqttSettings.getPrefix() + "powerlimiter/cmd/" + commandTopic;
const String statTopic = MqttSettings.getPrefix() + "powerlimiter/status/" + stateTopic;
JsonDocument root;
root["name"] = caption;
root["uniq_id"] = MqttHandleHass.getDtuUniqueId() + "_" + numberId;
if (strcmp(icon, "")) {
root["ic"] = icon;
}
root["ent_cat"] = category;
root["cmd_t"] = cmdTopic;
root["stat_t"] = statTopic;
root["unit_of_meas"] = unitOfMeasure;
root["min"] = min;
root["max"] = max;
root["step"] = step;
root["mode"] = "box";
auto const& config = Configuration.get();
if (config.Mqtt.Hass.Expire) {
root["exp_aft"] = config.Mqtt.PublishInterval * 3;
}
createDeviceInfo(root);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}
String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
}
void MqttHandlePowerLimiterHassClass::publishBinarySensor(
const char* caption, const char* icon,
const char* stateTopic, const char* payload_on, const char* payload_off)
{
String numberId = caption;
numberId.replace(" ", "_");
numberId.toLowerCase();
const String configTopic = "binary_sensor/" + MqttHandleHass.getDtuUniqueId() + "/" + numberId + "/config";
const String statTopic = MqttSettings.getPrefix() + "powerlimiter/status/" + stateTopic;
JsonDocument root;
root["name"] = caption;
root["uniq_id"] = MqttHandleHass.getDtuUniqueId() + "_" + numberId;
if (strcmp(icon, "")) {
root["ic"] = icon;
}
root["stat_t"] = statTopic;
root["pl_on"] = payload_on;
root["pl_off"] = payload_off;
auto const& config = Configuration.get();
if (config.Mqtt.Hass.Expire) {
root["exp_aft"] = config.Mqtt.PublishInterval * 3;
}
createDeviceInfo(root);
if (!Utils::checkJsonAlloc(root, __FUNCTION__, __LINE__)) {
return;
}
String buffer;
serializeJson(root, buffer);
publish(configTopic, buffer);
}
void MqttHandlePowerLimiterHassClass::createDeviceInfo(JsonDocument& root)
{
JsonObject object = root["dev"].to<JsonObject>();
object["name"] = "Dynamic Power Limiter";
object["ids"] = MqttHandleHass.getDtuUniqueId() + "_DPL";
object["cu"] = MqttHandleHass.getDtuUrl();
object["mf"] = "OpenDTU";
object["mdl"] = "Dynamic Power Limiter";
object["sw"] = __COMPILED_GIT_HASH__;
object["via_device"] = MqttHandleHass.getDtuUniqueId();
}
void MqttHandlePowerLimiterHassClass::publish(const String& subtopic, const String& payload)
{
String topic = Configuration.get().Mqtt.Hass.Topic;
topic += subtopic;
MqttSettings.publishGeneric(topic.c_str(), payload.c_str(), Configuration.get().Mqtt.Hass.Retain);
}