ESPHome 2026.1.0-dev
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->has_preset_mode()) {
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_.get_preset_mode() == nullptr
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
158 if (preset_mode == nullptr) {
159 // Treat nullptr as clearing the preset mode
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);
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
174bool Fan::set_preset_mode_(const std::string &preset_mode) { return this->set_preset_mode_(preset_mode.c_str()); }
175
176void Fan::clear_preset_mode_() { this->preset_mode_ = nullptr; }
177
178void Fan::add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
180 auto traits = this->get_traits();
181
182 ESP_LOGD(TAG, "'%s' - Sending state:", this->name_.c_str());
183 ESP_LOGD(TAG, " State: %s", ONOFF(this->state));
184 if (traits.supports_speed()) {
185 ESP_LOGD(TAG, " Speed: %d", this->speed);
186 }
187 if (traits.supports_oscillation()) {
188 ESP_LOGD(TAG, " Oscillating: %s", YESNO(this->oscillating));
189 }
190 if (traits.supports_direction()) {
191 ESP_LOGD(TAG, " Direction: %s", LOG_STR_ARG(fan_direction_to_string(this->direction)));
192 }
193 const char *preset = this->get_preset_mode();
194 if (preset != nullptr) {
195 ESP_LOGD(TAG, " Preset Mode: %s", preset);
196 }
197 this->state_callback_.call();
198#if defined(USE_FAN) && defined(USE_CONTROLLER_REGISTRY)
200#endif
201 this->save_state_();
202}
203
204// Random 32-bit value, change this every time the layout of the FanRestoreState struct changes.
205constexpr uint32_t RESTORE_STATE_VERSION = 0x71700ABA;
207 FanRestoreState recovered{};
208 this->rtc_ =
210 bool restored = this->rtc_.load(&recovered);
211
212 switch (this->restore_mode_) {
214 return {};
216 recovered.state = false;
217 return recovered;
219 recovered.state = true;
220 return recovered;
222 recovered.state = restored ? recovered.state : false;
223 return recovered;
225 recovered.state = restored ? recovered.state : true;
226 return recovered;
228 recovered.state = restored ? !recovered.state : false;
229 return recovered;
231 recovered.state = restored ? !recovered.state : true;
232 return recovered;
233 }
234
235 return {};
236}
239 return;
240 }
241
242 auto traits = this->get_traits();
243
245 state.state = this->state;
246 state.oscillating = this->oscillating;
247 state.speed = this->speed;
248 state.direction = this->direction;
249
250 const char *preset = this->get_preset_mode();
251 if (preset != nullptr) {
252 const auto &preset_modes = traits.supported_preset_modes();
253 // Find index of current preset mode (pointer comparison is safe since preset is from traits)
254 for (size_t i = 0; i < preset_modes.size(); i++) {
255 if (preset_modes[i] == preset) {
256 state.preset_mode = i;
257 break;
258 }
259 }
260 }
261
262 this->rtc_.save(&state);
263}
264
265void Fan::dump_traits_(const char *tag, const char *prefix) {
266 auto traits = this->get_traits();
267
268 if (traits.supports_speed()) {
269 ESP_LOGCONFIG(tag,
270 "%s Speed: YES\n"
271 "%s Speed count: %d",
272 prefix, prefix, traits.supported_speed_count());
273 }
274 if (traits.supports_oscillation()) {
275 ESP_LOGCONFIG(tag, "%s Oscillation: YES", prefix);
276 }
277 if (traits.supports_direction()) {
278 ESP_LOGCONFIG(tag, "%s Direction: YES", prefix);
279 }
280 if (traits.supports_preset_modes()) {
281 ESP_LOGCONFIG(tag, "%s Supported presets:", prefix);
282 for (const char *s : traits.supported_preset_modes())
283 ESP_LOGCONFIG(tag, "%s - %s", prefix, s);
284 }
285}
286
287} // namespace fan
288} // 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.
constexpr const char * c_str() const
Definition string_ref.h:69
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
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< bool > oscillating_
Definition fan.h:86
FanCall & set_preset_mode(const std::string &preset_mode)
Definition fan.cpp:22
friend FanCall
Definition fan.h:138
void publish_state()
Definition fan.cpp:179
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
bool set_preset_mode_(const char *preset_mode)
Set the preset mode (finds and stores pointer from traits). Returns true if changed.
Definition fan.cpp:157
FanCall toggle()
Definition fan.cpp:146
LazyCallbackManager< void()> state_callback_
Definition fan.h:158
ESPPreferenceObject rtc_
Definition fan.h:159
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:176
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition fan.cpp:178
FanDirection direction
The current direction of the fan.
Definition fan.h:114
void save_state_()
Definition fan.cpp:237
FanRestoreMode restore_mode_
Definition fan.h:160
bool oscillating
The current oscillation state of the fan.
Definition fan.h:110
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
Definition fan.h:108
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
const char * get_preset_mode() const
Get the current preset mode (returns pointer to string stored in traits, or nullptr if not set)
Definition fan.h:132
int speed
The current fan speed level.
Definition fan.h:112
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:265
optional< FanRestoreState > restore_state_()
Definition fan.cpp:206
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
ClimatePreset preset
Definition climate.h:8
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:205
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:533
ESPPreferences * global_preferences
void apply(Fan &fan)
Apply these settings to the fan.
Definition fan.cpp:126
FanDirection direction
Definition fan.h:96
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
Definition fan.cpp:109