retry HA MQTT - #567

This commit is contained in:
proddy
2020-10-20 13:59:34 +02:00
parent e8951d1b7f
commit 2315f526be
4 changed files with 37 additions and 15 deletions

View File

@@ -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 // create the config topics for Home Assistant MQTT Discovery
// for each of the main elements // 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 // Create the Master device
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc; StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
doc["name"] = F("Service Code"); 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_(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_(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); 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 // send stuff to the Web UI
@@ -606,14 +612,14 @@ bool Boiler::export_values_main(JsonObject & output) {
// publish values via MQTT // publish values via MQTT
void Boiler::publish_values(JsonObject & data) { 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); DynamicJsonDocument doc_main(EMSESP_MAX_JSON_SIZE_LARGE);
JsonObject output_main = doc_main.to<JsonObject>(); JsonObject output_main = doc_main.to<JsonObject>();
if (export_values_main(output_main)) { 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<JsonObject>()); Mqtt::publish(F("boiler_data_main"), doc_main.as<JsonObject>());
} }

View File

@@ -46,7 +46,7 @@ class Boiler : public EMSdevice {
private: private:
static uuid::log::Logger logger_; static uuid::log::Logger logger_;
void register_mqtt_ha_config(); bool register_mqtt_ha_config();
void check_active(); void check_active();
bool export_values_main(JsonObject & doc); bool export_values_main(JsonObject & doc);
bool export_values_ww(JsonObject & doc); bool export_values_ww(JsonObject & doc);

View File

@@ -454,8 +454,9 @@ bool Thermostat::export_values_main(JsonObject & rootThermostat) {
if ((Mqtt::mqtt_format() == Mqtt::Format::HA) && (!ha_registered())) { 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 // see if we have already registered this with HA MQTT Discovery, if not send the config
register_mqtt_ha_config(); if (register_mqtt_ha_config()) {
ha_registered(true); ha_registered(true);
}
} }
return (rootThermostat.size()); return (rootThermostat.size());
@@ -626,8 +627,9 @@ bool Thermostat::export_values_hc(uint8_t mqtt_format, JsonObject & rootThermost
rootThermostat.clear(); // clear object rootThermostat.clear(); // clear object
} else if ((mqtt_format == Mqtt::Format::HA) && (!hc->ha_registered())) { } 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 // see if we have already registered this with HA MQTT Discovery, if not send the config
register_mqtt_ha_config(hc->hc_num()); if (register_mqtt_ha_config(hc->hc_num())) {
hc->ha_registered(true); hc->ha_registered(true);
}
} }
} }
} }
@@ -737,7 +739,11 @@ std::shared_ptr<Thermostat::HeatingCircuit> Thermostat::heating_circuit(std::sha
// publish config topic for HA MQTT Discovery // publish config topic for HA MQTT Discovery
// homeassistant/climate/ems-esp/thermostat/config // homeassistant/climate/ems-esp/thermostat/config
void Thermostat::register_mqtt_ha_config() { bool Thermostat::register_mqtt_ha_config() {
if (!Mqtt::connected()) {
return false;
}
StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc; StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
doc["uniq_id"] = F("thermostat"); doc["uniq_id"] = F("thermostat");
doc["ic"] = F("mdi:home-thermometer-outline"); 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_(wwtemplow), this->device_type(), "wwtemplow", F_(degrees), F_(icontemperature));
Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(wwcircmode), this->device_type(), "wwcircmode", nullptr, nullptr); Mqtt::register_mqtt_ha_sensor(nullptr, nullptr, F_(wwcircmode), this->device_type(), "wwcircmode", nullptr, nullptr);
} }
return true;
} }
// publish config topic for HA MQTT Discovery // publish config topic for HA MQTT Discovery
// e.g. homeassistant/climate/ems-esp/thermostat_hc1/config // 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<EMSESP_MAX_JSON_SIZE_MEDIUM> doc; StaticJsonDocument<EMSESP_MAX_JSON_SIZE_MEDIUM> doc;
char str1[40]; char str1[40];
@@ -907,6 +919,8 @@ void Thermostat::register_mqtt_ha_config(uint8_t hc_num) {
default: default:
break; break;
} }
return true;
} }
// for HA specifically when receiving over MQTT in the thermostat topic // for HA specifically when receiving over MQTT in the thermostat topic

View File

@@ -110,9 +110,11 @@ class Thermostat : public EMSdevice {
void add_commands(); void add_commands();
bool export_values_main(JsonObject & doc); bool export_values_main(JsonObject & doc);
bool export_values_hc(uint8_t mqtt_format, JsonObject & doc); bool export_values_hc(uint8_t mqtt_format, JsonObject & doc);
bool ha_registered() const { bool ha_registered() const {
return ha_registered_; return ha_registered_;
} }
void ha_registered(bool b) { void ha_registered(bool b) {
ha_registered_ = b; ha_registered_ = b;
} }
@@ -235,8 +237,8 @@ class Thermostat : public EMSdevice {
std::shared_ptr<Thermostat::HeatingCircuit> heating_circuit(std::shared_ptr<const Telegram> telegram); std::shared_ptr<Thermostat::HeatingCircuit> heating_circuit(std::shared_ptr<const Telegram> telegram);
std::shared_ptr<Thermostat::HeatingCircuit> heating_circuit(const uint8_t hc_num); std::shared_ptr<Thermostat::HeatingCircuit> heating_circuit(const uint8_t hc_num);
void register_mqtt_ha_config(); bool register_mqtt_ha_config();
void register_mqtt_ha_config(uint8_t hc_num); bool register_mqtt_ha_config(uint8_t hc_num);
bool thermostat_ha_cmd(const char * message, 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); bool command_info(const char * value, const int8_t id, JsonObject & output);