From c3c1ae8e7f75d721fdcd387090f18b2d655918d7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 30 Oct 2025 18:44:28 -0500 Subject: [PATCH] simplify --- esphome/components/climate/climate.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/esphome/components/climate/climate.cpp b/esphome/components/climate/climate.cpp index 275db1d423..f0c466203f 100644 --- a/esphome/components/climate/climate.cpp +++ b/esphome/components/climate/climate.cpp @@ -619,18 +619,40 @@ template bool set_alternative(optional &dst, optio return is_changed; } +// Overload for optional + const char* pointer +template bool set_alternative(optional &dst, const char *&alt, const T &src) { + bool is_changed = (alt != nullptr); + alt = nullptr; + if (is_changed || dst != src) { + dst = src; + is_changed = true; + } + return is_changed; +} + +// Overload for const char* pointer + optional +template bool set_alternative(const char *&dst, optional &alt, const char *src) { + bool is_changed = alt.has_value(); + alt.reset(); + if (is_changed || dst != src) { + dst = src; + is_changed = true; + } + return is_changed; +} + bool Climate::set_fan_mode_(ClimateFanMode mode) { return set_alternative(this->fan_mode, this->custom_fan_mode, mode); } bool Climate::set_custom_fan_mode_(const std::string &mode) { - return set_alternative(this->custom_fan_mode, this->fan_mode, mode); + return set_alternative(this->custom_fan_mode, this->fan_mode, mode.c_str()); } 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); + return set_alternative(this->custom_preset, this->preset, preset.c_str()); } void Climate::dump_traits_(const char *tag) {