Skip to content

Commit f46b002

Browse files
committed
API call for shower coldshot - #1267
1 parent 65ea11b commit f46b002

File tree

6 files changed

+109
-38
lines changed

6 files changed

+109
-38
lines changed

src/locale_common.h

+1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ MAKE_WORD(info)
8080
MAKE_WORD(settings)
8181
MAKE_WORD(value)
8282
MAKE_WORD(entities)
83+
MAKE_WORD(coldshot)
8384

8485
// device types - lowercase, used in MQTT
8586
MAKE_WORD(boiler)

src/locale_translations.h

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ MAKE_WORD_TRANSLATION(system_info_cmd, "show system status", "Zeige System-Statu
7070
MAKE_WORD_TRANSLATION(schedule_cmd, "enable schedule item", "Aktiviere Zeitplan", "activeer tijdschema item", "", "aktywuj wybrany harmonogram", "", "", "program öğesini etkinleştir", "abilitare l'elemento programmato") // TODO translate
7171
MAKE_WORD_TRANSLATION(entity_cmd, "set custom value on ems", "Sende eigene Entitäten zu EMS", "verstuur custom waarde naar EMS", "", "wyślij własną wartość na EMS", "", "", "", "imposta valori personalizzati su EMS") // TODO translate
7272
MAKE_WORD_TRANSLATION(commands_response, "get response", "Hole Antwort", "Verzoek om antwoord", "", "", "", "", "") // TODO translate
73+
MAKE_WORD_TRANSLATION(coldshot_cmd, "send a cold shot of water", "", "", "", "", "", "", "", "") // TODO translate
7374

7475
// tags
7576
MAKE_WORD_TRANSLATION(tag_boiler_data_ww, "dhw", "WW", "dhw", "VV", "CWU", "dhw", "ecs", "SKS", "dhw")

src/shower.cpp

+76-27
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ namespace emsesp {
2222

2323
uuid::log::Logger Shower::logger_{F_(shower), uuid::log::Facility::CONSOLE};
2424

25+
static bool force_coldshot = false;
26+
2527
void Shower::start() {
2628
EMSESP::webSettingsService.read([&](WebSettings & settings) {
2729
shower_timer_ = settings.shower_timer;
@@ -30,7 +32,27 @@ void Shower::start() {
3032
shower_alert_coldshot_ = settings.shower_alert_coldshot * 1000; // convert from seconds
3133
});
3234

33-
set_shower_state(false, true); // turns shower to off and creates HA topic if not already done
35+
Command::add(
36+
EMSdevice::DeviceType::BOILER,
37+
F_(coldshot),
38+
[&](const char * value, const int8_t id, JsonObject & output) {
39+
LOG_INFO("Forcing coldshot...");
40+
if (shower_state_) {
41+
output["message"] = "OK";
42+
force_coldshot = true;
43+
} else {
44+
output["message"] = "Coldshot failed. Shower not active";
45+
LOG_WARNING("Coldshot failed. Shower not active");
46+
force_coldshot = false;
47+
}
48+
return true;
49+
},
50+
FL_(coldshot_cmd),
51+
CommandFlag::ADMIN_ONLY);
52+
53+
if (shower_timer_) {
54+
set_shower_state(false, true); // turns shower to off and creates HA topic if not already done
55+
}
3456
}
3557

3658
void Shower::loop() {
@@ -57,10 +79,10 @@ void Shower::loop() {
5779
// first check to see if hot water has been on long enough to be recognized as a Shower/Bath
5880
if (!shower_state_ && (time_now - timer_start_) > SHOWER_MIN_DURATION) {
5981
set_shower_state(true);
60-
LOG_DEBUG("[Shower] hot water still running, starting shower timer");
82+
LOG_DEBUG("hot water still running, starting shower timer");
6183
}
6284
// check if the shower has been on too long
63-
else if ((time_now - timer_start_) > shower_alert_trigger_) {
85+
else if ((shower_alert_ && ((time_now - timer_start_) > shower_alert_trigger_)) || force_coldshot) {
6486
shower_alert_start();
6587
}
6688
}
@@ -79,11 +101,11 @@ void Shower::loop() {
79101
if (duration_ > SHOWER_MIN_DURATION) {
80102
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> doc;
81103

82-
char s[50];
83-
snprintf(s, 50, "%d minutes and %d seconds", (uint8_t)(duration_ / 60000), (uint8_t)((duration_ / 1000) % 60));
84-
doc["duration"] = s;
104+
// char s[50];
105+
// snprintf(s, 50, "%02u:%02u:%02u", (uint8_t)(duration_ / 3600000UL), (uint8_t)(duration_ / 60000UL), (uint8_t)((duration_ / 1000UL) % 60));
106+
doc["duration"] = (uint8_t)(duration_ / 1000UL); // seconds
85107
Mqtt::queue_publish("shower_data", doc.as<JsonObject>());
86-
LOG_DEBUG("[Shower] finished with duration %d", duration_);
108+
LOG_INFO("finished with duration %d", duration_);
87109
}
88110
}
89111

@@ -106,37 +128,34 @@ void Shower::loop() {
106128
}
107129
}
108130

131+
// turn off hot water to send a shot of cold
132+
void Shower::shower_alert_start() {
133+
LOG_DEBUG("Shower Alert started");
134+
(void)Command::call(EMSdevice::DeviceType::BOILER, "wwtapactivated", "false");
135+
doing_cold_shot_ = true;
136+
force_coldshot = false;
137+
alert_timer_start_ = uuid::get_uptime(); // timer starts now
138+
}
139+
109140
// turn back on the hot water for the shower
110141
void Shower::shower_alert_stop() {
111142
if (doing_cold_shot_) {
112143
LOG_DEBUG("Shower Alert stopped");
113144
(void)Command::call(EMSdevice::DeviceType::BOILER, "wwtapactivated", "true");
114145
doing_cold_shot_ = false;
115-
}
116-
}
117-
// turn off hot water to send a shot of cold
118-
void Shower::shower_alert_start() {
119-
if (shower_alert_) {
120-
LOG_DEBUG("Shower Alert started");
121-
(void)Command::call(EMSdevice::DeviceType::BOILER, "wwtapactivated", "false");
122-
doing_cold_shot_ = true;
123-
alert_timer_start_ = uuid::get_uptime(); // timer starts now
146+
force_coldshot = false;
124147
}
125148
}
126149

127150
// send status of shower to MQTT topic called shower_active - which is determined by the state parameter
128151
// and creates the HA config topic if HA enabled
129152
// force is used by EMSESP::publish_all_loop()
130153
void Shower::set_shower_state(bool state, bool force) {
131-
if (!shower_timer_ && !shower_alert_) {
132-
return;
133-
}
134-
135154
// sets the state
136155
shower_state_ = state;
137156

138157
// only publish if that state has changed
139-
static bool old_shower_state_;
158+
static bool old_shower_state_ = false;
140159
if ((shower_state_ == old_shower_state_) && !force) {
141160
return;
142161
}
@@ -149,10 +168,15 @@ void Shower::set_shower_state(bool state, bool force) {
149168
// send out HA MQTT Discovery config topic
150169
if ((Mqtt::ha_enabled()) && (!ha_configdone_ || force)) {
151170
StaticJsonDocument<EMSESP_JSON_SIZE_LARGE> doc;
171+
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
172+
char str[70];
173+
char stat_t[50];
152174

175+
//
176+
// shower_active topic
177+
//
153178
doc["name"] = "Shower Active";
154179

155-
char str[70];
156180
if (Mqtt::entity_format() == Mqtt::entityFormat::MULTI_SHORT) {
157181
snprintf(str, sizeof(str), "%s_shower_active", Mqtt::basename().c_str());
158182
} else {
@@ -161,7 +185,6 @@ void Shower::set_shower_state(bool state, bool force) {
161185
doc["uniq_id"] = str;
162186
doc["object_id"] = str;
163187

164-
char stat_t[50];
165188
snprintf(stat_t, sizeof(stat_t), "%s/shower_active", Mqtt::basename().c_str());
166189
doc["stat_t"] = stat_t;
167190

@@ -181,13 +204,39 @@ void Shower::set_shower_state(bool state, bool force) {
181204
JsonArray ids = dev.createNestedArray("ids");
182205
ids.add(Mqtt::basename());
183206

184-
// add "availability" section
185-
Mqtt::add_avty_to_doc(stat_t, doc.as<JsonObject>());
207+
Mqtt::add_avty_to_doc(stat_t, doc.as<JsonObject>()); // add "availability" section
186208

187-
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
188209
snprintf(topic, sizeof(topic), "binary_sensor/%s/shower_active/config", Mqtt::basename().c_str());
189-
190210
ha_configdone_ = Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
211+
212+
//
213+
// shower_duaration topic
214+
//
215+
doc.clear();
216+
217+
snprintf(str, sizeof(str), "%s_shower_duration", Mqtt::basename().c_str());
218+
219+
doc["uniq_id"] = str;
220+
doc["object_id"] = str;
221+
222+
snprintf(stat_t, sizeof(stat_t), "%s/shower_data", Mqtt::basename().c_str());
223+
doc["stat_t"] = stat_t;
224+
225+
doc["name"] = "Shower Duration";
226+
doc["val_tpl"] = "{{value_json.duration if value_json.duration is defined else 0}}";
227+
doc["unit_of_meas"] = "s";
228+
doc["stat_cla"] = "measurement";
229+
doc["dev_cla"] = "duration";
230+
doc["ent_cat"] = "diagnostic";
231+
232+
JsonObject dev2 = doc.createNestedObject("dev");
233+
JsonArray ids2 = dev2.createNestedArray("ids");
234+
ids2.add(Mqtt::basename());
235+
236+
Mqtt::add_avty_to_doc(stat_t, doc.as<JsonObject>(), "value_json.duration is defined"); // add "availability" section
237+
238+
snprintf(topic, sizeof(topic), "sensor/%s/shower_duration/config", Mqtt::basename().c_str());
239+
Mqtt::queue_ha(topic, doc.as<JsonObject>()); // publish the config payload with retain flag
191240
}
192241
}
193242

src/shower.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class Shower {
3030

3131
void set_shower_state(bool state, bool force = false);
3232

33+
// commands
34+
static bool command_coldshot(const char * value, const int8_t id);
35+
3336
private:
3437
static uuid::log::Logger logger_;
3538

@@ -46,9 +49,9 @@ class Shower {
4649
uint32_t shower_alert_coldshot_; // default 10 seconds for cold water before turning back hot water
4750
bool ha_configdone_ = false; // for HA MQTT Discovery
4851
bool shower_state_;
49-
uint32_t timer_start_; // ms
50-
uint32_t timer_pause_; // ms
51-
uint32_t duration_; // ms
52+
uint32_t timer_start_; // ms
53+
uint32_t timer_pause_; // ms
54+
uint32_t duration_; // ms
5255

5356
// cold shot
5457
uint32_t alert_timer_start_; // ms

src/test/test.cpp

+22-7
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
266266
shell.printfln("Testing adding a general boiler & thermostat...");
267267
run_test("general");
268268
// shell.invoke_command("show devices");
269-
// shell.invoke_command("show values");
269+
shell.invoke_command("show values");
270270
shell.invoke_command("call system publish");
271271
// shell.invoke_command("show mqtt");
272272
ok = true;
@@ -280,6 +280,21 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
280280
ok = true;
281281
}
282282

283+
if (command == "coldshot") {
284+
shell.printfln("Testing coldshot...");
285+
run_test("general");
286+
287+
#ifdef EMSESP_STANDALONE
288+
AsyncWebServerRequest request;
289+
request.method(HTTP_GET);
290+
request.url("/api/boiler/coldshot");
291+
EMSESP::webAPIService.webAPIService_get(&request);
292+
#else
293+
shell.invoke_command("call boiler coldshot");
294+
#endif
295+
ok = true;
296+
}
297+
283298
if (command == "string2minutes") {
284299
shell.printfln("Testing string2minutes()...");
285300
std::string time_s = "12:00";
@@ -1005,10 +1020,10 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
10051020
EMSESP::mqtt_.incoming("ems-esp/thermostat_hc1", "22"); // HA only
10061021
EMSESP::mqtt_.incoming("ems-esp/thermostat_hc1", "off"); // HA only
10071022
EMSESP::mqtt_.incoming("ems-esp/system/send", "11 12 13");
1008-
EMSESP::mqtt_.incoming("ems-esp/boiler/syspress"); // empty payload
1009-
EMSESP::mqtt_.incoming("ems-esp/thermostat/mode"); // empty payload
1023+
EMSESP::mqtt_.incoming("ems-esp/boiler/syspress"); // empty payload
1024+
EMSESP::mqtt_.incoming("ems-esp/thermostat/mode"); // empty payload
10101025
EMSESP::mqtt_.incoming("ems-esp/system/publish");
1011-
EMSESP::mqtt_.incoming("ems-esp/thermostat/seltemp"); // empty payload
1026+
EMSESP::mqtt_.incoming("ems-esp/thermostat/seltemp"); // empty payload
10121027

10131028
EMSESP::mqtt_.incoming("ems-esp/boiler/wwseltemp", "59");
10141029
EMSESP::mqtt_.incoming("ems-esp/boiler/wwseltemp");
@@ -1204,7 +1219,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
12041219
uart_telegram("30 00 FF 0A 02 6A 04"); // SM100 pump on 1
12051220
uart_telegram("30 00 FF 00 02 64 00 00 00 04 00 00 FF 00 00 1E 0B 09 64 00 00 00 00"); // SM100 modulation
12061221

1207-
uart_telegram("30 00 FF 0A 02 6A 03"); // SM100 pump off 0
1222+
uart_telegram("30 00 FF 0A 02 6A 03"); // SM100 pump off 0
12081223

12091224
shell.invoke_command("show");
12101225
ok = true;
@@ -1446,7 +1461,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
14461461
shell.invoke_command("call");
14471462
shell.invoke_command("call system info");
14481463

1449-
EMSESP::mqtt_.incoming("ems-esp/system", "{\"cmd\":\"info\"}"); // this should fail
1464+
EMSESP::mqtt_.incoming("ems-esp/system", "{\"cmd\":\"info\"}"); // this should fail
14501465

14511466
EMSESP::mqtt_.incoming("ems-esp/thermostat", "{\"cmd\":\"temp\",\"data\":23.45}"); // this should work just fine
14521467
EMSESP::mqtt_.incoming("ems-esp/thermostat", "{\"cmd\":\"TeMP\",\"data\":23.45}"); // test mix cased cmd
@@ -1559,7 +1574,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
15591574
// EMSESP::mqtt_.incoming(system_topic, "{\"cmd\":\"pin\",\"id\":12,\"data\":\"1\"}");
15601575

15611576
EMSESP::mqtt_.incoming(thermostat_topic, "{\"cmd\":\"wwmode\",\"data\":\"auto\"}");
1562-
EMSESP::mqtt_.incoming(thermostat_topic, "{\"cmd\":\"mode\",\"data\":\"typo\",\"id\":2}"); // invalid mode
1577+
EMSESP::mqtt_.incoming(thermostat_topic, "{\"cmd\":\"mode\",\"data\":\"typo\",\"id\":2}"); // invalid mode
15631578
EMSESP::mqtt_.incoming(thermostat_topic, "{\"cmd\":\"mode\",\"data\":\"auto\",\"id\":2}");
15641579
EMSESP::mqtt_.incoming(thermostat_topic, "{\"cmd\":\"mode\",\"data\":\"auto\",\"hc\":2}"); // hc as number
15651580
EMSESP::mqtt_.incoming(thermostat_topic, "{\"cmd\":\"seltemp\",\"data\":19.5,\"hc\":1}"); // data as number

src/test/test.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626

2727
namespace emsesp {
2828

29+
// #define EMSESP_DEBUG_DEFAULT "general"
30+
2931
// #define EMSESP_DEBUG_DEFAULT "thermostat"
3032
// #define EMSESP_DEBUG_DEFAULT "solar"
3133
// #define EMSESP_DEBUG_DEFAULT "web"
3234
// #define EMSESP_DEBUG_DEFAULT "mqtt"
33-
#define EMSESP_DEBUG_DEFAULT "general"
3435
// #define EMSESP_DEBUG_DEFAULT "boiler"
3536
// #define EMSESP_DEBUG_DEFAULT "mqtt2"
3637
// #define EMSESP_DEBUG_DEFAULT "mqtt_nested"
@@ -53,6 +54,7 @@ namespace emsesp {
5354
// #define EMSESP_DEBUG_DEFAULT "custom"
5455
// #define EMSESP_DEBUG_DEFAULT "entity_dump"
5556
// #define EMSESP_DEBUG_DEFAULT "memory"
57+
#define EMSESP_DEBUG_DEFAULT "coldshot"
5658

5759
class Test {
5860
public:

0 commit comments

Comments
 (0)