only send MQTT after shower finished

This commit is contained in:
Proddy
2022-07-20 12:04:01 +02:00
parent 66fbd2b359
commit c3757f95e5
2 changed files with 6 additions and 50 deletions

View File

@@ -57,7 +57,6 @@ void Shower::loop() {
// first check to see if hot water has been on long enough to be recognized as a Shower/Bath // first check to see if hot water has been on long enough to be recognized as a Shower/Bath
if (!shower_state_ && (time_now - timer_start_) > SHOWER_MIN_DURATION) { if (!shower_state_ && (time_now - timer_start_) > SHOWER_MIN_DURATION) {
set_shower_state(true); set_shower_state(true);
publish_shower_data();
LOG_DEBUG(F("[Shower] hot water still running, starting shower timer")); LOG_DEBUG(F("[Shower] hot water still running, starting shower timer"));
} }
// check if the shower has been on too long // check if the shower has been on too long
@@ -78,7 +77,12 @@ void Shower::loop() {
if ((timer_pause_ - timer_start_) > SHOWER_OFFSET_TIME) { if ((timer_pause_ - timer_start_) > SHOWER_OFFSET_TIME) {
duration_ = (timer_pause_ - timer_start_ - SHOWER_OFFSET_TIME); duration_ = (timer_pause_ - timer_start_ - SHOWER_OFFSET_TIME);
if (duration_ > SHOWER_MIN_DURATION) { if (duration_ > SHOWER_MIN_DURATION) {
publish_shower_data(); StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> doc;
char s[50];
snprintf(s, 50, "%d minutes and %d seconds", (uint8_t)(duration_ / 60000), (uint8_t)((duration_ / 1000) % 60));
doc["duration"] = s;
Mqtt::publish(F("shower_data"), doc.as<JsonObject>());
LOG_DEBUG(F("[Shower] finished with duration %d"), duration_); LOG_DEBUG(F("[Shower] finished with duration %d"), duration_);
} }
} }
@@ -120,34 +124,6 @@ void Shower::shower_alert_start() {
} }
} }
// Publish to the shower_data topic
// showing whether the shower timer and alert are enabled or disabled
// and the duration of the last shower
void Shower::publish_shower_data() const {
StaticJsonDocument<EMSESP_JSON_SIZE_SMALL> doc;
if (EMSESP::system_.bool_format() == BOOL_FORMAT_TRUEFALSE) {
doc["shower_timer"] = shower_timer_;
doc["shower_alert"] = shower_alert_;
} else if (EMSESP::system_.bool_format() == BOOL_FORMAT_10) {
doc["shower_timer"] = shower_timer_ ? 1 : 0;
doc["shower_alert"] = shower_alert_ ? 1 : 0;
} else {
char result[10];
doc["shower_timer"] = Helpers::render_boolean(result, shower_timer_);
doc["shower_alert"] = Helpers::render_boolean(result, shower_alert_);
}
// only publish shower duration if there is a value
if (duration_ > SHOWER_MIN_DURATION) {
char s[50];
snprintf(s, 50, "%d minutes and %d seconds", (uint8_t)(duration_ / 60000), (uint8_t)((duration_ / 1000) % 60));
doc["duration"] = s;
}
Mqtt::publish(F("shower_data"), doc.as<JsonObject>());
}
// send status of shower to MQTT topic called shower_active - which is determined by the state parameter // send status of shower to MQTT topic called shower_active - which is determined by the state parameter
// and creates the HA config topic if HA enabled // and creates the HA config topic if HA enabled
// force is used by EMSESP::publish_all_loop() // force is used by EMSESP::publish_all_loop()

View File

@@ -30,25 +30,6 @@ class Shower {
void set_shower_state(bool state, bool force = false); void set_shower_state(bool state, bool force = false);
/* unused header
*
bool shower_alert() const {
return shower_alert_;
}
void shower_alert(const bool shower_alert) {
shower_alert_ = shower_alert;
}
bool shower_timer() const {
return shower_timer_;
}
void shower_timer(const bool shower_timer) {
shower_timer_ = shower_timer;
}
*/
private: private:
static uuid::log::Logger logger_; static uuid::log::Logger logger_;
@@ -56,7 +37,6 @@ class Shower {
static constexpr uint32_t SHOWER_MIN_DURATION = 120000; // in ms. 2 minutes, before recognizing its a shower static constexpr uint32_t SHOWER_MIN_DURATION = 120000; // in ms. 2 minutes, before recognizing its a shower
static constexpr uint32_t SHOWER_OFFSET_TIME = 5000; // in ms. 5 seconds grace time, to calibrate actual time under the shower static constexpr uint32_t SHOWER_OFFSET_TIME = 5000; // in ms. 5 seconds grace time, to calibrate actual time under the shower
void publish_shower_data() const;
void shower_alert_start(); void shower_alert_start();
void shower_alert_stop(); void shower_alert_stop();