mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 00:39:50 +03:00
retry HA MQTT - #567
This commit is contained in:
@@ -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<EMSESP_MAX_JSON_SIZE_MEDIUM> 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<JsonObject>();
|
||||
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>());
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -454,9 +454,10 @@ 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();
|
||||
if (register_mqtt_ha_config()) {
|
||||
ha_registered(true);
|
||||
}
|
||||
}
|
||||
|
||||
return (rootThermostat.size());
|
||||
}
|
||||
@@ -626,11 +627,12 @@ 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());
|
||||
if (register_mqtt_ha_config(hc->hc_num())) {
|
||||
hc->ha_registered(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (has_data);
|
||||
}
|
||||
@@ -737,7 +739,11 @@ std::shared_ptr<Thermostat::HeatingCircuit> 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<EMSESP_MAX_JSON_SIZE_MEDIUM> 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<EMSESP_MAX_JSON_SIZE_MEDIUM> 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
|
||||
|
||||
@@ -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<Thermostat::HeatingCircuit> heating_circuit(std::shared_ptr<const Telegram> telegram);
|
||||
std::shared_ptr<Thermostat::HeatingCircuit> 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);
|
||||
|
||||
Reference in New Issue
Block a user