From 657e6f0bce67b70dc6c6567bf63c82e526c2ba4d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 22 Oct 2025 11:28:53 -1000 Subject: [PATCH] fixed --- esphome/components/fan/fan.cpp | 2 +- esphome/components/fan/fan.h | 16 ++++++++++++++++ esphome/components/fan/fan_traits.h | 2 ++ esphome/components/hbridge/fan/hbridge_fan.cpp | 8 +++++--- esphome/components/hbridge/fan/hbridge_fan.h | 5 +---- esphome/components/speed/fan/speed_fan.cpp | 8 +++++--- esphome/components/speed/fan/speed_fan.h | 5 +---- esphome/components/template/fan/template_fan.cpp | 8 +++++--- esphome/components/template/fan/template_fan.h | 5 +---- 9 files changed, 37 insertions(+), 22 deletions(-) diff --git a/esphome/components/fan/fan.cpp b/esphome/components/fan/fan.cpp index ea9cfd0c37..26a61de0b1 100644 --- a/esphome/components/fan/fan.cpp +++ b/esphome/components/fan/fan.cpp @@ -39,7 +39,7 @@ void FanCall::perform() { } void FanCall::validate_() { - const auto &traits = this->parent_.get_traits(); + auto traits = this->parent_.get_traits(); if (this->speed_.has_value()) { this->speed_ = clamp(*this->speed_, 1, traits.supported_speed_count()); diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index b74187eb4a..9b11a214d6 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -1,5 +1,6 @@ #pragma once +#include #include "esphome/core/entity_base.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" @@ -132,6 +133,20 @@ class Fan : public EntityBase { /// Set the restore mode of this fan. void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; } + /// Set preset modes - helper for components + void set_preset_modes(const std::initializer_list &presets) { + this->preset_modes_.init(presets.size()); + for (const auto &mode : presets) { + this->preset_modes_.push_back(mode); + } + } + template void set_preset_modes(const T &presets) { + this->preset_modes_.init(presets.size()); + for (const auto &mode : presets) { + this->preset_modes_.push_back(mode); + } + } + protected: friend FanCall; @@ -145,6 +160,7 @@ class Fan : public EntityBase { CallbackManager state_callback_{}; ESPPreferenceObject rtc_; FanRestoreMode restore_mode_; + FixedVector preset_modes_{}; }; } // namespace fan diff --git a/esphome/components/fan/fan_traits.h b/esphome/components/fan/fan_traits.h index c37acfa67d..50090f9621 100644 --- a/esphome/components/fan/fan_traits.h +++ b/esphome/components/fan/fan_traits.h @@ -18,6 +18,8 @@ class FanTraits { FanTraits() = default; FanTraits(bool oscillation, bool speed, bool direction, int speed_count) : oscillation_(oscillation), speed_(speed), direction_(direction), speed_count_(speed_count) {} + FanTraits(FanTraits &&) = default; + FanTraits &operator=(FanTraits &&) = default; /// Return if this fan supports oscillation. bool supports_oscillation() const { return this->oscillation_; } diff --git a/esphome/components/hbridge/fan/hbridge_fan.cpp b/esphome/components/hbridge/fan/hbridge_fan.cpp index 605a9d4ef3..56df053d57 100644 --- a/esphome/components/hbridge/fan/hbridge_fan.cpp +++ b/esphome/components/hbridge/fan/hbridge_fan.cpp @@ -33,10 +33,12 @@ void HBridgeFan::setup() { restore->apply(*this); this->write_state_(); } +} - // Construct traits - this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_); - this->traits_.set_supported_preset_modes(this->preset_modes_); +fan::FanTraits HBridgeFan::get_traits() { + auto traits = fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_); + traits.set_supported_preset_modes(this->preset_modes_); + return traits; } void HBridgeFan::dump_config() { diff --git a/esphome/components/hbridge/fan/hbridge_fan.h b/esphome/components/hbridge/fan/hbridge_fan.h index cea4f81fe5..d8fa0f99cb 100644 --- a/esphome/components/hbridge/fan/hbridge_fan.h +++ b/esphome/components/hbridge/fan/hbridge_fan.h @@ -21,11 +21,10 @@ class HBridgeFan : public Component, public fan::Fan { void set_pin_a(output::FloatOutput *pin_a) { pin_a_ = pin_a; } void set_pin_b(output::FloatOutput *pin_b) { pin_b_ = pin_b; } void set_enable_pin(output::FloatOutput *enable) { enable_ = enable; } - void set_preset_modes(const FixedVector &presets) { preset_modes_ = presets; } void setup() override; void dump_config() override; - fan::FanTraits get_traits() override { return this->traits_; } + fan::FanTraits get_traits() override; fan::FanCall brake(); @@ -36,8 +35,6 @@ class HBridgeFan : public Component, public fan::Fan { output::BinaryOutput *oscillating_{nullptr}; int speed_count_{}; DecayMode decay_mode_{DECAY_MODE_SLOW}; - fan::FanTraits traits_; - FixedVector preset_modes_{}; void control(const fan::FanCall &call) override; void write_state_(); diff --git a/esphome/components/speed/fan/speed_fan.cpp b/esphome/components/speed/fan/speed_fan.cpp index 57bd795416..03d242178f 100644 --- a/esphome/components/speed/fan/speed_fan.cpp +++ b/esphome/components/speed/fan/speed_fan.cpp @@ -12,10 +12,12 @@ void SpeedFan::setup() { restore->apply(*this); this->write_state_(); } +} - // Construct traits - this->traits_ = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_); - this->traits_.set_supported_preset_modes(this->preset_modes_); +fan::FanTraits SpeedFan::get_traits() { + auto traits = fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_); + traits.set_supported_preset_modes(this->preset_modes_); + return traits; } void SpeedFan::dump_config() { LOG_FAN("", "Speed Fan", this); } diff --git a/esphome/components/speed/fan/speed_fan.h b/esphome/components/speed/fan/speed_fan.h index 3ffffac231..f29a42190e 100644 --- a/esphome/components/speed/fan/speed_fan.h +++ b/esphome/components/speed/fan/speed_fan.h @@ -17,8 +17,7 @@ class SpeedFan : public Component, public fan::Fan { void set_output(output::FloatOutput *output) { this->output_ = output; } void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; } void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; } - void set_preset_modes(const FixedVector &presets) { this->preset_modes_ = presets; } - fan::FanTraits get_traits() override { return this->traits_; } + fan::FanTraits get_traits() override; protected: void control(const fan::FanCall &call) override; @@ -28,8 +27,6 @@ class SpeedFan : public Component, public fan::Fan { output::BinaryOutput *oscillating_{nullptr}; output::BinaryOutput *direction_{nullptr}; int speed_count_{}; - fan::FanTraits traits_; - FixedVector preset_modes_{}; }; } // namespace speed diff --git a/esphome/components/template/fan/template_fan.cpp b/esphome/components/template/fan/template_fan.cpp index 5f4a2ae8f7..39e853fdb6 100644 --- a/esphome/components/template/fan/template_fan.cpp +++ b/esphome/components/template/fan/template_fan.cpp @@ -11,11 +11,13 @@ void TemplateFan::setup() { if (restore.has_value()) { restore->apply(*this); } +} - // Construct traits - this->traits_ = +fan::FanTraits TemplateFan::get_traits() { + auto traits = fan::FanTraits(this->has_oscillating_, this->speed_count_ > 0, this->has_direction_, this->speed_count_); - this->traits_.set_supported_preset_modes(this->preset_modes_); + traits.set_supported_preset_modes(this->preset_modes_); + return traits; } void TemplateFan::dump_config() { LOG_FAN("", "Template Fan", this); } diff --git a/esphome/components/template/fan/template_fan.h b/esphome/components/template/fan/template_fan.h index 330f8f2565..561c2de756 100644 --- a/esphome/components/template/fan/template_fan.h +++ b/esphome/components/template/fan/template_fan.h @@ -15,8 +15,7 @@ class TemplateFan : public Component, public fan::Fan { void set_has_direction(bool has_direction) { this->has_direction_ = has_direction; } void set_has_oscillating(bool has_oscillating) { this->has_oscillating_ = has_oscillating; } void set_speed_count(int count) { this->speed_count_ = count; } - void set_preset_modes(const FixedVector &presets) { this->preset_modes_ = presets; } - fan::FanTraits get_traits() override { return this->traits_; } + fan::FanTraits get_traits() override; protected: void control(const fan::FanCall &call) override; @@ -24,8 +23,6 @@ class TemplateFan : public Component, public fan::Fan { bool has_oscillating_{false}; bool has_direction_{false}; int speed_count_{0}; - fan::FanTraits traits_; - FixedVector preset_modes_{}; }; } // namespace template_