ESPHome 2026.5.0
Loading...
Searching...
No Matches
fan.h
Go to the documentation of this file.
1#pragma once
9#include "fan_traits.h"
10
11namespace esphome::fan {
12
13#define LOG_FAN(prefix, type, obj) \
14 if ((obj) != nullptr) { \
15 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
16 (obj)->dump_traits_(TAG, prefix); \
17 }
18
20enum class FanDirection { FORWARD = 0, REVERSE = 1 };
21
32
34
35class Fan;
36
37class FanCall {
38 public:
39 explicit FanCall(Fan &parent) : parent_(parent) {}
40
41 FanCall &set_state(bool binary_state) {
42 this->binary_state_ = binary_state;
43 return *this;
44 }
45 FanCall &set_state(optional<bool> binary_state) {
46 this->binary_state_ = binary_state;
47 return *this;
48 }
49 optional<bool> get_state() const { return this->binary_state_; }
52 return *this;
53 }
56 return *this;
57 }
58 optional<bool> get_oscillating() const { return this->oscillating_; }
59 FanCall &set_speed(int speed) {
60 this->speed_ = speed;
61 return *this;
62 }
63 optional<int> get_speed() const { return this->speed_; }
65 this->direction_ = direction;
66 return *this;
67 }
68 FanCall &set_direction(optional<FanDirection> direction) {
69 this->direction_ = direction;
70 return *this;
71 }
72 optional<FanDirection> get_direction() const { return this->direction_; }
73 FanCall &set_preset_mode(const std::string &preset_mode);
75 FanCall &set_preset_mode(const char *preset_mode, size_t len);
76 const char *get_preset_mode() const { return this->preset_mode_; }
77 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
78
79 void perform();
80
81 protected:
82 void validate_();
83
85 optional<bool> binary_state_;
86 optional<bool> oscillating_;
87 optional<int> speed_;
88 optional<FanDirection> direction_{};
89 const char *preset_mode_{nullptr}; // Pointer to string in traits (after validation)
90};
91
93 static constexpr uint8_t NO_PRESET = UINT8_MAX;
94
95 bool state;
96 int speed;
100
102 FanCall to_call(Fan &fan);
104 void apply(Fan &fan);
105} __attribute__((packed));
106
107class Fan : public EntityBase {
108 public:
110 bool state{false};
112 bool oscillating{false};
114 int speed{0};
117
120 FanCall toggle();
122
124 template<typename F> void add_on_state_callback(F &&callback) {
125 this->state_callback_.add(std::forward<F>(callback));
126 }
127
128 void publish_state();
129
130 virtual FanTraits get_traits() = 0;
131
133 void set_supported_preset_modes(std::initializer_list<const char *> preset_modes) {
134 this->ensure_preset_modes_().assign(preset_modes.begin(), preset_modes.end());
135 }
136 void set_supported_preset_modes(const std::vector<const char *> &preset_modes) {
137 this->ensure_preset_modes_() = preset_modes;
138 }
139
141 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
142
147 StringRef get_preset_mode() const { return StringRef::from_maybe_nullptr(this->preset_mode_); }
148
150 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
151
152 protected:
153 friend FanCall;
154 friend struct FanRestoreState;
155
156 virtual void control(const FanCall &call) = 0;
157
158 optional<FanRestoreState> restore_state_();
159 void save_state_();
160
161 void dump_traits_(const char *tag, const char *prefix);
162
165 bool set_preset_mode_(const char *preset_mode, size_t len);
166 bool set_preset_mode_(const char *preset_mode);
167 bool set_preset_mode_(const std::string &preset_mode);
170 void clear_preset_mode_();
172 void apply_preset_mode_(const FanCall &call);
174 const char *find_preset_mode_(const char *preset_mode);
175 const char *find_preset_mode_(const char *preset_mode, size_t len);
176
179 if (this->supported_preset_modes_) {
180 traits.set_supported_preset_modes_(this->supported_preset_modes_);
181 }
182 }
183
187
188 private:
190 std::vector<const char *> &ensure_preset_modes_() {
191 if (!this->supported_preset_modes_) {
192 this->supported_preset_modes_ = new std::vector<const char *>(); // NOLINT
193 }
194 return *this->supported_preset_modes_;
195 }
196
197 std::vector<const char *> *supported_preset_modes_{nullptr};
198 const char *preset_mode_{nullptr};
200
201} // namespace esphome::fan
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:53
FanCall & set_oscillating(bool oscillating)
Definition fan.h:50
FanCall & set_direction(FanDirection direction)
Definition fan.h:64
optional< bool > binary_state_
Definition fan.h:85
optional< FanDirection > direction_
Definition fan.h:88
FanCall & set_direction(optional< FanDirection > direction)
Definition fan.h:68
optional< bool > get_state() const
Definition fan.h:49
optional< int > speed_
Definition fan.h:87
const char * preset_mode_
Definition fan.h:89
const char * get_preset_mode() const
Definition fan.h:76
optional< bool > get_oscillating() const
Definition fan.h:58
FanCall & set_state(optional< bool > binary_state)
Definition fan.h:45
bool has_preset_mode() const
Definition fan.h:77
FanCall & set_speed(int speed)
Definition fan.h:59
FanCall & set_state(bool binary_state)
Definition fan.h:41
FanCall(Fan &parent)
Definition fan.h:39
FanCall & set_oscillating(optional< bool > oscillating)
Definition fan.h:54
optional< int > get_speed() const
Definition fan.h:63
optional< bool > oscillating_
Definition fan.h:86
FanCall & set_preset_mode(const std::string &preset_mode)
Definition fan.cpp:34
optional< FanDirection > get_direction() const
Definition fan.h:72
friend FanCall
Definition fan.h:153
void set_supported_preset_modes(std::initializer_list< const char * > preset_modes)
Set the supported preset modes (stored on Fan, referenced by FanTraits via pointer).
Definition fan.h:133
void add_on_state_callback(F &&callback)
Register a callback that will be called each time the state changes.
Definition fan.h:124
void publish_state()
Definition fan.cpp:223
FanCall turn_on()
Definition fan.cpp:156
FanCall turn_off()
Definition fan.cpp:157
void set_supported_preset_modes(const std::vector< const char * > &preset_modes)
Definition fan.h:136
FanCall make_call()
Definition fan.cpp:159
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:158
LazyCallbackManager< void()> state_callback_
Definition fan.h:184
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:214
ESPPreferenceObject rtc_
Definition fan.h:185
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:212
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:181
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:147
FanDirection direction
The current direction of the fan.
Definition fan.h:116
void save_state_()
Definition fan.cpp:286
FanRestoreMode restore_mode_
Definition fan.h:186
bool oscillating
The current oscillation state of the fan.
Definition fan.h:112
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
Definition fan.h:110
const char * find_preset_mode_(const char *preset_mode)
Find and return the matching preset mode pointer from traits, or nullptr if not found.
Definition fan.cpp:161
void wire_preset_modes_(FanTraits &traits)
Wire the Fan-owned preset modes pointer into the given traits object.
Definition fan.h:178
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:141
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:150
int speed
The current fan speed level.
Definition fan.h:114
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:325
optional< FanRestoreState > restore_state_()
Definition fan.cpp:251
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
FanDirection direction
Definition fan.h:5
bool oscillating
Definition fan.h:4
uint8_t preset_mode
Definition fan.h:6
FanRestoreMode
Restore mode of a fan.
Definition fan.h:23
const LogString * fan_direction_to_string(FanDirection direction)
Definition fan.cpp:30
esphome::fan::Fan __attribute__
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:20
const char * tag
Definition log.h:74
std::string size_t len
static constexpr uint8_t NO_PRESET
Definition fan.h:93
void apply(Fan &fan)
Apply these settings to the fan.
Definition fan.cpp:138
FanDirection direction
Definition fan.h:98
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
Definition fan.cpp:121