From 4838c61a8c36db6acd2ecdff7afaeadb4ad36712 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 26 Mar 2026 14:02:27 -1000 Subject: [PATCH] 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 --- esphome/components/fan/fan.cpp | 10 ++++++++++ esphome/components/fan/fan.h | 3 +-- esphome/components/fan/fan_traits.h | 7 ++----- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/esphome/components/fan/fan.cpp b/esphome/components/fan/fan.cpp index 3f4b7844c0..af34d28f7a 100644 --- a/esphome/components/fan/fan.cpp +++ b/esphome/components/fan/fan.cpp @@ -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 EMPTY_PRESET_MODES; // NOLINT + +const std::vector &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"); diff --git a/esphome/components/fan/fan.h b/esphome/components/fan/fan.h index eff57a343a..b7d52aa15f 100644 --- a/esphome/components/fan/fan.h +++ b/esphome/components/fan/fan.h @@ -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 &ensure_preset_modes_() { if (!this->supported_preset_modes_) { @@ -188,8 +189,6 @@ class Fan : public EntityBase { } std::vector *supported_preset_modes_{nullptr}; - - private: const char *preset_mode_{nullptr}; }; diff --git a/esphome/components/fan/fan_traits.h b/esphome/components/fan/fan_traits.h index b6549368c9..0098088ae6 100644 --- a/esphome/components/fan/fan_traits.h +++ b/esphome/components/fan/fan_traits.h @@ -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 &supported_preset_modes() const { - static const std::vector 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 &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 *preset_modes) { this->preset_modes_ = preset_modes; }