1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-31 23:21:54 +00:00
This commit is contained in:
J. Nick Koston
2025-10-30 20:16:36 -05:00
parent 60a303adb8
commit d1bb5c4d79

View File

@@ -593,76 +593,65 @@ void ClimateDeviceRestoreState::apply(Climate *climate) {
climate->publish_state(); climate->publish_state();
} }
bool Climate::set_fan_mode_(ClimateFanMode mode) { // Template helper for setting primary modes with mutual exclusion
// Clear the custom fan mode (mutual exclusion) // Clears custom pointer and sets primary optional value
bool changed = this->custom_fan_mode_ != nullptr; template<typename T> bool set_primary_mode_(optional<T> &primary, const char *&custom_ptr, T value) {
this->custom_fan_mode_ = nullptr; // Clear the custom mode (mutual exclusion)
// Set the primary fan mode bool changed = custom_ptr != nullptr;
if (changed || !this->fan_mode.has_value() || this->fan_mode.value() != mode) { custom_ptr = nullptr;
this->fan_mode = mode; // Set the primary mode
if (changed || !primary.has_value() || primary.value() != value) {
primary = value;
return true; return true;
} }
return false; return false;
} }
bool Climate::set_custom_fan_mode_(const char *mode) { // Template helper for setting custom modes with mutual exclusion
auto traits = this->get_traits(); // Takes pre-computed values: the found pointer from traits and whether custom mode is currently set
const char *mode_ptr = traits.find_custom_fan_mode_(mode); template<typename T>
if (mode_ptr != nullptr) { bool set_custom_mode_(const char *&custom_ptr, optional<T> &primary, const char *found_ptr, bool has_custom) {
// Clear the primary fan mode (mutual exclusion) if (found_ptr != nullptr) {
bool changed = this->fan_mode.has_value(); // Clear the primary mode (mutual exclusion)
this->fan_mode.reset(); bool changed = primary.has_value();
// Set the custom fan mode primary.reset();
if (changed || this->custom_fan_mode_ != mode_ptr) { // Set the custom mode
this->custom_fan_mode_ = mode_ptr; if (changed || custom_ptr != found_ptr) {
custom_ptr = found_ptr;
return true; return true;
} }
return false; return false;
} }
// Mode not found in supported custom modes, clear it if currently set // Mode not found in supported modes, clear it if currently set
if (this->has_custom_fan_mode()) { if (has_custom) {
this->clear_custom_fan_mode_(); custom_ptr = nullptr;
return true; return true;
} }
return false; return false;
} }
bool Climate::set_fan_mode_(ClimateFanMode mode) {
return set_primary_mode_(this->fan_mode, this->custom_fan_mode_, mode);
}
bool Climate::set_custom_fan_mode_(const char *mode) {
auto traits = this->get_traits();
return set_custom_mode_<ClimateFanMode>(this->custom_fan_mode_, this->fan_mode, traits.find_custom_fan_mode_(mode),
this->has_custom_fan_mode());
}
bool Climate::set_custom_fan_mode_(const std::string &mode) { return this->set_custom_fan_mode_(mode.c_str()); } bool Climate::set_custom_fan_mode_(const std::string &mode) { return this->set_custom_fan_mode_(mode.c_str()); }
void Climate::clear_custom_fan_mode_() { this->custom_fan_mode_ = nullptr; } void Climate::clear_custom_fan_mode_() { this->custom_fan_mode_ = nullptr; }
bool Climate::set_preset_(ClimatePreset preset) { bool Climate::set_preset_(ClimatePreset preset) {
// Clear the custom preset (mutual exclusion) return set_primary_mode_(this->preset, this->custom_preset_, preset);
bool changed = this->custom_preset_ != nullptr;
this->custom_preset_ = nullptr;
// Set the primary preset
if (changed || !this->preset.has_value() || this->preset.value() != preset) {
this->preset = preset;
return true;
}
return false;
} }
bool Climate::set_custom_preset_(const char *preset) { bool Climate::set_custom_preset_(const char *preset) {
auto traits = this->get_traits(); auto traits = this->get_traits();
const char *preset_ptr = traits.find_custom_preset_(preset); return set_custom_mode_<ClimatePreset>(this->custom_preset_, this->preset, traits.find_custom_preset_(preset),
if (preset_ptr != nullptr) { this->has_custom_preset());
// Clear the primary preset (mutual exclusion)
bool changed = this->preset.has_value();
this->preset.reset();
// Set the custom preset
if (changed || this->custom_preset_ != preset_ptr) {
this->custom_preset_ = preset_ptr;
return true;
}
return false;
}
// Preset not found in supported custom presets, clear it if currently set
if (this->has_custom_preset()) {
this->clear_custom_preset_();
return true;
}
return false;
} }
bool Climate::set_custom_preset_(const std::string &preset) { return this->set_custom_preset_(preset.c_str()); } bool Climate::set_custom_preset_(const std::string &preset) { return this->set_custom_preset_(preset.c_str()); }