dynamically add HA thermostat modes based on model

This commit is contained in:
proddy
2025-12-13 11:22:22 +01:00
parent c71b54fb5b
commit d3b4bab40a
3 changed files with 69 additions and 36 deletions

View File

@@ -546,7 +546,7 @@ void Mqtt::ha_status() {
dev["mf"] = "EMS-ESP";
dev["mdl"] = "EMS-ESP";
#ifndef EMSESP_STANDALONE
dev["cu"] = "http://" + (EMSESP::system_.ethernet_connected() ? ETH.localIP().toString() : WiFi.localIP().toString());
dev["cu"] = std::string("http://") + EMSESP::system_.get_ip_or_hostname().c_str();
#endif
JsonArray ids = dev["ids"].to<JsonArray>();
ids.add(Mqtt::basename());
@@ -1103,6 +1103,12 @@ bool Mqtt::publish_ha_sensor_config(uint8_t type, // EMSdev
// https://github.com/emsesp/EMS-ESP32/discussions/1459#discussioncomment-7694873
add_ha_classes(doc.as<JsonObject>(), device_type, type, uom, entity);
// add origin
JsonObject origin_json = doc["o"].to<JsonObject>();
origin_json["name"] = "EMS-ESP";
origin_json["sw"] = EMSESP_APP_VERSION;
origin_json["url"] = "https://emsesp.org";
// add dev section
if (device_type == EMSdevice::DeviceType::SYSTEM) {
add_ha_dev_section(doc.as<JsonObject>(), nullptr, nullptr, nullptr, nullptr, false);
@@ -1236,7 +1242,9 @@ void Mqtt::add_ha_classes(JsonObject doc, const uint8_t device_type, const uint8
}
}
bool Mqtt::publish_ha_climate_config(const DeviceValue & dv, const bool has_roomtemp, const bool remove) {
// publish the HA climate config
// https://www.home-assistant.io/integrations/climate.mqtt/
bool Mqtt::publish_ha_climate_config(const DeviceValue & dv, const bool has_roomtemp, const char * const ** mode_options, const bool remove) {
int8_t tag = dv.tag;
int16_t min = dv.min;
uint32_t max = dv.max;
@@ -1356,34 +1364,43 @@ bool Mqtt::publish_ha_climate_config(const DeviceValue & dv, const bool has_room
doc["act_t"] = "~/boiler_data";
doc["act_tpl"] = "{% if value_json.hpactivity=='cooling'%}cooling{%elif value_json.heatingactive=='on'%}heating{%else%}idle{%endif%}";
// the HA climate component only responds to auto, heat and off
JsonArray modes = doc["modes"].to<JsonArray>();
// go through dv.options and map to HA climate modes
// https://www.home-assistant.io/integrations/climate.mqtt/
// HA supports: ["auto", "off", "cool", "heat", "dry", "fan_only"]
if (dv.options != nullptr) {
// map EMS modes to HA climate modes
// EMS modes: auto, manual, heat, off, night, day, nofrost, eco, comfort, cool)
// HA supports: auto, off, cool, heat, dry, fan_only
if (mode_options != nullptr) {
// scan through mode_options and add to modes
bool found_auto = false;
bool found_heat = false;
bool found_off = false;
for (uint8_t i = 0; i < dv.options_size; i++) {
const char * option = dv.options[i][0];
if (strcmp(option, "auto") == 0) {
bool found_cool = false;
for (uint8_t i = 0; i < Helpers::count_items(mode_options); i++) {
const char * mode = mode_options[i][0]; // take EN
if (!strcmp(mode, FL_(auto)[0])) {
found_auto = true;
} else if (strcmp(option, "heat") == 0) {
} else if (!strcmp(mode, FL_(heat)[0])) {
found_heat = true;
} else if (strcmp(option, "off") == 0) {
} else if (!strcmp(mode, FL_(off)[0])) {
found_off = true;
} else if (!strcmp(mode, FL_(cool)[0])) {
found_cool = true;
}
}
if (found_auto) {
modes.add("auto");
}
if (found_heat) {
modes.add("heat");
}
if (found_off) {
modes.add("off");
// only add modes if we found at least one
if (found_auto || found_heat || found_off || found_cool) {
JsonArray modes = doc["modes"].to<JsonArray>();
if (found_auto) {
modes.add("auto");
}
if (found_heat) {
modes.add("heat");
}
if (found_off) {
modes.add("off");
}
if (found_cool) {
modes.add("cool");
}
}
}