ESPHome 2026.2.3
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 }
47 this->binary_state_ = binary_state;
48 return *this;
49 }
50 optional<bool> get_state() const { return this->binary_state_; }
53 return *this;
54 }
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 }
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
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 void add_on_state_callback(std::function<void()> &&callback);
126
127 void publish_state();
128
129 virtual FanTraits get_traits() = 0;
130
132 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
133
138 StringRef get_preset_mode() const { return StringRef::from_maybe_nullptr(this->preset_mode_); }
139
141 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
142
143 protected:
144 friend FanCall;
145 friend struct FanRestoreState;
146
147 virtual void control(const FanCall &call) = 0;
148
150 void save_state_();
151
152 void dump_traits_(const char *tag, const char *prefix);
153
156 bool set_preset_mode_(const char *preset_mode, size_t len);
157 bool set_preset_mode_(const char *preset_mode);
158 bool set_preset_mode_(const std::string &preset_mode);
161 void clear_preset_mode_();
163 void apply_preset_mode_(const FanCall &call);
165 const char *find_preset_mode_(const char *preset_mode);
166 const char *find_preset_mode_(const char *preset_mode, size_t len);
167
171
172 private:
173 const char *preset_mode_{nullptr};
175
176} // namespace fan
177} // 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:19
optional< FanDirection > get_direction() const
Definition fan.h:73
friend FanCall
Definition fan.h:144
void publish_state()
Definition fan.cpp:197
FanCall turn_on()
Definition fan.cpp:141
FanCall turn_off()
Definition fan.cpp:142
FanCall make_call()
Definition fan.cpp:144
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:143
LazyCallbackManager< void()> state_callback_
Definition fan.h:168
void apply_preset_mode_(const FanCall &call)
Apply preset mode from a FanCall (handles speed-clears-preset convention)
Definition fan.cpp:187
ESPPreferenceObject rtc_
Definition fan.h:169
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:185
bool set_preset_mode_(const char *preset_mode, size_t len)
Set the preset mode (finds and stores pointer from traits).
Definition fan.cpp:154
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:138
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition fan.cpp:196
FanDirection direction
The current direction of the fan.
Definition fan.h:117
void save_state_()
Definition fan.cpp:260
FanRestoreMode restore_mode_
Definition fan.h:170
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:146
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:132
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:141
int speed
The current fan speed level.
Definition fan.h:115
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:288
optional< FanRestoreState > restore_state_()
Definition fan.cpp:225
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:15
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
std::string size_t len
Definition helpers.h:692
static constexpr uint8_t NO_PRESET
Definition fan.h:94
void apply(Fan &fan)
Apply these settings to the fan.
Definition fan.cpp:123
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:106