diff --git a/esphome/components/copy/fan/copy_fan.cpp b/esphome/components/copy/fan/copy_fan.cpp index 15a7f5e025..e2b4c24dd8 100644 --- a/esphome/components/copy/fan/copy_fan.cpp +++ b/esphome/components/copy/fan/copy_fan.cpp @@ -35,7 +35,7 @@ fan::FanTraits CopyFan::get_traits() { traits.set_speed(base.supports_speed()); traits.set_supported_speed_count(base.supported_speed_count()); traits.set_direction(base.supports_direction()); - traits.set_supported_preset_modes(base.supported_preset_modes()); + traits.set_supported_preset_modes(&source_->preset_modes_); return traits; } diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index 9b11a214d6..b74187eb4a 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -1,6 +1,5 @@ #pragma once -#include #include "esphome/core/entity_base.h" #include "esphome/core/helpers.h" #include "esphome/core/log.h" @@ -133,20 +132,6 @@ 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; @@ -160,7 +145,6 @@ 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 50090f9621..4b0113c451 100644 --- a/esphome/components/fan/fan_traits.h +++ b/esphome/components/fan/fan_traits.h @@ -18,8 +18,12 @@ 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; + FanTraits(bool oscillation, bool speed, bool direction, int speed_count, const FixedVector *preset_modes) + : oscillation_(oscillation), + speed_(speed), + direction_(direction), + speed_count_(speed_count), + preset_modes_(preset_modes) {} /// Return if this fan supports oscillation. bool supports_oscillation() const { return this->oscillation_; } @@ -38,16 +42,11 @@ class FanTraits { /// Set whether this fan supports changing direction void set_direction(bool direction) { this->direction_ = direction; } /// Return the preset modes supported by the fan. - const FixedVector &supported_preset_modes() const { return this->preset_modes_; } - /// Set the preset modes supported by the fan. - template void set_supported_preset_modes(const T &preset_modes) { - this->preset_modes_.init(preset_modes.size()); - for (const auto &mode : preset_modes) { - this->preset_modes_.push_back(mode); - } - } + const FixedVector &supported_preset_modes() const { return *this->preset_modes_; } + /// Set the preset modes pointer (points to parent Fan's preset_modes_) + void set_supported_preset_modes(const FixedVector *preset_modes) { this->preset_modes_ = preset_modes; } /// Return if preset modes are supported - bool supports_preset_modes() const { return !this->preset_modes_.empty(); } + bool supports_preset_modes() const { return !this->preset_modes_->empty(); } protected: #ifdef USE_API @@ -57,13 +56,13 @@ class FanTraits { // It is used by the API to avoid copying data when encoding messages. // Warning: Do not use this method outside of the API connection code. // It returns a reference to internal data that can be invalidated. - const FixedVector &supported_preset_modes_for_api_() const { return this->preset_modes_; } + const FixedVector &supported_preset_modes_for_api_() const { return *this->preset_modes_; } #endif bool oscillation_{false}; bool speed_{false}; bool direction_{false}; int speed_count_{}; - FixedVector preset_modes_{}; + const FixedVector *preset_modes_{nullptr}; }; } // namespace fan diff --git a/esphome/components/hbridge/fan/hbridge_fan.cpp b/esphome/components/hbridge/fan/hbridge_fan.cpp index 56df053d57..6971e11cf6 100644 --- a/esphome/components/hbridge/fan/hbridge_fan.cpp +++ b/esphome/components/hbridge/fan/hbridge_fan.cpp @@ -36,9 +36,7 @@ void HBridgeFan::setup() { } 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; + return fan::FanTraits(this->oscillating_ != nullptr, true, true, this->speed_count_, &this->preset_modes_); } void HBridgeFan::dump_config() { diff --git a/esphome/components/hbridge/fan/hbridge_fan.h b/esphome/components/hbridge/fan/hbridge_fan.h index d8fa0f99cb..847eca6166 100644 --- a/esphome/components/hbridge/fan/hbridge_fan.h +++ b/esphome/components/hbridge/fan/hbridge_fan.h @@ -21,6 +21,8 @@ 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_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; } + void set_preset_modes(const std::initializer_list &presets) { this->preset_modes_ = presets; } void setup() override; void dump_config() override; @@ -35,6 +37,7 @@ class HBridgeFan : public Component, public fan::Fan { output::BinaryOutput *oscillating_{nullptr}; int speed_count_{}; DecayMode decay_mode_{DECAY_MODE_SLOW}; + 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 03d242178f..081588286f 100644 --- a/esphome/components/speed/fan/speed_fan.cpp +++ b/esphome/components/speed/fan/speed_fan.cpp @@ -15,9 +15,8 @@ void SpeedFan::setup() { } 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; + return fan::FanTraits(this->oscillating_ != nullptr, true, this->direction_ != nullptr, this->speed_count_, + &this->preset_modes_); } 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 f29a42190e..baf0fe30f0 100644 --- a/esphome/components/speed/fan/speed_fan.h +++ b/esphome/components/speed/fan/speed_fan.h @@ -17,6 +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 std::initializer_list &presets) { this->preset_modes_ = presets; } fan::FanTraits get_traits() override; protected: @@ -27,6 +28,7 @@ class SpeedFan : public Component, public fan::Fan { output::BinaryOutput *oscillating_{nullptr}; output::BinaryOutput *direction_{nullptr}; int speed_count_{}; + FixedVector preset_modes_{}; }; } // namespace speed diff --git a/esphome/components/template/fan/template_fan.cpp b/esphome/components/template/fan/template_fan.cpp index 39e853fdb6..94891e6a72 100644 --- a/esphome/components/template/fan/template_fan.cpp +++ b/esphome/components/template/fan/template_fan.cpp @@ -14,10 +14,8 @@ void TemplateFan::setup() { } fan::FanTraits TemplateFan::get_traits() { - auto traits = - fan::FanTraits(this->has_oscillating_, this->speed_count_ > 0, this->has_direction_, this->speed_count_); - traits.set_supported_preset_modes(this->preset_modes_); - return traits; + return fan::FanTraits(this->has_oscillating_, this->speed_count_ > 0, this->has_direction_, this->speed_count_, + &this->preset_modes_); } 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 561c2de756..affb313a2e 100644 --- a/esphome/components/template/fan/template_fan.h +++ b/esphome/components/template/fan/template_fan.h @@ -15,6 +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 std::initializer_list &presets) { this->preset_modes_ = presets; } fan::FanTraits get_traits() override; protected: @@ -23,6 +24,7 @@ class TemplateFan : public Component, public fan::Fan { bool has_oscillating_{false}; bool has_direction_{false}; int speed_count_{0}; + FixedVector preset_modes_{}; }; } // namespace template_