From 13148f2c893acf2dca62d2e5bd35b4d93e9fd064 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 30 Oct 2025 19:47:45 -0500 Subject: [PATCH] simplify --- esphome/components/api/api_connection.cpp | 4 ++-- esphome/components/climate/climate.h | 6 ++++++ esphome/components/web_server/web_server.cpp | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 5a33a82842..2914f15b4d 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -637,13 +637,13 @@ uint16_t APIConnection::try_send_climate_state(EntityBase *entity, APIConnection } if (traits.get_supports_fan_modes() && climate->fan_mode.has_value()) resp.fan_mode = static_cast(climate->fan_mode.value()); - if (!traits.get_supported_custom_fan_modes().empty() && climate->custom_fan_mode != nullptr) { + if (!traits.get_supported_custom_fan_modes().empty() && climate->has_custom_fan_mode()) { resp.set_custom_fan_mode(StringRef(climate->custom_fan_mode)); } if (traits.get_supports_presets() && climate->preset.has_value()) { resp.preset = static_cast(climate->preset.value()); } - if (!traits.get_supported_custom_presets().empty() && climate->custom_preset != nullptr) { + if (!traits.get_supported_custom_presets().empty() && climate->has_custom_preset()) { resp.set_custom_preset(StringRef(climate->custom_preset)); } if (traits.get_supports_swing_modes()) diff --git a/esphome/components/climate/climate.h b/esphome/components/climate/climate.h index ea94128f5c..5166e19319 100644 --- a/esphome/components/climate/climate.h +++ b/esphome/components/climate/climate.h @@ -216,6 +216,12 @@ class Climate : public EntityBase { void set_visual_min_humidity_override(float visual_min_humidity_override); void set_visual_max_humidity_override(float visual_max_humidity_override); + /// Check if a custom fan mode is currently active. + bool has_custom_fan_mode() const { return this->custom_fan_mode != nullptr; } + + /// Check if a custom preset is currently active. + bool has_custom_preset() const { return this->custom_preset != nullptr; } + /// The current temperature of the climate device, as reported from the integration. float current_temperature{NAN}; diff --git a/esphome/components/web_server/web_server.cpp b/esphome/components/web_server/web_server.cpp index ee626b8b9b..7901869b2f 100644 --- a/esphome/components/web_server/web_server.cpp +++ b/esphome/components/web_server/web_server.cpp @@ -1312,7 +1312,7 @@ std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_conf for (climate::ClimatePreset m : traits.get_supported_presets()) opt.add(PSTR_LOCAL(climate::climate_preset_to_string(m))); } - if (!traits.get_supported_custom_presets().empty() && obj->custom_preset != nullptr) { + if (!traits.get_supported_custom_presets().empty() && obj->has_custom_preset()) { JsonArray opt = root["custom_presets"].to(); for (auto const &custom_preset : traits.get_supported_custom_presets()) opt.add(custom_preset); @@ -1333,13 +1333,13 @@ std::string WebServer::climate_json(climate::Climate *obj, JsonDetail start_conf if (traits.get_supports_fan_modes() && obj->fan_mode.has_value()) { root["fan_mode"] = PSTR_LOCAL(climate_fan_mode_to_string(obj->fan_mode.value())); } - if (!traits.get_supported_custom_fan_modes().empty() && obj->custom_fan_mode != nullptr) { + if (!traits.get_supported_custom_fan_modes().empty() && obj->has_custom_fan_mode()) { root["custom_fan_mode"] = obj->custom_fan_mode; } if (traits.get_supports_presets() && obj->preset.has_value()) { root["preset"] = PSTR_LOCAL(climate_preset_to_string(obj->preset.value())); } - if (!traits.get_supported_custom_presets().empty() && obj->custom_preset != nullptr) { + if (!traits.get_supported_custom_presets().empty() && obj->has_custom_preset()) { root["custom_preset"] = obj->custom_preset; } if (traits.get_supports_swing_modes()) {