diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index bde16f2dc..22244fbdb 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -80,7 +80,11 @@ Boiler::Boiler(uint8_t device_type, int8_t device_id, uint8_t product_id, const // create the config topics for Home Assistant MQTT Discovery // for each of the main elements -void Boiler::register_mqtt_ha_config() { +bool Boiler::register_mqtt_ha_config() { + if (!Mqtt::connected()) { + return false; + } + // Create the Master device StaticJsonDocument doc; doc["name"] = F("Service Code"); @@ -164,6 +168,8 @@ void Boiler::register_mqtt_ha_config() { Mqtt::register_mqtt_ha_sensor(nullptr, F_(mqtt_suffix_ww), F_(wwBufferTemperature), this->device_type(), "wwBufferTemperature", F_(degrees), F_(icontemperature)); Mqtt::register_mqtt_ha_sensor(nullptr, F_(mqtt_suffix_ww), F_(wWStarts), this->device_type(), "wWStarts", nullptr, nullptr); Mqtt::register_mqtt_ha_sensor(nullptr, F_(mqtt_suffix_ww), F_(wWWorkM), this->device_type(), "wWWorkM", nullptr, nullptr); + + return true; } // send stuff to the Web UI @@ -606,14 +612,14 @@ bool Boiler::export_values_main(JsonObject & output) { // publish values via MQTT void Boiler::publish_values(JsonObject & data) { + // handle HA first + if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!mqtt_ha_config_)) { + mqtt_ha_config_ = register_mqtt_ha_config(); + } + DynamicJsonDocument doc_main(EMSESP_MAX_JSON_SIZE_LARGE); JsonObject output_main = doc_main.to(); if (export_values_main(output_main)) { - // see if we need to send out HA MQTT Discovery topics - if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!mqtt_ha_config_)) { - register_mqtt_ha_config(); - mqtt_ha_config_ = true; - } Mqtt::publish(F("boiler_data_main"), doc_main.as()); } diff --git a/src/devices/boiler.h b/src/devices/boiler.h index 5c3f91e68..e3a317ba3 100644 --- a/src/devices/boiler.h +++ b/src/devices/boiler.h @@ -46,7 +46,7 @@ class Boiler : public EMSdevice { private: static uuid::log::Logger logger_; - void register_mqtt_ha_config(); + bool register_mqtt_ha_config(); void check_active(); bool export_values_main(JsonObject & doc); bool export_values_ww(JsonObject & doc); diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index 001c7701a..e48ad7c2c 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -454,8 +454,9 @@ bool Thermostat::export_values_main(JsonObject & rootThermostat) { if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!ha_registered())) { // see if we have already registered this with HA MQTT Discovery, if not send the config - register_mqtt_ha_config(); - ha_registered(true); + if (register_mqtt_ha_config()) { + ha_registered(true); + } } return (rootThermostat.size()); @@ -626,8 +627,9 @@ bool Thermostat::export_values_hc(uint8_t mqtt_format, JsonObject & rootThermost rootThermostat.clear(); // clear object } else if ((mqtt_format == Mqtt::Format::HA) && (!hc->ha_registered())) { // see if we have already registered this with HA MQTT Discovery, if not send the config - register_mqtt_ha_config(hc->hc_num()); - hc->ha_registered(true); + if (register_mqtt_ha_config(hc->hc_num())) { + hc->ha_registered(true); + } } } } @@ -737,7 +739,11 @@ std::shared_ptr Thermostat::heating_circuit(std::sha // publish config topic for HA MQTT Discovery // homeassistant/climate/ems-esp/thermostat/config -void Thermostat::register_mqtt_ha_config() { +bool Thermostat::register_mqtt_ha_config() { + if (!Mqtt::connected()) { + return false; + } + StaticJsonDocument doc; doc["uniq_id"] = F("thermostat"); doc["ic"] = F("mdi:home-thermometer-outline"); @@ -777,11 +783,17 @@ void Thermostat::register_mqtt_ha_config() { Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(wwtemplow), this->device_type(), "wwtemplow", F_(degrees), F_(icontemperature)); Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(wwcircmode), this->device_type(), "wwcircmode", nullptr, nullptr); } + + return true; } // publish config topic for HA MQTT Discovery // e.g. homeassistant/climate/ems-esp/thermostat_hc1/config -void Thermostat::register_mqtt_ha_config(uint8_t hc_num) { +bool Thermostat::register_mqtt_ha_config(uint8_t hc_num) { + if (!Mqtt::connected()) { + return false; + } + StaticJsonDocument doc; char str1[40]; @@ -907,6 +919,8 @@ void Thermostat::register_mqtt_ha_config(uint8_t hc_num) { default: break; } + + return true; } // for HA specifically when receiving over MQTT in the thermostat topic diff --git a/src/devices/thermostat.h b/src/devices/thermostat.h index 13a132490..a8af5bc09 100644 --- a/src/devices/thermostat.h +++ b/src/devices/thermostat.h @@ -110,9 +110,11 @@ class Thermostat : public EMSdevice { void add_commands(); bool export_values_main(JsonObject & doc); bool export_values_hc(uint8_t mqtt_format, JsonObject & doc); + bool ha_registered() const { return ha_registered_; } + void ha_registered(bool b) { ha_registered_ = b; } @@ -235,8 +237,8 @@ class Thermostat : public EMSdevice { std::shared_ptr heating_circuit(std::shared_ptr telegram); std::shared_ptr heating_circuit(const uint8_t hc_num); - void register_mqtt_ha_config(); - void register_mqtt_ha_config(uint8_t hc_num); + bool register_mqtt_ha_config(); + bool register_mqtt_ha_config(uint8_t hc_num); bool thermostat_ha_cmd(const char * message, uint8_t hc_num); bool command_info(const char * value, const int8_t id, JsonObject & output);