ESPHome 2025.12.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 const char *get_preset_mode() const { return this->preset_mode_; }
76 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
77
78 void perform();
79
80 protected:
81 void validate_();
82
88 const char *preset_mode_{nullptr}; // Pointer to string in traits (after validation)
89};
90
92 bool state;
93 int speed;
96 uint8_t preset_mode;
97
99 FanCall to_call(Fan &fan);
101 void apply(Fan &fan);
102} __attribute__((packed));
103
104class Fan : public EntityBase {
105 public:
107 bool state{false};
109 bool oscillating{false};
111 int speed{0};
114
117 FanCall toggle();
119
121 void add_on_state_callback(std::function<void()> &&callback);
122
123 void publish_state();
124
125 virtual FanTraits get_traits() = 0;
126
128 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
129
131 const char *get_preset_mode() const { return this->preset_mode_; }
132
134 bool has_preset_mode() const { return this->preset_mode_ != nullptr; }
135
136 protected:
137 friend FanCall;
138 friend struct FanRestoreState;
139
140 virtual void control(const FanCall &call) = 0;
141
143 void save_state_();
144
145 void dump_traits_(const char *tag, const char *prefix);
146
148 bool set_preset_mode_(const char *preset_mode);
150 bool set_preset_mode_(const std::string &preset_mode);
152 void clear_preset_mode_();
154 const char *find_preset_mode_(const char *preset_mode);
155
159
160 private:
161 const char *preset_mode_{nullptr};
163
164} // namespace fan
165} // 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:84
optional< FanDirection > direction_
Definition fan.h:87
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:86
const char * preset_mode_
Definition fan.h:88
const char * get_preset_mode() const
Definition fan.h:75
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:76
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:85
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:137
void publish_state()
Definition fan.cpp:167
FanCall turn_on()
Definition fan.cpp:138
FanCall turn_off()
Definition fan.cpp:139
FanCall make_call()
Definition fan.cpp:141
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:145
FanCall toggle()
Definition fan.cpp:140
ESPPreferenceObject rtc_
Definition fan.h:157
void clear_preset_mode_()
Clear the preset mode.
Definition fan.cpp:164
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition fan.cpp:166
FanDirection direction
The current direction of the fan.
Definition fan.h:113
void save_state_()
Definition fan.cpp:225
FanRestoreMode restore_mode_
Definition fan.h:158
CallbackManager< void()> state_callback_
Definition fan.h:156
bool oscillating
The current oscillation state of the fan.
Definition fan.h:109
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
Definition fan.h:107
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:143
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:128
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:131
bool has_preset_mode() const
Check if a preset mode is currently active.
Definition fan.h:134
int speed
The current fan speed level.
Definition fan.h:111
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:253
optional< FanRestoreState > restore_state_()
Definition fan.cpp:194
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
void apply(Fan &fan)
Apply these settings to the fan.
Definition fan.cpp:120
FanDirection direction
Definition fan.h:95
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
Definition fan.cpp:103