This commit is contained in:
J. Nick Koston
2025-10-22 13:29:14 -10:00
parent 39b93079e5
commit 091c12cb48
+16 -5
View File
@@ -1,6 +1,5 @@
#include "fan.h"
#include "esphome/core/log.h"
#include <algorithm>
namespace esphome {
namespace fan {
@@ -52,7 +51,14 @@ void FanCall::validate_() {
if (!this->preset_mode_.empty()) {
const auto &preset_modes = traits.supported_preset_modes();
if (std::find(preset_modes.begin(), preset_modes.end(), this->preset_mode_) == preset_modes.end()) {
bool found = false;
for (const auto &mode : preset_modes) {
if (mode == this->preset_mode_) {
found = true;
break;
}
}
if (!found) {
ESP_LOGW(TAG, "%s: Preset mode '%s' not supported", this->parent_.get_name().c_str(), this->preset_mode_.c_str());
this->preset_mode_.clear();
}
@@ -192,9 +198,14 @@ void Fan::save_state_() {
if (this->get_traits().supports_preset_modes() && !this->preset_mode.empty()) {
const auto &preset_modes = this->get_traits().supported_preset_modes();
// Store index of current preset mode
auto preset_iterator = std::find(preset_modes.begin(), preset_modes.end(), this->preset_mode);
if (preset_iterator != preset_modes.end())
state.preset_mode = std::distance(preset_modes.begin(), preset_iterator);
size_t i = 0;
for (const auto &mode : preset_modes) {
if (mode == this->preset_mode) {
state.preset_mode = i;
break;
}
i++;
}
}
this->rtc_.save(&state);