From 17a9b7eb0ab91a1ac1618f7116193e1afaad7905 Mon Sep 17 00:00:00 2001 From: proddy Date: Fri, 12 Dec 2025 21:58:35 +0100 Subject: [PATCH] send HA modes based on actual thermostat modes --- src/core/emsdevice.cpp | 6 +++--- src/core/mqtt.cpp | 35 +++++++++++++++++++++++++++++++---- src/core/mqtt.h | 2 +- src/emsesp_version.h | 2 +- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/core/emsdevice.cpp b/src/core/emsdevice.cpp index 8b5ec063b..828c38e40 100644 --- a/src/core/emsdevice.cpp +++ b/src/core/emsdevice.cpp @@ -2050,14 +2050,14 @@ void EMSdevice::mqtt_ha_entity_config_create() { // create climate when we reach the haclimate entity if (!strcmp(dv.short_name, FL_(haclimate)[0]) && !dv.has_state(DeviceValueState::DV_API_MQTT_EXCLUDE) && dv.has_state(DeviceValueState::DV_ACTIVE)) { if (*(int8_t *)(dv.value_p) == 1 && (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) || dv.has_state(DeviceValueState::DV_HA_CLIMATE_NO_RT))) { - if (Mqtt::publish_ha_climate_config(dv.tag, true, false, dv.min, dv.max)) { // roomTemp + if (Mqtt::publish_ha_climate_config(dv, true, false)) { // roomTemp dv.remove_state(DeviceValueState::DV_HA_CLIMATE_NO_RT); dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); count++; } } else if (*(int8_t *)(dv.value_p) == 0 && (!dv.has_state(DeviceValueState::DV_HA_CONFIG_CREATED) || !dv.has_state(DeviceValueState::DV_HA_CLIMATE_NO_RT))) { - if (Mqtt::publish_ha_climate_config(dv.tag, false, false, dv.min, dv.max)) { // no roomTemp + if (Mqtt::publish_ha_climate_config(dv, false, false)) { // no roomTemp dv.add_state(DeviceValueState::DV_HA_CLIMATE_NO_RT); dv.add_state(DeviceValueState::DV_HA_CONFIG_CREATED); count++; @@ -2075,7 +2075,7 @@ void EMSdevice::mqtt_ha_entity_config_create() { } // SRC thermostats mapped to connect/src1/... if (dv.tag >= DeviceValueTAG::TAG_SRC1 && dv.tag <= DeviceValueTAG::TAG_SRC16 && !strcmp(dv.short_name, FL_(selRoomTemp)[0])) { - Mqtt::publish_ha_climate_config(dv.tag, true, false, dv.min, dv.max); + Mqtt::publish_ha_climate_config(dv, true, false); } #ifndef EMSESP_STANDALONE diff --git a/src/core/mqtt.cpp b/src/core/mqtt.cpp index 38342f7cb..6fecc5d3a 100644 --- a/src/core/mqtt.cpp +++ b/src/core/mqtt.cpp @@ -1236,7 +1236,10 @@ void Mqtt::add_ha_classes(JsonObject doc, const uint8_t device_type, const uint8 } } -bool Mqtt::publish_ha_climate_config(const int8_t tag, const bool has_roomtemp, const bool remove, const int16_t min, const uint32_t max) { +bool Mqtt::publish_ha_climate_config(const DeviceValue & dv, const bool has_roomtemp, const bool remove) { + int8_t tag = dv.tag; + int16_t min = dv.min; + uint32_t max = dv.max; uint8_t hc_num = tag < DeviceValueTAG::TAG_SRC1 ? tag : tag - DeviceValueTAG::TAG_SRC1 + 1; const char * tagname = tag < DeviceValueTAG::TAG_SRC1 ? "hc" : "src"; const uint8_t device_type = tag < DeviceValueTAG::TAG_SRC1 ? EMSdevice::DeviceType::THERMOSTAT : EMSdevice::DeviceType::CONNECT; @@ -1356,9 +1359,33 @@ bool Mqtt::publish_ha_climate_config(const int8_t tag, const bool has_roomtemp, // the HA climate component only responds to auto, heat and off JsonArray modes = doc["modes"].to(); - modes.add("auto"); - modes.add("heat"); - modes.add("off"); + // 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) { + 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) { + found_auto = true; + } else if (strcmp(option, "heat") == 0) { + found_heat = true; + } else if (strcmp(option, "off") == 0) { + found_off = true; + } + } + if (found_auto) { + modes.add("auto"); + } + if (found_heat) { + modes.add("heat"); + } + if (found_off) { + modes.add("off"); + } + } add_ha_dev_section(doc.as(), devicename, nullptr, nullptr, nullptr, false); // add dev section add_ha_avty_section(doc.as(), topic_t, seltemp_cond, has_roomtemp ? currtemp_cond : nullptr, hc_mode_cond); // add availability section diff --git a/src/core/mqtt.h b/src/core/mqtt.h index cf86f1ee5..60b4f2a5e 100644 --- a/src/core/mqtt.h +++ b/src/core/mqtt.h @@ -113,7 +113,7 @@ class Mqtt { const bool create_device_config = false); static bool publish_system_ha_sensor_config(uint8_t type, const char * name, const char * entity, const uint8_t uom); - static bool publish_ha_climate_config(const int8_t tag, const bool has_roomtemp, const bool remove = false, const int16_t min = 5, const uint32_t max = 30); + static bool publish_ha_climate_config(const DeviceValue & dv, const bool has_roomtemp, const bool remove = false); static void show_topic_handlers(uuid::console::Shell & shell, const uint8_t device_type); static void show_mqtt(uuid::console::Shell & shell); diff --git a/src/emsesp_version.h b/src/emsesp_version.h index 830281e32..1b5f3d375 100644 --- a/src/emsesp_version.h +++ b/src/emsesp_version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "3.7.3-dev.35" +#define EMSESP_APP_VERSION "3.7.3-dev.36"