ESPHome 2026.1.4
Loading...
Searching...
No Matches
fan.h
Go to the documentation of this file.
1#pragma once
5#include "esphome/core/log.h"
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 bool state;
95 int speed;
98 uint8_t preset_mode;
99
101 FanCall to_call(Fan &fan);
103 void apply(Fan &fan);
104} __attribute__((packed));
105
106class Fan : public EntityBase {
107 public:
109 bool state{false};
111 bool oscillating{false};
113 int speed{0};
116
119 FanCall toggle();
121
123 void add_on_state_callback(std::function<void()> &&callback);
124
125 void publish_state();
126
127 virtual FanTraits get_traits() = 0;
128
130 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
131
136 StringRef get_preset_mode() const { return StringRef::from_maybe_nullptr(this->preset_mode_); }
137
139 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
140
141 protected:
142 friend FanCall;
143 friend struct FanRestoreState;
144
145 virtual void control(const FanCall &call) = 0;
146
148 void save_state_();
149
150 void dump_traits_(const char *tag, const char *prefix);
151
154 bool set_preset_mode_(const char *preset_mode, size_t len);
155 bool set_preset_mode_(const char *preset_mode);
156 bool set_preset_mode_(const std::string &preset_mode);
159 void clear_preset_mode_();
161 void apply_preset_mode_(const FanCall &call);
163 const char *find_preset_mode_(const char *preset_mode);
164 const char *find_preset_mode_(const char *preset_mode, size_t len);
165
169
170 private:
171 const char *preset_mode_{nullptr};
173
174} // namespace fan
175} // 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:22
optional< FanDirection > get_direction() const
Definition fan.h:73
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
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:136
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
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:130
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
FanDirection direction
Definition fan.h:3
bool oscillating
Definition fan.h:2
uint8_t preset_mode
Definition fan.h:4
FanRestoreMode
Restore mode of a fan.
Definition fan.h:24
const LogString * fan_direction_to_string(FanDirection direction)
Definition fan.cpp:11
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:595
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