From c120268409b04bf2658a2c6ccd21eb58424a85af Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 6 Mar 2021 18:52:45 +0100 Subject: [PATCH] fixes for publish ha --- src/emsesp.cpp | 5 +++++ src/shower.cpp | 23 +++++++++++++++++------ src/shower.h | 2 +- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 0c38f8c76..fc8fc7000 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -392,7 +392,11 @@ void EMSESP::publish_all_loop() { publish_sensor_values(true, true); break; case 7: + if (Mqtt::ha_enabled()) { + Mqtt::ha_status(); + } system_.send_heartbeat(); + shower_.send_mqtt_stat(false, true); break; default: // all finished @@ -409,6 +413,7 @@ void EMSESP::reset_mqtt_ha() { for (const auto & emsdevice : emsdevices) { emsdevice->ha_config_done(false); } + dallassensor_.reload(); } // create json doc for the devices values and add to MQTT publish queue diff --git a/src/shower.cpp b/src/shower.cpp index 971a43b37..c6e768401 100644 --- a/src/shower.cpp +++ b/src/shower.cpp @@ -93,7 +93,7 @@ void Shower::loop() { } // send status of shower to MQTT -void Shower::send_mqtt_stat(bool state) { +void Shower::send_mqtt_stat(bool state, bool force) { if (!shower_timer_ && !shower_alert_) { return; } @@ -102,7 +102,7 @@ void Shower::send_mqtt_stat(bool state) { Mqtt::publish(F("shower_active"), Helpers::render_boolean(s, state)); // https://github.com/proddy/EMS-ESP/issues/369 // if we're in HA mode make sure we've first sent out the HA MQTT Discovery config topic - if ((Mqtt::ha_enabled()) && (!ha_configdone_)) { + if ((Mqtt::ha_enabled()) && (!ha_configdone_ || force)) { ha_configdone_ = true; StaticJsonDocument doc; @@ -115,7 +115,7 @@ void Shower::send_mqtt_stat(bool state) { ids.add("ems-esp"); char topic[100]; - snprintf_P(topic, sizeof(topic), PSTR("homeassistant/binary_sensor/%s/shower_active/config"), EMSESP::system_.hostname().c_str()); + snprintf_P(topic, sizeof(topic), PSTR("homeassistant/binary_sensor/%s/shower_active/config"), Mqtt::base().c_str()); Mqtt::publish_ha(topic, doc.as()); // publish the config payload with retain flag } } @@ -146,12 +146,23 @@ void Shower::shower_alert_start() { void Shower::publish_values() { StaticJsonDocument doc; - char s[50]; - doc["shower_timer"] = Helpers::render_boolean(s, shower_timer_); - doc["shower_alert"] = Helpers::render_boolean(s, shower_alert_); + if (Mqtt::bool_format() == BOOL_FORMAT_ONOFF) { + doc["shower_timer"] = shower_timer_ ? "on" : "off"; + doc["shower_alert"] = shower_alert_ ? "on" : "off"; + } else if (Mqtt::bool_format() == BOOL_FORMAT_ONOFF_CAP) { + doc["shower_timer"] = shower_timer_ ? "ON" : "OFF"; + doc["shower_alert"] = shower_alert_ ? "ON" : "OFF"; + } else if (Mqtt::bool_format() == BOOL_FORMAT_TRUEFALSE) { + doc["shower_timer"] = shower_timer_; + doc["shower_alert"] = shower_alert_; + } else { + doc["shower_timer"] = shower_timer_ ? 1 : 0; + doc["shower_alert"] = shower_alert_ ? 1 : 0; + } // only publish shower duration if there is a value if (duration_ > SHOWER_MIN_DURATION) { + char s[50]; snprintf_P(s, 50, PSTR("%d minutes and %d seconds"), (uint8_t)(duration_ / 60000), (uint8_t)((duration_ / 1000) % 60)); doc["duration"] = s; } diff --git a/src/shower.h b/src/shower.h index 75b0c0c89..0c6dc7e3f 100644 --- a/src/shower.h +++ b/src/shower.h @@ -28,7 +28,7 @@ class Shower { void start(); void loop(); - void send_mqtt_stat(bool state); + void send_mqtt_stat(bool state, bool force = false); bool shower_alert() const { return shower_alert_;