diff --git a/esphome/components/climate/climate.cpp b/esphome/components/climate/climate.cpp index ff97265d9e..20df78ea1f 100644 --- a/esphome/components/climate/climate.cpp +++ b/esphome/components/climate/climate.cpp @@ -635,13 +635,33 @@ bool Climate::set_fan_mode_(ClimateFanMode mode) { } bool Climate::set_custom_fan_mode_(const std::string &mode) { - return set_alternative(this->custom_fan_mode, this->fan_mode, mode.c_str()); + auto traits = this->get_traits(); + const char *mode_ptr = traits.find_custom_fan_mode(mode.c_str()); + if (mode_ptr != nullptr) { + return set_alternative(this->custom_fan_mode, this->fan_mode, mode_ptr); + } + // Mode not found in supported custom modes, clear it + if (this->custom_fan_mode != nullptr) { + this->custom_fan_mode = nullptr; + return true; + } + return false; } bool Climate::set_preset_(ClimatePreset preset) { return set_alternative(this->preset, this->custom_preset, preset); } bool Climate::set_custom_preset_(const std::string &preset) { - return set_alternative(this->custom_preset, this->preset, preset.c_str()); + auto traits = this->get_traits(); + const char *preset_ptr = traits.find_custom_preset(preset.c_str()); + if (preset_ptr != nullptr) { + return set_alternative(this->custom_preset, this->preset, preset_ptr); + } + // Preset not found in supported custom presets, clear it + if (this->custom_preset != nullptr) { + this->custom_preset = nullptr; + return true; + } + return false; } void Climate::dump_traits_(const char *tag) {