Always include preset/custom_preset in climate state JSON

When a preset is cleared, the field was omitted entirely from the JSON.
Since the frontend uses Object.assign to merge state updates, the old
preset value was never removed. Now we always send the field (empty
string when no preset is active) so the UI updates correctly.
This commit is contained in:
Ryan Wagoner
2026-02-17 22:22:22 -05:00
parent 5b5d3fa9b3
commit 38fc007a6a

View File

@@ -1568,11 +1568,16 @@ std::string WebServer::climate_json_(climate::Climate *obj, JsonDetail start_con
if (!traits.get_supported_custom_fan_modes().empty() && obj->has_custom_fan_mode()) {
root[ESPHOME_F("custom_fan_mode")] = obj->get_custom_fan_mode();
}
if (traits.get_supports_presets() && obj->preset.has_value()) {
root[ESPHOME_F("preset")] = PSTR_LOCAL(climate_preset_to_string(obj->preset.value()));
if (traits.get_supports_presets()) {
root[ESPHOME_F("preset")] =
obj->preset.has_value() ? PSTR_LOCAL(climate_preset_to_string(obj->preset.value())) : "";
}
if (!traits.get_supported_custom_presets().empty() && obj->has_custom_preset()) {
root[ESPHOME_F("custom_preset")] = obj->get_custom_preset();
if (!traits.get_supported_custom_presets().empty()) {
if (obj->has_custom_preset()) {
root[ESPHOME_F("custom_preset")] = obj->get_custom_preset();
} else {
root[ESPHOME_F("custom_preset")] = "";
}
}
if (traits.get_supports_swing_modes()) {
root[ESPHOME_F("swing_mode")] = PSTR_LOCAL(climate_swing_mode_to_string(obj->swing_mode));