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