diff --git a/esphome/components/copy/fan/copy_fan.cpp b/esphome/components/copy/fan/copy_fan.cpp index c1e873e083..d35ece950b 100644 --- a/esphome/components/copy/fan/copy_fan.cpp +++ b/esphome/components/copy/fan/copy_fan.cpp @@ -12,11 +12,7 @@ void CopyFan::setup() { this->oscillating = source_->oscillating; this->speed = source_->speed; this->direction = source_->direction; - const char *preset = source_->get_preset_mode(); - if (preset != nullptr) - this->set_preset_mode_(preset); - else - this->clear_preset_mode_(); + this->set_preset_mode_(source_->get_preset_mode()); this->publish_state(); }); @@ -24,11 +20,7 @@ void CopyFan::setup() { this->oscillating = source_->oscillating; this->speed = source_->speed; this->direction = source_->direction; - const char *preset = source_->get_preset_mode(); - if (preset != nullptr) - this->set_preset_mode_(preset); - else - this->clear_preset_mode_(); + this->set_preset_mode_(source_->get_preset_mode()); this->publish_state(); } diff --git a/esphome/components/fan/fan.cpp b/esphome/components/fan/fan.cpp index eb2e8743f3..c4abab0b4a 100644 --- a/esphome/components/fan/fan.cpp +++ b/esphome/components/fan/fan.cpp @@ -138,6 +138,14 @@ FanCall Fan::make_call() { return FanCall(*this); } const char *Fan::find_preset_mode_(const char *preset_mode) { return this->get_traits().find_preset_mode(preset_mode); } bool Fan::set_preset_mode_(const char *preset_mode) { + if (preset_mode == nullptr) { + // Treat nullptr as clearing the preset mode + if (this->preset_mode_ == nullptr) { + return false; // No change + } + this->clear_preset_mode_(); + return true; + } const char *validated = this->find_preset_mode_(preset_mode); if (validated == nullptr || this->preset_mode_ == validated) { return false; // Preset mode not supported or no change