ESPHome 2026.5.1
Loading...
Searching...
No Matches
fan.cpp
Go to the documentation of this file.
1#include "fan.h"
4#include "esphome/core/log.h"
6
7namespace esphome::fan {
8
9static const char *const TAG = "fan";
10
11// Compat: shared empty vector for getter when no preset modes are set.
12// Remove in 2026.11.0 when deprecated FanTraits setters are removed
13// and getter can return const vector * instead of const vector &.
14static const std::vector<const char *> EMPTY_PRESET_MODES; // NOLINT
15
16const std::vector<const char *> &FanTraits::supported_preset_modes() const {
17 if (this->preset_modes_) {
18 return *this->preset_modes_;
19 }
20 // Compat: fall back to owned vector from deprecated setters. Remove in 2026.11.0 (change return to const vector *).
21 if (!this->compat_preset_modes_.empty()) {
22 return this->compat_preset_modes_;
23 }
24 return EMPTY_PRESET_MODES;
25}
26
27// Fan direction strings indexed by FanDirection enum (0-1): FORWARD, REVERSE, plus UNKNOWN
28PROGMEM_STRING_TABLE(FanDirectionStrings, "FORWARD", "REVERSE", "UNKNOWN");
29
31 return FanDirectionStrings::get_log_str(static_cast<uint8_t>(direction), FanDirectionStrings::LAST_INDEX);
32}
33
35 return this->set_preset_mode(preset_mode.data(), preset_mode.size());
36}
37
39 return this->set_preset_mode(preset_mode, preset_mode ? strlen(preset_mode) : 0);
40}
41
43 if (preset_mode == nullptr || len == 0) {
44 this->preset_mode_ = nullptr;
45 return *this;
46 }
47
48 // Find and validate pointer from traits immediately
49 auto traits = this->parent_.get_traits();
50 const char *validated_mode = traits.find_preset_mode(preset_mode, len);
51 if (validated_mode != nullptr) {
52 this->preset_mode_ = validated_mode; // Store pointer from traits
53 } else {
54 // Preset mode not found in traits - log warning and don't set
55 ESP_LOGW(TAG, "%s: Preset mode '%.*s' not supported", this->parent_.get_name().c_str(), (int) len, preset_mode);
56 this->preset_mode_ = nullptr;
57 }
58 return *this;
59}
60
62 ESP_LOGV(TAG, "'%s' - Setting:", this->parent_.get_name().c_str());
63 this->validate_();
64 if (this->binary_state_.has_value()) {
65 ESP_LOGV(TAG, " State: %s", ONOFF(*this->binary_state_));
66 }
67 if (this->oscillating_.has_value()) {
68 ESP_LOGV(TAG, " Oscillating: %s", YESNO(*this->oscillating_));
69 }
70 if (this->speed_.has_value()) {
71 ESP_LOGV(TAG, " Speed: %d", *this->speed_);
72 }
73 if (this->direction_.has_value()) {
74 ESP_LOGV(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(*this->direction_)));
75 }
76 if (this->preset_mode_ != nullptr) {
77 ESP_LOGV(TAG, " Preset Mode: %s", this->preset_mode_);
78 }
79 this->parent_.control(*this);
80}
81
83 auto traits = this->parent_.get_traits();
84
85 if (this->speed_.has_value()) {
86 this->speed_ = clamp(*this->speed_, 1, traits.supported_speed_count());
87
88 // https://developers.home-assistant.io/docs/core/entity/fan/#preset-modes
89 // "Manually setting a speed must disable any set preset mode"
90 this->preset_mode_ = nullptr;
91 }
92
93 // when turning on...
94 if (!this->parent_.state && this->binary_state_.has_value() &&
95 *this->binary_state_
96 // ..,and no preset mode will be active...
97 && !this->has_preset_mode() &&
98 !this->parent_.has_preset_mode()
99 // ...and neither current nor new speed is available...
100 && traits.supports_speed() && this->parent_.speed == 0 && !this->speed_.has_value()) {
101 // ...set speed to 100%
102 this->speed_ = traits.supported_speed_count();
103 }
104
105 if (this->oscillating_.has_value() && !traits.supports_oscillation()) {
106 ESP_LOGW(TAG, "%s: Oscillation not supported", this->parent_.get_name().c_str());
107 this->oscillating_.reset();
108 }
109
110 if (this->speed_.has_value() && !traits.supports_speed()) {
111 ESP_LOGW(TAG, "%s: Speed control not supported", this->parent_.get_name().c_str());
112 this->speed_.reset();
113 }
114
115 if (this->direction_.has_value() && !traits.supports_direction()) {
116 ESP_LOGW(TAG, "%s: Direction control not supported", this->parent_.get_name().c_str());
117 this->direction_.reset();
118 }
119}
120
122 auto call = fan.make_call();
123 call.set_state(this->state);
124 call.set_oscillating(this->oscillating);
125 call.set_speed(this->speed);
126 call.set_direction(this->direction);
127
128 auto traits = fan.get_traits();
129 if (traits.supports_preset_modes()) {
130 // Use stored preset index to get preset name
131 const auto &preset_modes = traits.supported_preset_modes();
132 if (this->preset_mode < preset_modes.size()) {
133 call.set_preset_mode(preset_modes[this->preset_mode]);
134 }
135 }
136 return call;
137}
139 fan.state = this->state;
140 fan.oscillating = this->oscillating;
141 fan.speed = this->speed;
142 fan.direction = this->direction;
143
144 auto traits = fan.get_traits();
145 if (traits.supports_preset_modes()) {
146 // Use stored preset index to get preset name from traits
147 const auto &preset_modes = traits.supported_preset_modes();
148 if (this->preset_mode < preset_modes.size()) {
149 fan.set_preset_mode_(preset_modes[this->preset_mode]);
150 }
151 }
152
153 fan.publish_state();
154}
155
156FanCall Fan::turn_on() { return this->make_call().set_state(true); }
157FanCall Fan::turn_off() { return this->make_call().set_state(false); }
158FanCall Fan::toggle() { return this->make_call().set_state(!this->state); }
159FanCall Fan::make_call() { return FanCall(*this); }
160
161const char *Fan::find_preset_mode_(const char *preset_mode) {
162 return this->find_preset_mode_(preset_mode, preset_mode ? strlen(preset_mode) : 0);
163}
164
165const char *Fan::find_preset_mode_(const char *preset_mode, size_t len) {
166 if (preset_mode == nullptr || len == 0) {
167 return nullptr;
168 }
169 if (this->supported_preset_modes_) {
170 for (const char *mode : *this->supported_preset_modes_) {
171 if (strncmp(mode, preset_mode, len) == 0 && mode[len] == '\0') {
172 return mode;
173 }
174 }
175 return nullptr;
176 }
177 // Fallback for deprecated path: external components may set modes on FanTraits directly
178 return this->get_traits().find_preset_mode(preset_mode, len);
179}
180
181bool Fan::set_preset_mode_(const char *preset_mode, size_t len) {
182 if (preset_mode == nullptr || len == 0) {
183 // Treat nullptr or empty string as clearing the preset mode (no valid preset is "")
184 if (this->preset_mode_ == nullptr) {
185 return false; // No change
186 }
187 this->clear_preset_mode_();
188 return true;
189 }
190 const char *validated = this->find_preset_mode_(preset_mode, len);
191 if (validated == nullptr || this->preset_mode_ == validated) {
192 return false; // Preset mode not supported or no change
193 }
194 this->preset_mode_ = validated;
195 return true;
196}
197
199 return this->set_preset_mode_(preset_mode, preset_mode ? strlen(preset_mode) : 0);
200}
201
202bool Fan::set_preset_mode_(const std::string &preset_mode) {
203 return this->set_preset_mode_(preset_mode.data(), preset_mode.size());
204}
205
207 // Safe: find_preset_mode_ only uses the input for comparison and returns
208 // a pointer from traits, so the input StringRef's lifetime doesn't matter.
209 return this->set_preset_mode_(preset_mode.c_str(), preset_mode.size());
210}
211
212void Fan::clear_preset_mode_() { this->preset_mode_ = nullptr; }
213
215 if (call.has_preset_mode()) {
216 this->set_preset_mode_(call.get_preset_mode());
217 } else if (call.get_speed().has_value()) {
218 // Manually setting speed clears preset (per Home Assistant convention)
219 this->clear_preset_mode_();
220 }
221}
222
224 auto traits = this->get_traits();
225
226 ESP_LOGV(TAG,
227 "'%s' >>\n"
228 " State: %s",
229 this->name_.c_str(), ONOFF(this->state));
230 if (traits.supports_speed()) {
231 ESP_LOGV(TAG, " Speed: %d", this->speed);
232 }
233 if (traits.supports_oscillation()) {
234 ESP_LOGV(TAG, " Oscillating: %s", YESNO(this->oscillating));
235 }
236 if (traits.supports_direction()) {
237 ESP_LOGV(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(this->direction)));
238 }
239 if (this->preset_mode_ != nullptr) {
240 ESP_LOGV(TAG, " Preset Mode: %s", this->preset_mode_);
241 }
242 this->state_callback_.call();
243#if defined(USE_FAN) && defined(USE_CONTROLLER_REGISTRY)
244 ControllerRegistry::notify_fan_update(this);
245#endif
246 this->save_state_();
247}
248
249// Random 32-bit value, change this every time the layout of the FanRestoreState struct changes.
250constexpr uint32_t RESTORE_STATE_VERSION = 0x71700ABB;
251optional<FanRestoreState> Fan::restore_state_() {
252 FanRestoreState recovered{};
253 this->rtc_ = this->make_entity_preference<FanRestoreState>(RESTORE_STATE_VERSION);
254 bool restored = this->rtc_.load(&recovered);
255
256 if (!restored) {
257 // No valid saved data; ensure preset_mode sentinel is set
258 recovered.preset_mode = FanRestoreState::NO_PRESET;
259 }
260
261 switch (this->restore_mode_) {
263 return {};
265 recovered.state = false;
266 return recovered;
268 recovered.state = true;
269 return recovered;
271 recovered.state = restored ? recovered.state : false;
272 return recovered;
274 recovered.state = restored ? recovered.state : true;
275 return recovered;
277 recovered.state = restored ? !recovered.state : false;
278 return recovered;
280 recovered.state = restored ? !recovered.state : true;
281 return recovered;
282 }
283
284 return {};
285}
288 return;
289 }
290
292 state.state = this->state;
293 state.oscillating = this->oscillating;
294 state.speed = this->speed;
295 state.direction = this->direction;
296 state.preset_mode = FanRestoreState::NO_PRESET;
297
298 if (this->has_preset_mode()) {
299 if (this->supported_preset_modes_) {
300 // New path: search Fan-owned vector directly
301 for (size_t i = 0; i < this->supported_preset_modes_->size(); i++) {
302 if ((*this->supported_preset_modes_)[i] == this->preset_mode_) {
303 state.preset_mode = i;
304 break;
305 }
306 }
307 } else {
308 // Compat: fall back to traits for deprecated path. Remove in 2026.11.0.
309 // Pointer comparison works because preset_mode_ and the compat vector both
310 // hold pointers to string literals in .rodata (stable addresses).
311 auto traits = this->get_traits();
312 const auto &preset_modes = traits.supported_preset_modes();
313 for (size_t i = 0; i < preset_modes.size(); i++) {
314 if (preset_modes[i] == this->preset_mode_) {
315 state.preset_mode = i;
316 break;
317 }
318 }
319 }
320 }
321
322 this->rtc_.save(&state);
323}
324
325void Fan::dump_traits_(const char *tag, const char *prefix) {
326 auto traits = this->get_traits();
327
328 if (traits.supports_speed()) {
329 ESP_LOGCONFIG(tag,
330 "%s Speed: YES\n"
331 "%s Speed count: %d",
332 prefix, prefix, traits.supported_speed_count());
333 }
334 if (traits.supports_oscillation()) {
335 ESP_LOGCONFIG(tag, "%s Oscillation: YES", prefix);
336 }
337 if (traits.supports_direction()) {
338 ESP_LOGCONFIG(tag, "%s Direction: YES", prefix);
339 }
340 if (traits.supports_preset_modes()) {
341 ESP_LOGCONFIG(tag, "%s Supported presets:", prefix);
342 for (const char *s : traits.supported_preset_modes())
343 ESP_LOGCONFIG(tag, "%s - %s", prefix, s);
344 }
345}
346
347} // namespace esphome::fan
BedjetMode mode
BedJet operating mode.
const StringRef & get_name() const
Definition entity_base.h:71
ESPPreferenceObject make_entity_preference(uint32_t version=0)
Create a preference object for storing this entity's state/settings.
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: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
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
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
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
friend FanCall
Definition fan.h:153
void publish_state()
Definition fan.cpp:223
FanCall turn_on()
Definition fan.cpp:156
FanCall turn_off()
Definition fan.cpp:157
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
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
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
std::vector< const char * > compat_preset_modes_
Definition fan_traits.h:93
const std::vector< const char * > * preset_modes_
Definition fan_traits.h:90
const std::vector< const char * > & supported_preset_modes() const
Definition fan.cpp:16
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
FanDirection direction
Definition fan.h:5
uint8_t preset_mode
Definition fan.h:6
const LogString * fan_direction_to_string(FanDirection direction)
Definition fan.cpp:30
constexpr uint32_t RESTORE_STATE_VERSION
Definition fan.cpp:250
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:20
PROGMEM_STRING_TABLE(FanDirectionStrings, "FORWARD", "REVERSE", "UNKNOWN")
const char * tag
Definition log.h:74
std::string size_t len
static void uint32_t
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