ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
fan.h
Go to the documentation of this file.
1#pragma once
3#include <vector>
7#include "esphome/core/log.h"
11#include "fan_traits.h"
12
13namespace esphome::fan {
14
15#define LOG_FAN(prefix, type, obj) \
16 if (log_entity_header(TAG, prefix, LOG_STR_LITERAL(type), (obj))) { \
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
119 FanCall turn_on() { return this->make_call().set_state(true); }
120 FanCall turn_off() { return this->make_call().set_state(false); }
121 FanCall toggle() { return this->make_call().set_state(!this->state); }
122 FanCall make_call() { return FanCall(*this); }
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->supported_preset_modes_.assign_copy(preset_modes.begin(), preset_modes.size());
136 }
137 void set_supported_preset_modes(const std::vector<const char *> &preset_modes) {
138 this->supported_preset_modes_.assign_copy(preset_modes.data(), preset_modes.size());
139 }
140 void set_supported_preset_modes(const FanPresetModes &preset_modes) {
141 this->supported_preset_modes_.assign_copy(preset_modes.data(), preset_modes.size());
142 }
144 void set_supported_preset_modes_static(const char *const *preset_modes, size_t count) {
145 this->supported_preset_modes_.assign_static(preset_modes, count);
146 }
147
149 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
150
155 StringRef get_preset_mode() const { return StringRef::from_maybe_nullptr(this->preset_mode_); }
156
158 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
159
160 protected:
161 friend FanCall;
162 friend struct FanRestoreState;
163
164 virtual void control(const FanCall &call) = 0;
165
166 optional<FanRestoreState> restore_state_();
167 void save_state_();
168
169 void dump_traits_(const char *tag, const char *prefix);
170
173 bool set_preset_mode_(const char *preset_mode, size_t len);
174 bool set_preset_mode_(const char *preset_mode);
175 bool set_preset_mode_(const std::string &preset_mode);
178 void clear_preset_mode_();
180 void apply_preset_mode_(const FanCall &call);
182 const char *find_preset_mode_(const char *preset_mode);
183 const char *find_preset_mode_(const char *preset_mode, size_t len);
184
186 void wire_preset_modes_(FanTraits &traits) { traits.set_supported_preset_modes_(&this->supported_preset_modes_); }
187
191
192 private:
193 FanPresetModes supported_preset_modes_;
194 const char *preset_mode_{nullptr};
196
197} // namespace esphome::fan
const T * data() const
Definition helpers.h:139
constexpr size_t size() const
Definition helpers.h:140
StringRef is a reference to a string owned by something else.
Definition string_ref.h:31
static StringRef from_maybe_nullptr(const char *s)
Definition string_ref.h:58
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:24
optional< FanDirection > get_direction() const
Definition fan.h:73
friend FanCall
Definition fan.h:161
void set_supported_preset_modes(std::initializer_list< const char * > preset_modes)
Set the supported preset modes; the list is copied (stored on Fan, referenced by FanTraits).
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:205
FanCall turn_on()
Definition fan.h:119
FanCall turn_off()
Definition fan.h:120
void set_supported_preset_modes(const std::vector< const char * > &preset_modes)
Definition fan.h:137
FanCall make_call()
Definition fan.h:122
virtual FanTraits get_traits()=0
void set_supported_preset_modes(const FanPresetModes &preset_modes)
Definition fan.h:140
FanCall toggle()
Definition fan.h:121
LazyCallbackManager< void()> state_callback_
Definition fan.h:188
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:196
ESPPreferenceObject rtc_
Definition fan.h:189
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:194
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:163
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:155
FanDirection direction
The current direction of the fan.
Definition fan.h:117
void save_state_()
Definition fan.cpp:268
void set_supported_preset_modes_static(const char *const *preset_modes, size_t count)
Points at a static table that outlives the fan (codegen); call before any runtime copy.
Definition fan.h:144
FanRestoreMode restore_mode_
Definition fan.h:190
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:147
void wire_preset_modes_(FanTraits &traits)
Wire the Fan-owned preset modes pointer into the given traits object.
Definition fan.h:186
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:149
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:158
int speed
The current fan speed level.
Definition fan.h:115
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:292
optional< FanRestoreState > restore_state_()
Definition fan.cpp:233
void set_supported_preset_modes_(const FanPresetModes *preset_modes)
Set the preset modes pointer (only Fan::wire_preset_modes_() should call this).
Definition fan_traits.h:61
FanDirection direction
Definition fan.h:5
bool oscillating
Definition fan.h:4
uint8_t preset_mode
Definition fan.h:6
unsigned count
Definition gif.cpp:51
FanRestoreMode
Restore mode of a fan.
Definition fan.h:24
const LogString * fan_direction_to_string(FanDirection direction)
Definition fan.cpp:20
esphome::fan::Fan __attribute__
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:21
const void size_t len
Definition hal.h:68
static constexpr uint8_t NO_PRESET
Definition fan.h:94
void apply(Fan &fan)
Apply these settings to the fan.
Definition fan.cpp:129
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:112