ESPHome 2025.5.0
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 ESPDEPRECATED("set_speed() with string argument is deprecated, use integer argument instead.", "2021.9")
64 FanCall &set_speed(const char *legacy_speed);
65 optional<int> get_speed() const { return this->speed_; }
67 this->direction_ = direction;
68 return *this;
69 }
75 FanCall &set_preset_mode(const std::string &preset_mode) {
77 return *this;
78 }
79 std::string get_preset_mode() const { return this->preset_mode_; }
80
81 void perform();
82
83 protected:
84 void validate_();
85
91 std::string preset_mode_{};
92};
93
95 bool state;
96 int speed;
99 uint8_t preset_mode;
100
102 FanCall to_call(Fan &fan);
104 void apply(Fan &fan);
105} __attribute__((packed));
106
107class Fan : public EntityBase {
108 public:
110 bool state{false};
112 bool oscillating{false};
114 int speed{0};
117 // The current preset mode of the fan
118 std::string preset_mode{};
119
122 FanCall toggle();
124
126 void add_on_state_callback(std::function<void()> &&callback);
127
128 void publish_state();
129
130 virtual FanTraits get_traits() = 0;
131
133 void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
134
135 protected:
136 friend FanCall;
137
138 virtual void control(const FanCall &call) = 0;
139
141 void save_state_();
142
143 void dump_traits_(const char *tag, const char *prefix);
144
149
150} // namespace fan
151} // namespace esphome
FanCall & set_oscillating(bool oscillating)
Definition fan.h:50
FanCall & set_direction(FanDirection direction)
Definition fan.h:66
optional< bool > binary_state_
Definition fan.h:87
optional< FanDirection > direction_
Definition fan.h:90
FanCall & set_direction(optional< FanDirection > direction)
Definition fan.h:70
optional< bool > get_state() const
Definition fan.h:49
optional< int > speed_
Definition fan.h:89
ESPDEPRECATED("set_speed() with string argument is deprecated, use integer argument instead.", "2021.9") FanCall &set_speed(const char *legacy_speed)
std::string preset_mode_
Definition fan.h:91
optional< bool > get_oscillating() const
Definition fan.h:58
FanCall & set_state(optional< bool > binary_state)
Definition fan.h:45
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:65
optional< bool > oscillating_
Definition fan.h:88
FanCall & set_preset_mode(const std::string &preset_mode)
Definition fan.h:75
optional< FanDirection > get_direction() const
Definition fan.h:74
std::string get_preset_mode() const
Definition fan.h:79
friend FanCall
Definition fan.h:136
void publish_state()
Definition fan.cpp:117
FanCall turn_on()
Definition fan.cpp:111
FanCall turn_off()
Definition fan.cpp:112
FanCall make_call()
Definition fan.cpp:114
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:113
ESPPreferenceObject rtc_
Definition fan.h:146
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition fan.cpp:116
FanDirection direction
The current direction of the fan.
Definition fan.h:116
void save_state_()
Definition fan.cpp:170
FanRestoreMode restore_mode_
Definition fan.h:147
std::string preset_mode
Definition fan.h:118
CallbackManager< void()> state_callback_
Definition fan.h:145
bool oscillating
The current oscillation state of the fan.
Definition fan.h:112
virtual void control(const FanCall &call)=0
bool state
The current on/off state of the fan.
Definition fan.h:110
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition fan.h:133
int speed
The current fan speed level.
Definition fan.h:114
void dump_traits_(const char *tag, const char *prefix)
Definition fan.cpp:188
optional< FanRestoreState > restore_state_()
Definition fan.cpp:140
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:9
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:95
FanDirection direction
Definition fan.h:98
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
Definition fan.cpp:79