ESPHome 2026.1.3
Loading...
Searching...
No Matches
fan.cpp
Go to the documentation of this file.
1#include "fan.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace fan {
8
9static const char *const TAG = "fan";
10
12 switch (direction) {
14 return LOG_STR("FORWARD");
16 return LOG_STR("REVERSE");
17 default:
18 return LOG_STR("UNKNOWN");
19 }
20}
21
23 return this->set_preset_mode(preset_mode.data(), preset_mode.size());
24}
25
27 return this->set_preset_mode(preset_mode, preset_mode ? strlen(preset_mode) : 0);
28}
29
31 if (preset_mode == nullptr || len == 0) {
32 this->preset_mode_ = nullptr;
33 return *this;
34 }
35
36 // Find and validate pointer from traits immediately
37 auto traits = this->parent_.get_traits();
38 const char *validated_mode = traits.find_preset_mode(preset_mode, len);
39 if (validated_mode != nullptr) {
40 this->preset_mode_ = validated_mode; // Store pointer from traits
41 } else {
42 // Preset mode not found in traits - log warning and don't set
43 ESP_LOGW(TAG, "%s: Preset mode '%.*s' not supported", this->parent_.get_name().c_str(), (int) len, preset_mode);
44 this->preset_mode_ = nullptr;
45 }
46 return *this;
47}
48
50 ESP_LOGD(TAG, "'%s' - Setting:", this->parent_.get_name().c_str());
51 this->validate_();
52 if (this->binary_state_.has_value()) {
53 ESP_LOGD(TAG, " State: %s", ONOFF(*this->binary_state_));
54 }
55 if (this->oscillating_.has_value()) {
56 ESP_LOGD(TAG, " Oscillating: %s", YESNO(*this->oscillating_));
57 }
58 if (this->speed_.has_value()) {
59 ESP_LOGD(TAG, " Speed: %d", *this->speed_);
60 }
61 if (this->direction_.has_value()) {
62 ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(*this->direction_)));
63 }
64 if (this->preset_mode_ != nullptr) {
65 ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_);
66 }
67 this->parent_.control(*this);
68}
69
71 auto traits = this->parent_.get_traits();
72
73 if (this->speed_.has_value()) {
74 this->speed_ = clamp(*this->speed_, 1, traits.supported_speed_count());
75
76 // https://developers.home-assistant.io/docs/core/entity/fan/#preset-modes
77 // "Manually setting a speed must disable any set preset mode"
78 this->preset_mode_ = nullptr;
79 }
80
81 // when turning on...
82 if (!this->parent_.state && this->binary_state_.has_value() &&
83 *this->binary_state_
84 // ..,and no preset mode will be active...
85 && !this->has_preset_mode() &&
86 !this->parent_.has_preset_mode()
87 // ...and neither current nor new speed is available...
88 && traits.supports_speed() && this->parent_.speed == 0 && !this->speed_.has_value()) {
89 // ...set speed to 100%
90 this->speed_ = traits.supported_speed_count();
91 }
92
93 if (this->oscillating_.has_value() && !traits.supports_oscillation()) {
94 ESP_LOGW(TAG, "%s: Oscillation not supported", this->parent_.get_name().c_str());
95 this->oscillating_.reset();
96 }
97
98 if (this->speed_.has_value() && !traits.supports_speed()) {
99 ESP_LOGW(TAG, "%s: Speed control not supported", this->parent_.get_name().c_str());
100 this->speed_.reset();
101 }
102
103 if (this->direction_.has_value() && !traits.supports_direction()) {
104 ESP_LOGW(TAG, "%s: Direction control not supported", this->parent_.get_name().c_str());
105 this->direction_.reset();
106 }
107}
108
110 auto call = fan.make_call();
111 call.set_state(this->state);
112 call.set_oscillating(this->oscillating);
113 call.set_speed(this->speed);
114 call.set_direction(this->direction);
115
116 auto traits = fan.get_traits();
117 if (traits.supports_preset_modes()) {
118 // Use stored preset index to get preset name
119 const auto &preset_modes = traits.supported_preset_modes();
120 if (this->preset_mode < preset_modes.size()) {
121 call.set_preset_mode(preset_modes[this->preset_mode]);
122 }
123 }
124 return call;
125}
127 fan.state = this->state;
128 fan.oscillating = this->oscillating;
129 fan.speed = this->speed;
130 fan.direction = this->direction;
131
132 auto traits = fan.get_traits();
133 if (traits.supports_preset_modes()) {
134 // Use stored preset index to get preset name from traits
135 const auto &preset_modes = traits.supported_preset_modes();
136 if (this->preset_mode < preset_modes.size()) {
137 fan.set_preset_mode_(preset_modes[this->preset_mode]);
138 }
139 }
140
141 fan.publish_state();
142}
143
144FanCall Fan::turn_on() { return this->make_call().set_state(true); }
145FanCall Fan::turn_off() { return this->make_call().set_state(false); }
146FanCall Fan::toggle() { return this->make_call().set_state(!this->state); }
147FanCall Fan::make_call() { return FanCall(*this); }
148
149const char *Fan::find_preset_mode_(const char *preset_mode) {
150 return this->find_preset_mode_(preset_mode, preset_mode ? strlen(preset_mode) : 0);
151}
152
153const char *Fan::find_preset_mode_(const char *preset_mode, size_t len) {
154 return this->get_traits().find_preset_mode(preset_mode, len);
155}
156
157bool Fan::set_preset_mode_(const char *preset_mode, size_t len) {
158 if (preset_mode == nullptr || len == 0) {
159 // Treat nullptr or empty string as clearing the preset mode (no valid preset is "")
160 if (this->preset_mode_ == nullptr) {
161 return false; // No change
162 }
163 this->clear_preset_mode_();
164 return true;
165 }
166 const char *validated = this->find_preset_mode_(preset_mode, len);
167 if (validated == nullptr || this->preset_mode_ == validated) {
168 return false; // Preset mode not supported or no change
169 }
170 this->preset_mode_ = validated;
171 return true;
172}
173
175 return this->set_preset_mode_(preset_mode, preset_mode ? strlen(preset_mode) : 0);
176}
177
178bool Fan::set_preset_mode_(const std::string &preset_mode) {
179 return this->set_preset_mode_(preset_mode.data(), preset_mode.size());
180}
181
183 // Safe: find_preset_mode_ only uses the input for comparison and returns
184 // a pointer from traits, so the input StringRef's lifetime doesn't matter.
185 return this->set_preset_mode_(preset_mode.c_str(), preset_mode.size());
186}
187
188void Fan::clear_preset_mode_() { this->preset_mode_ = nullptr; }
189
191 if (call.has_preset_mode()) {
192 this->set_preset_mode_(call.get_preset_mode());
193 } else if (call.get_speed().has_value()) {
194 // Manually setting speed clears preset (per Home Assistant convention)
195 this->clear_preset_mode_();
196 }
197}
198
199void Fan::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
201 auto traits = this->get_traits();
202
203 ESP_LOGD(TAG,
204 "'%s' >>\n"
205 " State: %s",
206 this->name_.c_str(), ONOFF(this->state));
207 if (traits.supports_speed()) {
208 ESP_LOGD(TAG, " Speed: %d", this->speed);
209 }
210 if (traits.supports_oscillation()) {
211 ESP_LOGD(TAG, " Oscillating: %s", YESNO(this->oscillating));
212 }
213 if (traits.supports_direction()) {
214 ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(this->direction)));
215 }
216 if (this->preset_mode_ != nullptr) {
217 ESP_LOGD(TAG, " Preset Mode: %s", this->preset_mode_);
218 }
219 this->state_callback_.call();
220#if defined(USE_FAN) && defined(USE_CONTROLLER_REGISTRY)
222#endif
223 this->save_state_();
224}
225
226// Random 32-bit value, change this every time the layout of the FanRestoreState struct changes.
227constexpr uint32_t RESTORE_STATE_VERSION = 0x71700ABA;
229 FanRestoreState recovered{};
230 this->rtc_ =
232 bool restored = this->rtc_.load(&recovered);
233
234 switch (this->restore_mode_) {
236 return {};
238 recovered.state = false;
239 return recovered;
241 recovered.state = true;
242 return recovered;
244 recovered.state = restored ? recovered.state : false;
245 return recovered;
247 recovered.state = restored ? recovered.state : true;
248 return recovered;
250 recovered.state = restored ? !recovered.state : false;
251 return recovered;
253 recovered.state = restored ? !recovered.state : true;
254 return recovered;
255 }
256
257 return {};
258}
261 return;
262 }
263
264 auto traits = this->get_traits();
265
267 state.state = this->state;
268 state.oscillating = this->oscillating;
269 state.speed = this->speed;
270 state.direction = this->direction;
271
272 if (this->has_preset_mode()) {
273 const auto &preset_modes = traits.supported_preset_modes();
274 // Find index of current preset mode (pointer comparison is safe since preset is from traits)
275 for (size_t i = 0; i < preset_modes.size(); i++) {
276 if (preset_modes[i] == this->preset_mode_) {
277 state.preset_mode = i;
278 break;
279 }
280 }
281 }
282
283 this->rtc_.save(&state);
284}
285
286void Fan::dump_traits_(const char *tag, const char *prefix) {
287 auto traits = this->get_traits();
288
289 if (traits.supports_speed()) {
290 ESP_LOGCONFIG(tag,
291 "%s Speed: YES\n"
292 "%s Speed count: %d",
293 prefix, prefix, traits.supported_speed_count());
294 }
295 if (traits.supports_oscillation()) {
296 ESP_LOGCONFIG(tag, "%s Oscillation: YES", prefix);
297 }
298 if (traits.supports_direction()) {
299 ESP_LOGCONFIG(tag, "%s Direction: YES", prefix);
300 }
301 if (traits.supports_preset_modes()) {
302 ESP_LOGCONFIG(tag, "%s Supported presets:", prefix);
303 for (const char *s : traits.supported_preset_modes())
304 ESP_LOGCONFIG(tag, "%s - %s", prefix, s);
305 }
306}
307
308} // namespace fan
309} // namespace esphome
static void notify_fan_update(fan::Fan *obj)
bool save(const T *src)
Definition preferences.h:21
virtual ESPPreferenceObject make_preference(size_t length, uint32_t type, bool in_flash)=0
const StringRef & get_name() const
uint32_t get_preference_hash()
Get a unique hash for storing preferences/settings for this entity.
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
constexpr const char * c_str() const
Definition string_ref.h:73
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
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
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
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:22
friend FanCall
Definition fan.h:142
void publish_state()
Definition fan.cpp:200
FanCall turn_on()
Definition fan.cpp:144
FanCall turn_off()
Definition fan.cpp:145
FanCall make_call()
Definition fan.cpp:147
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:146
LazyCallbackManager< void()> state_callback_
Definition fan.h:166
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:190
ESPPreferenceObject rtc_
Definition fan.h:167
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:188
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:157
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition fan.cpp:199
FanDirection direction
The current direction of the fan.
Definition fan.h:115
void save_state_()
Definition fan.cpp:259
FanRestoreMode restore_mode_
Definition fan.h:168
bool oscillating
The current oscillation state of the fan.
Definition fan.h:111
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
Definition fan.h:109
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:149
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:139
int speed
The current fan speed level.
Definition fan.h:113
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:286
optional< FanRestoreState > restore_state_()
Definition fan.cpp:228
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:49
bool has_value() const
Definition optional.h:92
FanDirection direction
Definition fan.h:3
uint8_t preset_mode
Definition fan.h:4
const LogString * fan_direction_to_string(FanDirection direction)
Definition fan.cpp:11
constexpr uint32_t RESTORE_STATE_VERSION
Definition fan.cpp:227
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
std::string size_t len
Definition helpers.h:595
ESPPreferences * global_preferences
void apply(Fan &fan)
Apply these settings to the fan.
Definition fan.cpp:126
FanDirection direction
Definition fan.h:97
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
Definition fan.cpp:109