mirror of
https://github.com/esphome/esphome.git
synced 2026-07-11 01:15:33 +00:00
Move preset modes pointer to private, consolidate empty vector to cpp
- Move supported_preset_modes_ pointer and ensure helper to private - Move static EMPTY_VECTOR from inline header getter to a single file-scope constant in fan.cpp (avoids duplication per TU) - Add 2026.11.0 removal comments on all compat code paths
This commit is contained in:
@@ -9,6 +9,16 @@ namespace fan {
|
||||
|
||||
static const char *const TAG = "fan";
|
||||
|
||||
// Compat: shared empty vector for getter when no preset modes are set.
|
||||
// Remove in 2026.11.0 when deprecated FanTraits setters are removed
|
||||
// and getter can return const vector * instead of const vector &.
|
||||
static const std::vector<const char *> EMPTY_PRESET_MODES; // NOLINT
|
||||
|
||||
const std::vector<const char *> &FanTraits::supported_preset_modes() const {
|
||||
// Compat: return empty ref when pointer is null. Remove in 2026.11.0 (change return to const vector *).
|
||||
return this->preset_modes_ ? *this->preset_modes_ : EMPTY_PRESET_MODES;
|
||||
}
|
||||
|
||||
// Fan direction strings indexed by FanDirection enum (0-1): FORWARD, REVERSE, plus UNKNOWN
|
||||
PROGMEM_STRING_TABLE(FanDirectionStrings, "FORWARD", "REVERSE", "UNKNOWN");
|
||||
|
||||
|
||||
@@ -179,6 +179,7 @@ class Fan : public EntityBase {
|
||||
ESPPreferenceObject rtc_;
|
||||
FanRestoreMode restore_mode_;
|
||||
|
||||
private:
|
||||
/// Lazy-allocate preset modes vector (never freed — entity lives forever).
|
||||
std::vector<const char *> &ensure_preset_modes_() {
|
||||
if (!this->supported_preset_modes_) {
|
||||
@@ -188,8 +189,6 @@ class Fan : public EntityBase {
|
||||
}
|
||||
|
||||
std::vector<const char *> *supported_preset_modes_{nullptr};
|
||||
|
||||
private:
|
||||
const char *preset_mode_{nullptr};
|
||||
};
|
||||
|
||||
|
||||
@@ -31,11 +31,8 @@ class FanTraits {
|
||||
bool supports_direction() const { return this->direction_; }
|
||||
/// Set whether this fan supports changing direction
|
||||
void set_direction(bool direction) { this->direction_ = direction; }
|
||||
/// Return the preset modes supported by the fan.
|
||||
const std::vector<const char *> &supported_preset_modes() const {
|
||||
static const std::vector<const char *> EMPTY_VECTOR;
|
||||
return this->preset_modes_ ? *this->preset_modes_ : EMPTY_VECTOR;
|
||||
}
|
||||
// Compat: returns const ref with empty fallback. In 2026.11.0 change to return const vector *.
|
||||
const std::vector<const char *> &supported_preset_modes() const;
|
||||
/// Set the preset modes pointer (points to vector owned by Fan base class).
|
||||
void set_supported_preset_modes(const std::vector<const char *> *preset_modes) { this->preset_modes_ = preset_modes; }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user