ESPHome 2026.1.0-dev
Loading...
Searching...
No Matches
fan.h
Go to the documentation of this file.
1#pragma once
5#include "esphome/core/log.h"
8#include "fan_traits.h"
10namespace esphome {
11namespace fan {
12
13#define LOG_FAN(prefix, type, obj) \
14 if ((obj) != nullptr) { \
15 ESP_LOGCONFIG(TAG, "%s%s '%s'", prefix, LOG_STR_LITERAL(type), (obj)->get_name().c_str()); \
16 (obj)->dump_traits_(TAG, prefix); \
17 }
18
20enum class FanDirection { FORWARD = 0, REVERSE = 1 };
21
32
34
35class Fan;
36
37class FanCall {
38 public:
39 explicit FanCall(Fan &parent) : parent_(parent) {}
40
41 FanCall &set_state(bool binary_state) {
42 this->binary_state_ = binary_state;
43 return *this;
44 }
46 this->binary_state_ = binary_state;
47 return *this;
48 }
49 optional<bool> get_state() const { return this->binary_state_; }
52 return *this;
53 }
59 FanCall &set_speed(int speed) {
60 this->speed_ = speed;
61 return *this;
62 }
63 optional<int> get_speed() const { return this->speed_; }
65 this->direction_ = direction;
66 return *this;
67 }
73 FanCall &set_preset_mode(const std::string &preset_mode);
75 FanCall &set_preset_mode(const char *preset_mode, size_t len);
76 const char *get_preset_mode() const { return this->preset_mode_; }
77 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
78
79 void perform();
80
81 protected:
82 void validate_();
83
89 const char *preset_mode_{nullptr}; // Pointer to string in traits (after validation)
90};
91
93 bool state;
94 int speed;
97 uint8_t preset_mode;
98
100 FanCall to_call(Fan &fan);
102 void apply(Fan &fan);
103} __attribute__((packed));
104
105class Fan : public EntityBase {
106 public:
108 bool state{false};
110 bool oscillating{false};
112 int speed{0};
115
118 FanCall toggle();
120
122 void add_on_state_callback(std::function<void()> &&callback);
123
124 void publish_state();
125
126 virtual FanTraits get_traits() = 0;
127
129 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
130
132 const char *get_preset_mode() const { return this->preset_mode_; }
133
135 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
136
137 protected:
138 friend FanCall;
139 friend struct FanRestoreState;
140
141 virtual void control(const FanCall &call) = 0;
142
144 void save_state_();
145
146 void dump_traits_(const char *tag, const char *prefix);
147
149 bool set_preset_mode_(const char *preset_mode);
151 bool set_preset_mode_(const std::string &preset_mode);
153 void clear_preset_mode_();
155 const char *find_preset_mode_(const char *preset_mode);
156 const char *find_preset_mode_(const char *preset_mode, size_t len);
157
161
162 private:
163 const char *preset_mode_{nullptr};
165
166} // namespace fan
167} // namespace esphome
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
FanCall & set_direction(optional< FanDirection > direction)
Definition fan.h:68
optional< bool > get_state() const
Definition fan.h:49
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
optional< bool > get_oscillating() const
Definition fan.h:58
FanCall & set_state(optional< bool > binary_state)
Definition fan.h:45
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
FanCall(Fan &parent)
Definition fan.h:39
FanCall & set_oscillating(optional< bool > oscillating)
Definition fan.h:54
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:22
optional< FanDirection > get_direction() const
Definition fan.h:72
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
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:129
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
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:135
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
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:23
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: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
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