1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-01 15:41:52 +00:00

Compare commits

...

38 Commits

Author SHA1 Message Date
J. Nick Koston
410afd196f preen 2025-10-31 11:13:57 -05:00
J. Nick Koston
4e6d74c981 Merge branch 'dev' into fan_fixed 2025-10-31 10:40:02 -05:00
J. Nick Koston
0bb6a6872d Merge branch 'dev' into fan_fixed 2025-10-28 23:47:02 -05:00
J. Nick Koston
372c162e6b make sure no dangling 2025-10-28 23:02:14 -05:00
J. Nick Koston
b635689c29 make sure no dangling 2025-10-28 23:01:28 -05:00
J. Nick Koston
e4aec7f413 make sure no dangling 2025-10-28 22:57:50 -05:00
J. Nick Koston
bb99f68d33 cleanup 2025-10-28 22:47:36 -05:00
J. Nick Koston
47cbe74453 cleanup 2025-10-28 22:41:13 -05:00
J. Nick Koston
cc815fd683 cleanup 2025-10-28 22:40:56 -05:00
J. Nick Koston
4cc41606d1 cleanup 2025-10-28 22:40:45 -05:00
J. Nick Koston
6cf0a38b86 preen 2025-10-28 22:26:27 -05:00
J. Nick Koston
5e6ce6ee48 Merge branch 'dev' into fan_fixed 2025-10-28 22:15:50 -05:00
J. Nick Koston
091c12cb48 preen 2025-10-22 13:29:14 -10:00
J. Nick Koston
39b93079e5 simp 2025-10-22 13:26:53 -10:00
J. Nick Koston
93c555ae87 reset 2025-10-22 13:18:14 -10:00
J. Nick Koston
977dd9dd34 manual copy 2025-10-22 12:29:23 -10:00
J. Nick Koston
fe6f877185 manual copy 2025-10-22 12:28:51 -10:00
J. Nick Koston
c7aef0016a manual copy 2025-10-22 12:27:29 -10:00
J. Nick Koston
c69e7f4e78 init 2025-10-22 12:25:35 -10:00
J. Nick Koston
6d1ee10742 manual copy 2025-10-22 12:24:47 -10:00
J. Nick Koston
516889f35e Merge remote-tracking branch 'origin/fan_fixed' into fan_fixed 2025-10-22 12:02:31 -10:00
J. Nick Koston
26e4754673 fixed 2025-10-22 12:02:20 -10:00
J. Nick Koston
a3b3032319 Merge branch 'dev' into fan_fixed 2025-10-22 11:56:27 -10:00
J. Nick Koston
b0f764a37e fixed 2025-10-22 11:52:15 -10:00
J. Nick Koston
5c7029623e fixed 2025-10-22 11:44:42 -10:00
J. Nick Koston
fdb23a2c13 fixed 2025-10-22 11:42:31 -10:00
J. Nick Koston
43bcd98649 fixed 2025-10-22 11:41:15 -10:00
J. Nick Koston
274c0505f7 fixed 2025-10-22 11:38:52 -10:00
J. Nick Koston
eaf0a367b4 fixed 2025-10-22 11:37:19 -10:00
J. Nick Koston
657e6f0bce fixed 2025-10-22 11:28:53 -10:00
J. Nick Koston
935acc7d5e fixed 2025-10-22 11:24:12 -10:00
J. Nick Koston
acd24402dd reduce scope 2025-10-22 11:16:28 -10:00
J. Nick Koston
ac36b97262 reduce scope 2025-10-22 11:16:13 -10:00
J. Nick Koston
828f2addcd Merge remote-tracking branch 'origin/fan_fixed' into fan_fixed 2025-10-22 11:09:23 -10:00
J. Nick Koston
f11e8e36b5 missed 2025-10-22 11:09:10 -10:00
J. Nick Koston
788c402cfe Merge branch 'fan_base_tests' into fan_fixed 2025-10-22 11:05:09 -10:00
J. Nick Koston
04d127015c Add basic fan compile tests
baseline for https://github.com/esphome/esphome/pull/11483
2025-10-22 11:04:38 -10:00
J. Nick Koston
f559fad4fc [fan] Use FixedVector for preset modes, preserve config order (breaking) 2025-10-22 11:03:32 -10:00
9 changed files with 59 additions and 55 deletions

View File

@@ -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) = "std::set"];
repeated string supported_preset_modes = 12 [(container_pointer_no_template) = "std::vector<const char *>"];
uint32 device_id = 13 [(field_ifdef) = "USE_DEVICES"];
}
// Deprecated in API version 1.6 - only used in deprecated fields

View File

@@ -423,7 +423,7 @@ uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *con
msg.supports_speed = traits.supports_speed();
msg.supports_direction = traits.supports_direction();
msg.supported_speed_count = traits.supported_speed_count();
msg.supported_preset_modes = &traits.supported_preset_modes_for_api_();
msg.supported_preset_modes = &traits.supported_preset_modes();
return fill_and_encode_entity_info(fan, msg, ListEntitiesFanResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
}
void APIConnection::fan_command(const FanCommandRequest &msg) {

View File

@@ -355,8 +355,8 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer buffer) const {
buffer.encode_string(10, this->icon_ref_);
#endif
buffer.encode_uint32(11, static_cast<uint32_t>(this->entity_category));
for (const auto &it : *this->supported_preset_modes) {
buffer.encode_string(12, it, true);
for (const char *it : *this->supported_preset_modes) {
buffer.encode_string(12, it, strlen(it), true);
}
#ifdef USE_DEVICES
buffer.encode_uint32(13, this->device_id);
@@ -376,8 +376,8 @@ void ListEntitiesFanResponse::calculate_size(ProtoSize &size) const {
#endif
size.add_uint32(1, static_cast<uint32_t>(this->entity_category));
if (!this->supported_preset_modes->empty()) {
for (const auto &it : *this->supported_preset_modes) {
size.add_length_force(1, it.size());
for (const char *it : *this->supported_preset_modes) {
size.add_length_force(1, strlen(it));
}
}
#ifdef USE_DEVICES

View File

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

View File

@@ -51,7 +51,14 @@ void FanCall::validate_() {
if (!this->preset_mode_.empty()) {
const auto &preset_modes = traits.supported_preset_modes();
if (preset_modes.find(this->preset_mode_) == preset_modes.end()) {
bool found = false;
for (const auto &mode : preset_modes) {
if (strcmp(mode, this->preset_mode_.c_str()) == 0) {
found = true;
break;
}
}
if (!found) {
ESP_LOGW(TAG, "%s: Preset mode '%s' not supported", this->parent_.get_name().c_str(), this->preset_mode_.c_str());
this->preset_mode_.clear();
}
@@ -92,11 +99,12 @@ FanCall FanRestoreState::to_call(Fan &fan) {
call.set_speed(this->speed);
call.set_direction(this->direction);
if (fan.get_traits().supports_preset_modes()) {
auto traits = fan.get_traits();
if (traits.supports_preset_modes()) {
// Use stored preset index to get preset name
const auto &preset_modes = fan.get_traits().supported_preset_modes();
const auto &preset_modes = traits.supported_preset_modes();
if (this->preset_mode < preset_modes.size()) {
call.set_preset_mode(*std::next(preset_modes.begin(), this->preset_mode));
call.set_preset_mode(preset_modes[this->preset_mode]);
}
}
return call;
@@ -107,11 +115,12 @@ void FanRestoreState::apply(Fan &fan) {
fan.speed = this->speed;
fan.direction = this->direction;
if (fan.get_traits().supports_preset_modes()) {
auto traits = fan.get_traits();
if (traits.supports_preset_modes()) {
// Use stored preset index to get preset name
const auto &preset_modes = fan.get_traits().supported_preset_modes();
const auto &preset_modes = traits.supported_preset_modes();
if (this->preset_mode < preset_modes.size()) {
fan.preset_mode = *std::next(preset_modes.begin(), this->preset_mode);
fan.preset_mode = preset_modes[this->preset_mode];
}
}
fan.publish_state();
@@ -182,18 +191,25 @@ void Fan::save_state_() {
return;
}
auto traits = this->get_traits();
FanRestoreState state{};
state.state = this->state;
state.oscillating = this->oscillating;
state.speed = this->speed;
state.direction = this->direction;
if (this->get_traits().supports_preset_modes() && !this->preset_mode.empty()) {
const auto &preset_modes = this->get_traits().supported_preset_modes();
if (traits.supports_preset_modes() && !this->preset_mode.empty()) {
const auto &preset_modes = traits.supported_preset_modes();
// Store index of current preset mode
auto preset_iterator = preset_modes.find(this->preset_mode);
if (preset_iterator != preset_modes.end())
state.preset_mode = std::distance(preset_modes.begin(), preset_iterator);
size_t i = 0;
for (const auto &mode : preset_modes) {
if (strcmp(mode, this->preset_mode.c_str()) == 0) {
state.preset_mode = i;
break;
}
i++;
}
}
this->rtc_.save(&state);
@@ -216,8 +232,8 @@ void Fan::dump_traits_(const char *tag, const char *prefix) {
}
if (traits.supports_preset_modes()) {
ESP_LOGCONFIG(tag, "%s Supported presets:", prefix);
for (const std::string &s : traits.supported_preset_modes())
ESP_LOGCONFIG(tag, "%s - %s", prefix, s.c_str());
for (const char *s : traits.supported_preset_modes())
ESP_LOGCONFIG(tag, "%s - %s", prefix, s);
}
}

View File

@@ -1,15 +1,9 @@
#include <set>
#include <utility>
#pragma once
namespace esphome {
#include <vector>
#include <initializer_list>
#ifdef USE_API
namespace api {
class APIConnection;
} // namespace api
#endif
namespace esphome {
namespace fan {
@@ -36,27 +30,27 @@ 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.
std::set<std::string> supported_preset_modes() const { return this->preset_modes_; }
/// Set the preset modes supported by the fan.
void set_supported_preset_modes(const std::set<std::string> &preset_modes) { this->preset_modes_ = preset_modes; }
const std::vector<const char *> &supported_preset_modes() const { return this->preset_modes_; }
/// Set the preset modes supported by the fan (from initializer list).
void set_supported_preset_modes(std::initializer_list<const char *> preset_modes) {
this->preset_modes_ = preset_modes;
}
/// Set the preset modes supported by the fan (from vector).
void set_supported_preset_modes(const std::vector<const char *> &preset_modes) { this->preset_modes_ = preset_modes; }
// Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
void set_supported_preset_modes(const std::vector<std::string> &preset_modes) = delete;
void set_supported_preset_modes(std::initializer_list<std::string> preset_modes) = delete;
/// Return if preset modes are supported
bool supports_preset_modes() const { return !this->preset_modes_.empty(); }
protected:
#ifdef USE_API
// The API connection is a friend class to access internal methods
friend class api::APIConnection;
// This method returns a reference to the internal preset modes set.
// 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 std::set<std::string> &supported_preset_modes_for_api_() const { return this->preset_modes_; }
#endif
bool oscillation_{false};
bool speed_{false};
bool direction_{false};
int speed_count_{};
std::set<std::string> preset_modes_{};
std::vector<const char *> preset_modes_{};
};
} // namespace fan

View File

@@ -1,7 +1,5 @@
#pragma once
#include <set>
#include "esphome/core/automation.h"
#include "esphome/components/output/binary_output.h"
#include "esphome/components/output/float_output.h"
@@ -22,7 +20,7 @@ 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::set<std::string> &presets) { preset_modes_ = presets; }
void set_preset_modes(std::initializer_list<const char *> presets) { preset_modes_ = presets; }
void setup() override;
void dump_config() override;
@@ -38,7 +36,7 @@ class HBridgeFan : public Component, public fan::Fan {
int speed_count_{};
DecayMode decay_mode_{DECAY_MODE_SLOW};
fan::FanTraits traits_;
std::set<std::string> preset_modes_{};
std::vector<const char *> preset_modes_{};
void control(const fan::FanCall &call) override;
void write_state_();

View File

@@ -1,7 +1,5 @@
#pragma once
#include <set>
#include "esphome/core/component.h"
#include "esphome/components/output/binary_output.h"
#include "esphome/components/output/float_output.h"
@@ -18,7 +16,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::set<std::string> &presets) { this->preset_modes_ = presets; }
void set_preset_modes(std::initializer_list<const char *> presets) { this->preset_modes_ = presets; }
fan::FanTraits get_traits() override { return this->traits_; }
protected:
@@ -30,7 +28,7 @@ class SpeedFan : public Component, public fan::Fan {
output::BinaryOutput *direction_{nullptr};
int speed_count_{};
fan::FanTraits traits_;
std::set<std::string> preset_modes_{};
std::vector<const char *> preset_modes_{};
};
} // namespace speed

View File

@@ -1,7 +1,5 @@
#pragma once
#include <set>
#include "esphome/core/component.h"
#include "esphome/components/fan/fan.h"
@@ -16,7 +14,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::set<std::string> &presets) { this->preset_modes_ = presets; }
void set_preset_modes(std::initializer_list<const char *> presets) { this->preset_modes_ = presets; }
fan::FanTraits get_traits() override { return this->traits_; }
protected:
@@ -26,7 +24,7 @@ class TemplateFan : public Component, public fan::Fan {
bool has_direction_{false};
int speed_count_{0};
fan::FanTraits traits_;
std::set<std::string> preset_modes_{};
std::vector<const char *> preset_modes_{};
};
} // namespace template_