diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 34be6e4aa2..a4c2557ffe 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -425,7 +425,7 @@ message ListEntitiesFanResponse { bool disabled_by_default = 9; string icon = 10 [(field_ifdef) = "USE_ENTITY_ICON"]; EntityCategory entity_category = 11; - repeated string supported_preset_modes = 12 [(container_pointer) = "FixedVector"]; + repeated string supported_preset_modes = 12 [(container_pointer) = "std::vector"]; uint32 device_id = 13 [(field_ifdef) = "USE_DEVICES"]; } // Deprecated in API version 1.6 - only used in deprecated fields diff --git a/esphome/components/api/api_connection.cpp b/esphome/components/api/api_connection.cpp index 05a4f9e63e..7c135946f8 100644 --- a/esphome/components/api/api_connection.cpp +++ b/esphome/components/api/api_connection.cpp @@ -401,7 +401,7 @@ uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *co bool is_single) { auto *fan = static_cast(entity); FanStateResponse msg; - const auto &traits = fan->get_traits(); + auto traits = fan->get_traits(); msg.state = fan->state; if (traits.supports_oscillation()) msg.oscillating = fan->oscillating; @@ -418,7 +418,7 @@ uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *con bool is_single) { auto *fan = static_cast(entity); ListEntitiesFanResponse msg; - const auto &traits = fan->get_traits(); + auto traits = fan->get_traits(); msg.supports_oscillation = traits.supports_oscillation(); msg.supports_speed = traits.supports_speed(); msg.supports_direction = traits.supports_direction(); diff --git a/esphome/components/api/api_pb2.h b/esphome/components/api/api_pb2.h index 647dd47b89..e71ad2c64e 100644 --- a/esphome/components/api/api_pb2.h +++ b/esphome/components/api/api_pb2.h @@ -725,7 +725,7 @@ class ListEntitiesFanResponse final : public InfoResponseProtoMessage { bool supports_speed{false}; bool supports_direction{false}; int32_t supported_speed_count{0}; - const FixedVector *supported_preset_modes{}; + const std::vector *supported_preset_modes{}; void encode(ProtoWriteBuffer buffer) const override; void calculate_size(ProtoSize &size) const override; #ifdef HAS_PROTO_MESSAGE_DUMP diff --git a/esphome/components/binary/fan/binary_fan.h b/esphome/components/binary/fan/binary_fan.h index b87e1c5d9d..16bce2e6af 100644 --- a/esphome/components/binary/fan/binary_fan.h +++ b/esphome/components/binary/fan/binary_fan.h @@ -16,7 +16,7 @@ class BinaryFan : public Component, public fan::Fan { void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; } void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; } - const fan::FanTraits &get_traits() override; + fan::FanTraits get_traits() override; protected: void control(const fan::FanCall &call) override; diff --git a/esphome/components/copy/fan/copy_fan.h b/esphome/components/copy/fan/copy_fan.h index 194827b9f8..e1212537f1 100644 --- a/esphome/components/copy/fan/copy_fan.h +++ b/esphome/components/copy/fan/copy_fan.h @@ -12,11 +12,10 @@ class CopyFan : public fan::Fan, public Component { void setup() override; void dump_config() override; - const fan::FanTraits &get_traits() override; + fan::FanTraits get_traits() override; protected: void control(const fan::FanCall &call) override; - ; fan::Fan *source_; }; diff --git a/esphome/components/demo/demo_fan.h b/esphome/components/demo/demo_fan.h index 568e90b826..09edc4e0b7 100644 --- a/esphome/components/demo/demo_fan.h +++ b/esphome/components/demo/demo_fan.h @@ -16,9 +16,8 @@ enum class DemoFanType { class DemoFan : public fan::Fan, public Component { public: void set_type(DemoFanType type) { type_ = type; } - const fan::FanTraits &get_traits() override { - // Note: Demo fan builds traits dynamically, so we store it as a member - this->traits_ = fan::FanTraits{}; + fan::FanTraits get_traits() override { + fan::FanTraits traits{}; // oscillation // speed @@ -28,22 +27,22 @@ class DemoFan : public fan::Fan, public Component { case DemoFanType::TYPE_1: break; case DemoFanType::TYPE_2: - this->traits_.set_oscillation(true); + traits.set_oscillation(true); break; case DemoFanType::TYPE_3: - this->traits_.set_direction(true); - this->traits_.set_speed(true); - this->traits_.set_supported_speed_count(5); + traits.set_direction(true); + traits.set_speed(true); + traits.set_supported_speed_count(5); break; case DemoFanType::TYPE_4: - this->traits_.set_direction(true); - this->traits_.set_speed(true); - this->traits_.set_supported_speed_count(100); - this->traits_.set_oscillation(true); + traits.set_direction(true); + traits.set_speed(true); + traits.set_supported_speed_count(100); + traits.set_oscillation(true); break; } - return this->traits_; + return traits; } protected: @@ -61,7 +60,6 @@ class DemoFan : public fan::Fan, public Component { } DemoFanType type_; - fan::FanTraits traits_; }; } // namespace demo diff --git a/esphome/components/fan/fan.cpp b/esphome/components/fan/fan.cpp index 839b0d08cc..ea9cfd0c37 100644 --- a/esphome/components/fan/fan.cpp +++ b/esphome/components/fan/fan.cpp @@ -132,7 +132,7 @@ FanCall Fan::make_call() { return FanCall(*this); } void Fan::add_on_state_callback(std::function &&callback) { this->state_callback_.add(std::move(callback)); } void Fan::publish_state() { - const auto &traits = this->get_traits(); + auto traits = this->get_traits(); ESP_LOGD(TAG, "'%s' - Sending state:", this->name_.c_str()); ESP_LOGD(TAG, " State: %s", ONOFF(this->state)); @@ -211,7 +211,7 @@ void Fan::save_state_() { } void Fan::dump_traits_(const char *tag, const char *prefix) { - const auto &traits = this->get_traits(); + auto traits = this->get_traits(); if (traits.supports_speed()) { ESP_LOGCONFIG(tag, diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index 901181903a..b74187eb4a 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -127,7 +127,7 @@ class Fan : public EntityBase { void publish_state(); - virtual const FanTraits &get_traits() = 0; + virtual FanTraits get_traits() = 0; /// Set the restore mode of this fan. void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; } diff --git a/esphome/components/fan/fan_traits.h b/esphome/components/fan/fan_traits.h index e0b64aa0fa..9e1b669a2b 100644 --- a/esphome/components/fan/fan_traits.h +++ b/esphome/components/fan/fan_traits.h @@ -1,5 +1,5 @@ #include -#include "esphome/core/helpers.h" +#include #pragma once @@ -36,18 +36,9 @@ 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 (from initializer list). - void set_supported_preset_modes(const std::initializer_list &preset_modes) { - this->preset_modes_ = preset_modes; - } - /// Set the preset modes supported by the fan (from FixedVector). - 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 std::vector &supported_preset_modes() const { return this->preset_modes_; } + /// Set the preset modes supported by the fan. + void set_supported_preset_modes(const std::vector &preset_modes) { this->preset_modes_ = preset_modes; } /// Return if preset modes are supported bool supports_preset_modes() const { return !this->preset_modes_.empty(); } @@ -59,13 +50,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 std::vector &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_{}; + std::vector preset_modes_{}; }; } // namespace fan diff --git a/esphome/components/hbridge/fan/hbridge_fan.h b/esphome/components/hbridge/fan/hbridge_fan.h index 68458d7922..8562fd20be 100644 --- a/esphome/components/hbridge/fan/hbridge_fan.h +++ b/esphome/components/hbridge/fan/hbridge_fan.h @@ -21,11 +21,11 @@ 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 std::initializer_list &presets) { preset_modes_ = presets; } + void set_preset_modes(const std::vector &presets) { preset_modes_ = presets; } void setup() override; void dump_config() override; - const fan::FanTraits &get_traits() override { return this->traits_; } + fan::FanTraits get_traits() override { return this->traits_; } fan::FanCall brake(); @@ -37,7 +37,7 @@ class HBridgeFan : public Component, public fan::Fan { int speed_count_{}; DecayMode decay_mode_{DECAY_MODE_SLOW}; fan::FanTraits traits_; - FixedVector preset_modes_{}; + std::vector preset_modes_{}; void control(const fan::FanCall &call) override; void write_state_(); diff --git a/esphome/components/speed/fan/speed_fan.h b/esphome/components/speed/fan/speed_fan.h index 60c2267b04..d994ddd15e 100644 --- a/esphome/components/speed/fan/speed_fan.h +++ b/esphome/components/speed/fan/speed_fan.h @@ -17,8 +17,8 @@ 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; } - const fan::FanTraits &get_traits() override { return this->traits_; } + void set_preset_modes(const std::vector &presets) { this->preset_modes_ = presets; } + fan::FanTraits get_traits() override { return this->traits_; } protected: void control(const fan::FanCall &call) override; @@ -29,7 +29,7 @@ class SpeedFan : public Component, public fan::Fan { output::BinaryOutput *direction_{nullptr}; int speed_count_{}; fan::FanTraits traits_; - FixedVector preset_modes_{}; + std::vector preset_modes_{}; }; } // namespace speed diff --git a/esphome/components/template/fan/template_fan.cpp b/esphome/components/template/fan/template_fan.cpp index 477e2c4981..5f4a2ae8f7 100644 --- a/esphome/components/template/fan/template_fan.cpp +++ b/esphome/components/template/fan/template_fan.cpp @@ -15,8 +15,7 @@ void TemplateFan::setup() { // Construct traits this->traits_ = fan::FanTraits(this->has_oscillating_, this->speed_count_ > 0, this->has_direction_, this->speed_count_); - if (!this->preset_modes_.empty()) - this->traits_.set_supported_preset_modes(this->preset_modes_); + this->traits_.set_supported_preset_modes(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 5b175b21a4..4a32c912fc 100644 --- a/esphome/components/template/fan/template_fan.h +++ b/esphome/components/template/fan/template_fan.h @@ -15,8 +15,8 @@ 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; } - const fan::FanTraits &get_traits() override { return this->traits_; } + void set_preset_modes(const std::vector &presets) { this->preset_modes_ = presets; } + fan::FanTraits get_traits() override { return this->traits_; } protected: void control(const fan::FanCall &call) override; @@ -25,7 +25,7 @@ class TemplateFan : public Component, public fan::Fan { bool has_direction_{false}; int speed_count_{0}; fan::FanTraits traits_; - FixedVector preset_modes_{}; + std::vector preset_modes_{}; }; } // namespace template_ diff --git a/esphome/components/tuya/fan/tuya_fan.h b/esphome/components/tuya/fan/tuya_fan.h index 100579ea9f..527efa8246 100644 --- a/esphome/components/tuya/fan/tuya_fan.h +++ b/esphome/components/tuya/fan/tuya_fan.h @@ -17,7 +17,7 @@ class TuyaFan : public Component, public fan::Fan { void set_oscillation_id(uint8_t oscillation_id) { this->oscillation_id_ = oscillation_id; } void set_direction_id(uint8_t direction_id) { this->direction_id_ = direction_id; } - const fan::FanTraits &get_traits() override; + fan::FanTraits get_traits() override; protected: void control(const fan::FanCall &call) override;