ESPHome 2026.5.1
Loading...
Searching...
No Matches
fan_traits.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstring>
4#include <vector>
5#include <initializer_list>
7
8namespace esphome::fan {
9
10class Fan; // Forward declaration
11
12class FanTraits {
13 friend class Fan; // Allow Fan to access protected pointer setter
14
15 public:
16 FanTraits() = default;
17 FanTraits(bool oscillation, bool speed, bool direction, int speed_count)
18 : oscillation_(oscillation), speed_(speed), direction_(direction), speed_count_(speed_count) {}
19
21 bool supports_oscillation() const { return this->oscillation_; }
23 void set_oscillation(bool oscillation) { this->oscillation_ = oscillation; }
25 bool supports_speed() const { return this->speed_; }
27 void set_speed(bool speed) { this->speed_ = speed; }
29 int supported_speed_count() const { return this->speed_count_; }
31 void set_supported_speed_count(int speed_count) { this->speed_count_ = speed_count; }
33 bool supports_direction() const { return this->direction_; }
36 // Compat: returns const ref with empty fallback. In 2026.11.0 change to return const vector *.
37 const std::vector<const char *> &supported_preset_modes() const;
38 // Remove before 2026.11.0
39 ESPDEPRECATED("Call set_supported_preset_modes() on the Fan entity instead. Removed in 2026.11.0", "2026.5.0")
40 void set_supported_preset_modes(std::initializer_list<const char *> preset_modes) {
41 // Compat: store in owned vector. Copies copy the vector (deprecated path still copies this vector).
42 this->compat_preset_modes_ = preset_modes;
43 }
44 // Remove before 2026.11.0
45 ESPDEPRECATED("Call set_supported_preset_modes() on the Fan entity instead. Removed in 2026.11.0", "2026.5.0")
46 void set_supported_preset_modes(const std::vector<const char *> &preset_modes) {
47 this->compat_preset_modes_ = preset_modes;
48 }
49
50 // Deleted overloads to catch incorrect std::string usage at compile time with clear error messages
51 void set_supported_preset_modes(const std::vector<std::string> &preset_modes) = delete;
52 void set_supported_preset_modes(std::initializer_list<std::string> preset_modes) = delete;
53
55 bool supports_preset_modes() const {
56 // Same precedence as supported_preset_modes() getter
57 if (this->preset_modes_) {
58 return !this->preset_modes_->empty();
59 }
60 return !this->compat_preset_modes_.empty();
61 }
63 const char *find_preset_mode(const char *preset_mode) const {
64 return this->find_preset_mode(preset_mode, preset_mode ? strlen(preset_mode) : 0);
65 }
66 const char *find_preset_mode(const char *preset_mode, size_t len) const {
67 if (preset_mode == nullptr || len == 0) {
68 return nullptr;
69 }
70 // Check pointer-based storage (new path) then compat owned vector (deprecated path)
71 const auto &modes = this->preset_modes_ ? *this->preset_modes_ : this->compat_preset_modes_;
72 for (const char *mode : modes) {
73 if (strncmp(mode, preset_mode, len) == 0 && mode[len] == '\0') {
74 return mode;
75 }
76 }
77 return nullptr;
78 }
79
80 protected:
82 void set_supported_preset_modes_(const std::vector<const char *> *preset_modes) {
83 this->preset_modes_ = preset_modes;
84 }
85
86 bool oscillation_{false};
87 bool speed_{false};
88 bool direction_{false};
90 const std::vector<const char *> *preset_modes_{nullptr};
91 // Compat: owned storage for deprecated setters. Copies copy the vector (copies include this vector).
92 // Remove in 2026.11.0.
93 std::vector<const char *> compat_preset_modes_;
94};
95
96} // namespace esphome::fan
BedjetMode mode
BedJet operating mode.
void set_direction(bool direction)
Set whether this fan supports changing direction.
Definition fan_traits.h:35
bool supports_preset_modes() const
Return if preset modes are supported.
Definition fan_traits.h:55
void set_speed(bool speed)
Set whether this fan supports speed levels.
Definition fan_traits.h:27
int supported_speed_count() const
Return how many speed levels the fan has.
Definition fan_traits.h:29
const char * find_preset_mode(const char *preset_mode, size_t len) const
Definition fan_traits.h:66
std::vector< const char * > compat_preset_modes_
Definition fan_traits.h:93
void set_supported_preset_modes_(const std::vector< const char * > *preset_modes)
Set the preset modes pointer (only Fan::wire_preset_modes_() should call this).
Definition fan_traits.h:82
void set_supported_speed_count(int speed_count)
Set how many speed levels this fan has.
Definition fan_traits.h:31
const std::vector< const char * > * preset_modes_
Definition fan_traits.h:90
bool supports_direction() const
Return if this fan supports changing direction.
Definition fan_traits.h:33
void set_oscillation(bool oscillation)
Set whether this fan supports oscillation.
Definition fan_traits.h:23
void set_supported_preset_modes(const std::vector< std::string > &preset_modes)=delete
bool supports_speed() const
Return if this fan supports speed modes.
Definition fan_traits.h:25
FanTraits(bool oscillation, bool speed, bool direction, int speed_count)
Definition fan_traits.h:17
void set_supported_preset_modes(std::initializer_list< std::string > preset_modes)=delete
ESPDEPRECATED("Call set_supported_preset_modes() on the Fan entity instead. Removed in 2026.11.0", "2026.5.0") void set_supported_preset_modes(const std
Definition fan_traits.h:45
const std::vector< const char * > & supported_preset_modes() const
Definition fan.cpp:16
ESPDEPRECATED("Call set_supported_preset_modes() on the Fan entity instead. Removed in 2026.11.0", "2026.5.0") void set_supported_preset_modes(std
Definition fan_traits.h:39
const char * find_preset_mode(const char *preset_mode) const
Find and return the matching preset mode pointer from supported modes, or nullptr if not found.
Definition fan_traits.h:63
bool supports_oscillation() const
Return if this fan supports oscillation.
Definition fan_traits.h:21
FanDirection direction
Definition fan.h:5
int speed
Definition fan.h:3
uint8_t preset_mode
Definition fan.h:6
std::string size_t len