1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 06:04:01 +00:00

reduce scope

This commit is contained in:
J. Nick Koston
2025-10-22 11:16:13 -10:00
parent 828f2addcd
commit ac36b97262
14 changed files with 37 additions and 50 deletions

View File

@@ -425,7 +425,7 @@ message ListEntitiesFanResponse {
bool disabled_by_default = 9; bool disabled_by_default = 9;
string icon = 10 [(field_ifdef) = "USE_ENTITY_ICON"]; string icon = 10 [(field_ifdef) = "USE_ENTITY_ICON"];
EntityCategory entity_category = 11; 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"]; uint32 device_id = 13 [(field_ifdef) = "USE_DEVICES"];
} }
// Deprecated in API version 1.6 - only used in deprecated fields // Deprecated in API version 1.6 - only used in deprecated fields

View File

@@ -401,7 +401,7 @@ uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *co
bool is_single) { bool is_single) {
auto *fan = static_cast<fan::Fan *>(entity); auto *fan = static_cast<fan::Fan *>(entity);
FanStateResponse msg; FanStateResponse msg;
const auto &traits = fan->get_traits(); auto traits = fan->get_traits();
msg.state = fan->state; msg.state = fan->state;
if (traits.supports_oscillation()) if (traits.supports_oscillation())
msg.oscillating = fan->oscillating; msg.oscillating = fan->oscillating;
@@ -418,7 +418,7 @@ uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *con
bool is_single) { bool is_single) {
auto *fan = static_cast<fan::Fan *>(entity); auto *fan = static_cast<fan::Fan *>(entity);
ListEntitiesFanResponse msg; ListEntitiesFanResponse msg;
const auto &traits = fan->get_traits(); auto traits = fan->get_traits();
msg.supports_oscillation = traits.supports_oscillation(); msg.supports_oscillation = traits.supports_oscillation();
msg.supports_speed = traits.supports_speed(); msg.supports_speed = traits.supports_speed();
msg.supports_direction = traits.supports_direction(); msg.supports_direction = traits.supports_direction();

View File

@@ -725,7 +725,7 @@ class ListEntitiesFanResponse final : public InfoResponseProtoMessage {
bool supports_speed{false}; bool supports_speed{false};
bool supports_direction{false}; bool supports_direction{false};
int32_t supported_speed_count{0}; int32_t supported_speed_count{0};
const FixedVector<std::string> *supported_preset_modes{}; const std::vector<std::string> *supported_preset_modes{};
void encode(ProtoWriteBuffer buffer) const override; void encode(ProtoWriteBuffer buffer) const override;
void calculate_size(ProtoSize &size) const override; void calculate_size(ProtoSize &size) const override;
#ifdef HAS_PROTO_MESSAGE_DUMP #ifdef HAS_PROTO_MESSAGE_DUMP

View File

@@ -16,7 +16,7 @@ class BinaryFan : public Component, public fan::Fan {
void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; } void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; }
void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; } void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; }
const fan::FanTraits &get_traits() override; fan::FanTraits get_traits() override;
protected: protected:
void control(const fan::FanCall &call) override; void control(const fan::FanCall &call) override;

View File

@@ -12,11 +12,10 @@ class CopyFan : public fan::Fan, public Component {
void setup() override; void setup() override;
void dump_config() override; void dump_config() override;
const fan::FanTraits &get_traits() override; fan::FanTraits get_traits() override;
protected: protected:
void control(const fan::FanCall &call) override; void control(const fan::FanCall &call) override;
;
fan::Fan *source_; fan::Fan *source_;
}; };

View File

@@ -16,9 +16,8 @@ enum class DemoFanType {
class DemoFan : public fan::Fan, public Component { class DemoFan : public fan::Fan, public Component {
public: public:
void set_type(DemoFanType type) { type_ = type; } void set_type(DemoFanType type) { type_ = type; }
const fan::FanTraits &get_traits() override { fan::FanTraits get_traits() override {
// Note: Demo fan builds traits dynamically, so we store it as a member fan::FanTraits traits{};
this->traits_ = fan::FanTraits{};
// oscillation // oscillation
// speed // speed
@@ -28,22 +27,22 @@ class DemoFan : public fan::Fan, public Component {
case DemoFanType::TYPE_1: case DemoFanType::TYPE_1:
break; break;
case DemoFanType::TYPE_2: case DemoFanType::TYPE_2:
this->traits_.set_oscillation(true); traits.set_oscillation(true);
break; break;
case DemoFanType::TYPE_3: case DemoFanType::TYPE_3:
this->traits_.set_direction(true); traits.set_direction(true);
this->traits_.set_speed(true); traits.set_speed(true);
this->traits_.set_supported_speed_count(5); traits.set_supported_speed_count(5);
break; break;
case DemoFanType::TYPE_4: case DemoFanType::TYPE_4:
this->traits_.set_direction(true); traits.set_direction(true);
this->traits_.set_speed(true); traits.set_speed(true);
this->traits_.set_supported_speed_count(100); traits.set_supported_speed_count(100);
this->traits_.set_oscillation(true); traits.set_oscillation(true);
break; break;
} }
return this->traits_; return traits;
} }
protected: protected:
@@ -61,7 +60,6 @@ class DemoFan : public fan::Fan, public Component {
} }
DemoFanType type_; DemoFanType type_;
fan::FanTraits traits_;
}; };
} // namespace demo } // namespace demo

View File

@@ -132,7 +132,7 @@ FanCall Fan::make_call() { return FanCall(*this); }
void Fan::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); } void Fan::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
void Fan::publish_state() { 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, "'%s' - Sending state:", this->name_.c_str());
ESP_LOGD(TAG, " State: %s", ONOFF(this->state)); 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) { 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()) { if (traits.supports_speed()) {
ESP_LOGCONFIG(tag, ESP_LOGCONFIG(tag,

View File

@@ -127,7 +127,7 @@ class Fan : public EntityBase {
void publish_state(); void publish_state();
virtual const FanTraits &get_traits() = 0; virtual FanTraits get_traits() = 0;
/// Set the restore mode of this fan. /// Set the restore mode of this fan.
void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; } void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }

View File

@@ -1,5 +1,5 @@
#include <utility> #include <utility>
#include "esphome/core/helpers.h" #include <vector>
#pragma once #pragma once
@@ -36,18 +36,9 @@ class FanTraits {
/// Set whether this fan supports changing direction /// Set whether this fan supports changing direction
void set_direction(bool direction) { this->direction_ = direction; } void set_direction(bool direction) { this->direction_ = direction; }
/// Return the preset modes supported by the fan. /// Return the preset modes supported by the fan.
const FixedVector<std::string> &supported_preset_modes() const { return this->preset_modes_; } const std::vector<std::string> &supported_preset_modes() const { return this->preset_modes_; }
/// Set the preset modes supported by the fan (from initializer list). /// Set the preset modes supported by the fan.
void set_supported_preset_modes(const std::initializer_list<std::string> &preset_modes) { void set_supported_preset_modes(const std::vector<std::string> &preset_modes) { this->preset_modes_ = preset_modes; }
this->preset_modes_ = preset_modes;
}
/// Set the preset modes supported by the fan (from FixedVector).
template<typename T> 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);
}
}
/// Return if preset modes are supported /// 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(); }
@@ -59,13 +50,13 @@ class FanTraits {
// It is used by the API to avoid copying data when encoding messages. // 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. // Warning: Do not use this method outside of the API connection code.
// It returns a reference to internal data that can be invalidated. // It returns a reference to internal data that can be invalidated.
const FixedVector<std::string> &supported_preset_modes_for_api_() const { return this->preset_modes_; } const std::vector<std::string> &supported_preset_modes_for_api_() const { return this->preset_modes_; }
#endif #endif
bool oscillation_{false}; bool oscillation_{false};
bool speed_{false}; bool speed_{false};
bool direction_{false}; bool direction_{false};
int speed_count_{}; int speed_count_{};
FixedVector<std::string> preset_modes_{}; std::vector<std::string> preset_modes_{};
}; };
} // namespace fan } // namespace fan

View File

@@ -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_a(output::FloatOutput *pin_a) { pin_a_ = pin_a; }
void set_pin_b(output::FloatOutput *pin_b) { pin_b_ = pin_b; } void set_pin_b(output::FloatOutput *pin_b) { pin_b_ = pin_b; }
void set_enable_pin(output::FloatOutput *enable) { enable_ = enable; } void set_enable_pin(output::FloatOutput *enable) { enable_ = enable; }
void set_preset_modes(const std::initializer_list<std::string> &presets) { preset_modes_ = presets; } void set_preset_modes(const std::vector<std::string> &presets) { preset_modes_ = presets; }
void setup() override; void setup() override;
void dump_config() 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(); fan::FanCall brake();
@@ -37,7 +37,7 @@ class HBridgeFan : public Component, public fan::Fan {
int speed_count_{}; int speed_count_{};
DecayMode decay_mode_{DECAY_MODE_SLOW}; DecayMode decay_mode_{DECAY_MODE_SLOW};
fan::FanTraits traits_; fan::FanTraits traits_;
FixedVector<std::string> preset_modes_{}; std::vector<std::string> preset_modes_{};
void control(const fan::FanCall &call) override; void control(const fan::FanCall &call) override;
void write_state_(); void write_state_();

View File

@@ -17,8 +17,8 @@ class SpeedFan : public Component, public fan::Fan {
void set_output(output::FloatOutput *output) { this->output_ = output; } void set_output(output::FloatOutput *output) { this->output_ = output; }
void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; } void set_oscillating(output::BinaryOutput *oscillating) { this->oscillating_ = oscillating; }
void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; } void set_direction(output::BinaryOutput *direction) { this->direction_ = direction; }
void set_preset_modes(const std::initializer_list<std::string> &presets) { this->preset_modes_ = presets; } void set_preset_modes(const std::vector<std::string> &presets) { this->preset_modes_ = presets; }
const fan::FanTraits &get_traits() override { return this->traits_; } fan::FanTraits get_traits() override { return this->traits_; }
protected: protected:
void control(const fan::FanCall &call) override; void control(const fan::FanCall &call) override;
@@ -29,7 +29,7 @@ class SpeedFan : public Component, public fan::Fan {
output::BinaryOutput *direction_{nullptr}; output::BinaryOutput *direction_{nullptr};
int speed_count_{}; int speed_count_{};
fan::FanTraits traits_; fan::FanTraits traits_;
FixedVector<std::string> preset_modes_{}; std::vector<std::string> preset_modes_{};
}; };
} // namespace speed } // namespace speed

View File

@@ -15,8 +15,7 @@ void TemplateFan::setup() {
// Construct traits // Construct traits
this->traits_ = this->traits_ =
fan::FanTraits(this->has_oscillating_, this->speed_count_ > 0, this->has_direction_, this->speed_count_); 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); } void TemplateFan::dump_config() { LOG_FAN("", "Template Fan", this); }

View File

@@ -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_direction(bool has_direction) { this->has_direction_ = has_direction; }
void set_has_oscillating(bool has_oscillating) { this->has_oscillating_ = has_oscillating; } void set_has_oscillating(bool has_oscillating) { this->has_oscillating_ = has_oscillating; }
void set_speed_count(int count) { this->speed_count_ = count; } void set_speed_count(int count) { this->speed_count_ = count; }
void set_preset_modes(const std::initializer_list<std::string> &presets) { this->preset_modes_ = presets; } void set_preset_modes(const std::vector<std::string> &presets) { this->preset_modes_ = presets; }
const fan::FanTraits &get_traits() override { return this->traits_; } fan::FanTraits get_traits() override { return this->traits_; }
protected: protected:
void control(const fan::FanCall &call) override; void control(const fan::FanCall &call) override;
@@ -25,7 +25,7 @@ class TemplateFan : public Component, public fan::Fan {
bool has_direction_{false}; bool has_direction_{false};
int speed_count_{0}; int speed_count_{0};
fan::FanTraits traits_; fan::FanTraits traits_;
FixedVector<std::string> preset_modes_{}; std::vector<std::string> preset_modes_{};
}; };
} // namespace template_ } // namespace template_

View File

@@ -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_oscillation_id(uint8_t oscillation_id) { this->oscillation_id_ = oscillation_id; }
void set_direction_id(uint8_t direction_id) { this->direction_id_ = direction_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: protected:
void control(const fan::FanCall &call) override; void control(const fan::FanCall &call) override;