From c3757f95e5e982b5a349d05bcab8162d2b3ae260 Mon Sep 17 00:00:00 2001 From: Proddy Date: Wed, 20 Jul 2022 12:04:01 +0200 Subject: [PATCH] only send MQTT after shower finished --- src/shower.cpp | 36 ++++++------------------------------ src/shower.h | 20 -------------------- 2 files changed, 6 insertions(+), 50 deletions(-) diff --git a/src/shower.cpp b/src/shower.cpp index 8f024dfa4..b9f537c4f 100644 --- a/src/shower.cpp +++ b/src/shower.cpp @@ -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 if (!shower_state_ && (time_now - timer_start_) > SHOWER_MIN_DURATION) { set_shower_state(true); - publish_shower_data(); LOG_DEBUG(F("[Shower] hot water still running, starting shower timer")); } // check if the shower has been on too long @@ -78,7 +77,12 @@ void Shower::loop() { if ((timer_pause_ - timer_start_) > SHOWER_OFFSET_TIME) { duration_ = (timer_pause_ - timer_start_ - SHOWER_OFFSET_TIME); if (duration_ > SHOWER_MIN_DURATION) { - publish_shower_data(); + StaticJsonDocument 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()); 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 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()); -} - // 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 // force is used by EMSESP::publish_all_loop() diff --git a/src/shower.h b/src/shower.h index b4fb29f7c..895d2cfad 100644 --- a/src/shower.h +++ b/src/shower.h @@ -30,25 +30,6 @@ class Shower { 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: 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_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_stop();