ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
fan.h
Go to the documentation of this file.
1#pragma once
9#include "fan_traits.h"
10
11namespace esphome {
12namespace fan {
13
14#define LOG_FAN(prefix, type, obj) \
15 if ((obj) != nullptr) { \
16 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
17 (obj)->dump_traits_(TAG, prefix); \
18 }
19
21enum class FanDirection { FORWARD = 0, REVERSE = 1 };
22
33
35
36class Fan;
37
38class FanCall {
39 public:
40 explicit FanCall(Fan &parent) : parent_(parent) {}
41
42 FanCall &set_state(bool binary_state) {
43 this->binary_state_ = binary_state;
44 return *this;
45 }
46 FanCall &set_state(optional<bool> binary_state) {
47 this->binary_state_ = binary_state;
48 return *this;
49 }
50 optional<bool> get_state() const { return this->binary_state_; }
53 return *this;
54 }
57 return *this;
58 }
59 optional<bool> get_oscillating() const { return this->oscillating_; }
60 FanCall &set_speed(int speed) {
61 this->speed_ = speed;
62 return *this;
63 }
64 optional<int> get_speed() const { return this->speed_; }
66 this->direction_ = direction;
67 return *this;
68 }
69 FanCall &set_direction(optional<FanDirection> direction) {
70 this->direction_ = direction;
71 return *this;
72 }
73 optional<FanDirection> get_direction() const { return this->direction_; }
74 FanCall &set_preset_mode(const std::string &preset_mode);
76 FanCall &set_preset_mode(const char *preset_mode, size_t len);
77 const char *get_preset_mode() const { return this->preset_mode_; }
78 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
79
80 void perform();
81
82 protected:
83 void validate_();
84
86 optional<bool> binary_state_;
87 optional<bool> oscillating_;
88 optional<int> speed_;
89 optional<FanDirection> direction_{};
90 const char *preset_mode_{nullptr}; // Pointer to string in traits (after validation)
91};
92
94 static constexpr uint8_t NO_PRESET = UINT8_MAX;
95
96 bool state;
97 int speed;
101
103 FanCall to_call(Fan &fan);
105 void apply(Fan &fan);
106} __attribute__((packed));
107
108class Fan : public EntityBase {
109 public:
111 bool state{false};
113 bool oscillating{false};
115 int speed{0};
118
121 FanCall toggle();
123
125 template<typename F> void add_on_state_callback(F &&callback) {
126 this->state_callback_.add(std::forward<F>(callback));
127 }
128
129 void publish_state();
130
131 virtual FanTraits get_traits() = 0;
132
134 void set_supported_preset_modes(std::initializer_list<const char *> preset_modes) {
135 this->ensure_preset_modes_().assign(preset_modes.begin(), preset_modes.end());
136 }
137 void set_supported_preset_modes(const std::vector<const char *> &preset_modes) {
138 this->ensure_preset_modes_() = preset_modes;
139 }
140
142 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
143
148 StringRef get_preset_mode() const { return StringRef::from_maybe_nullptr(this->preset_mode_); }
149
151 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
152
153 protected:
154 friend FanCall;
155 friend struct FanRestoreState;
156
157 virtual void control(const FanCall &call) = 0;
158
159 optional<FanRestoreState> restore_state_();
160 void save_state_();
161
162 void dump_traits_(const char *tag, const char *prefix);
163
166 bool set_preset_mode_(const char *preset_mode, size_t len);
167 bool set_preset_mode_(const char *preset_mode);
168 bool set_preset_mode_(const std::string &preset_mode);
171 void clear_preset_mode_();
173 void apply_preset_mode_(const FanCall &call);
175 const char *find_preset_mode_(const char *preset_mode);
176 const char *find_preset_mode_(const char *preset_mode, size_t len);
177
180 if (this->supported_preset_modes_) {
181 traits.set_supported_preset_modes_(this->supported_preset_modes_);
182 }
183 }
184
188
189 private:
191 std::vector<const char *> &ensure_preset_modes_() {
192 if (!this->supported_preset_modes_) {
193 this->supported_preset_modes_ = new std::vector<const char *>(); // NOLINT
194 }
195 return *this->supported_preset_modes_;
196 }
197
198 std::vector<const char *> *supported_preset_modes_{nullptr};
199 const char *preset_mode_{nullptr};
201
202} // namespace fan
203} // namespace esphome
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:51
FanCall & set_direction(FanDirection direction)
Definition fan.h:65
optional< bool > binary_state_
Definition fan.h:86
optional< FanDirection > direction_
Definition fan.h:89
FanCall & set_direction(optional< FanDirection > direction)
Definition fan.h:69
optional< bool > get_state() const
Definition fan.h:50
optional< int > speed_
Definition fan.h:88
const char * preset_mode_
Definition fan.h:90
const char * get_preset_mode() const
Definition fan.h:77
optional< bool > get_oscillating() const
Definition fan.h:59
FanCall & set_state(optional< bool > binary_state)
Definition fan.h:46
bool has_preset_mode() const
Definition fan.h:78
FanCall & set_speed(int speed)
Definition fan.h:60
FanCall & set_state(bool binary_state)
Definition fan.h:42
FanCall(Fan &parent)
Definition fan.h:40
FanCall & set_oscillating(optional< bool > oscillating)
Definition fan.h:55
optional< int > get_speed() const
Definition fan.h:64
optional< bool > oscillating_
Definition fan.h:87
FanCall & set_preset_mode(const std::string &preset_mode)
Definition fan.cpp:35
optional< FanDirection > get_direction() const
Definition fan.h:73
friend FanCall
Definition fan.h:154
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:134
void add_on_state_callback(F &&callback)
Register a callback that will be called each time the state changes.
Definition fan.h:125
void publish_state()
Definition fan.cpp:224
FanCall turn_on()
Definition fan.cpp:157
FanCall turn_off()
Definition fan.cpp:158
void set_supported_preset_modes(const std::vector< const char * > &preset_modes)
Definition fan.h:137
FanCall make_call()
Definition fan.cpp:160
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:159
LazyCallbackManager< void()> state_callback_
Definition fan.h:185
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:215
ESPPreferenceObject rtc_
Definition fan.h:186
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:213
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:182
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:148
FanDirection direction
The current direction of the fan.
Definition fan.h:117
void save_state_()
Definition fan.cpp:287
FanRestoreMode restore_mode_
Definition fan.h:187
bool oscillating
The current oscillation state of the fan.
Definition fan.h:113
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
Definition fan.h:111
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:162
void wire_preset_modes_(FanTraits &traits)
Wire the Fan-owned preset modes pointer into the given traits object.
Definition fan.h:179
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:142
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:151
int speed
The current fan speed level.
Definition fan.h:115
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:326
optional< FanRestoreState > restore_state_()
Definition fan.cpp:252
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
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:24
const LogString * fan_direction_to_string(FanDirection direction)
Definition fan.cpp:31
esphome::fan::Fan __attribute__
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:21
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
const char * tag
Definition log.h:74
std::string size_t len
Definition helpers.h:1045
static constexpr uint8_t NO_PRESET
Definition fan.h:94
void apply(Fan &fan)
Apply these settings to the fan.
Definition fan.cpp:139
FanDirection direction
Definition fan.h:99
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
Definition fan.cpp:122