ESPHome  2023.5.4
fan.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/helpers.h"
5 #include "esphome/core/log.h"
8 #include "fan_traits.h"
9 
10 namespace esphome {
11 namespace 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 
20 enum class FanDirection { FORWARD = 0, REVERSE = 1 };
21 
23 enum class FanRestoreMode {
24  NO_RESTORE,
25  ALWAYS_OFF,
26  ALWAYS_ON,
31 };
32 
34 
35 class Fan;
36 
37 class 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  }
45  FanCall &set_state(optional<bool> binary_state) {
46  this->binary_state_ = binary_state;
47  return *this;
48  }
49  optional<bool> get_state() const { return this->binary_state_; }
51  this->oscillating_ = oscillating;
52  return *this;
53  }
55  this->oscillating_ = oscillating;
56  return *this;
57  }
58  optional<bool> get_oscillating() const { return this->oscillating_; }
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  }
71  this->direction_ = direction;
72  return *this;
73  }
74  optional<FanDirection> get_direction() const { return this->direction_; }
75 
76  void perform();
77 
78  protected:
79  void validate_();
80 
85  optional<FanDirection> direction_{};
86 };
87 
89  bool state;
90  int speed;
93 
95  FanCall to_call(Fan &fan);
97  void apply(Fan &fan);
98 } __attribute__((packed));
99 
100 class Fan : public EntityBase {
101  public:
103  bool state{false};
105  bool oscillating{false};
107  int speed{0};
110 
111  FanCall turn_on();
112  FanCall turn_off();
113  FanCall toggle();
114  FanCall make_call();
115 
117  void add_on_state_callback(std::function<void()> &&callback);
118 
119  void publish_state();
120 
121  virtual FanTraits get_traits() = 0;
122 
124  void set_restore_mode(FanRestoreMode restore_mode) { this->restore_mode_ = restore_mode; }
125 
126  protected:
127  friend FanCall;
128 
129  virtual void control(const FanCall &call) = 0;
130 
131  optional<FanRestoreState> restore_state_();
132  void save_state_();
133 
134  void dump_traits_(const char *tag, const char *prefix);
135 
136  CallbackManager<void()> state_callback_{};
139 };
140 
141 } // namespace fan
142 } // namespace esphome
optional< bool > oscillating_
Definition: fan.h:83
FanCall & set_direction(optional< FanDirection > direction)
Definition: fan.h:70
optional< FanDirection > get_direction() const
Definition: fan.h:74
optional< int > get_speed() const
Definition: fan.h:65
void apply(Fan &fan)
Apply these settings to the fan.
int speed
Definition: fan.h:35
FanCall to_call(Fan &fan)
Convert this struct to a fan call that can be performed.
FanRestoreMode
Restore mode of a fan.
Definition: fan.h:23
FanCall(Fan &parent)
Definition: fan.h:39
optional< int > speed_
Definition: fan.h:84
FanDirection direction
Definition: fan.h:37
FanCall & set_speed(int speed)
Definition: fan.h:59
esphome::fan::Fan __attribute__
optional< bool > binary_state_
Definition: fan.h:82
FanDirection
Simple enum to represent the direction of a fan.
Definition: fan.h:20
void set_restore_mode(FanRestoreMode restore_mode)
Set the restore mode of this fan.
Definition: fan.h:124
optional< bool > get_state() const
Definition: fan.h:49
optional< bool > get_oscillating() const
Definition: fan.h:58
FanCall & set_oscillating(bool oscillating)
Definition: fan.h:50
FanRestoreMode restore_mode_
Definition: fan.h:138
FanCall & set_state(optional< bool > binary_state)
Definition: fan.h:45
ESPPreferenceObject rtc_
Definition: fan.h:137
FanCall & set_state(bool binary_state)
Definition: fan.h:41
bool oscillating
Definition: fan.h:36
Definition: a4988.cpp:4
FanDirection direction
Definition: fan.h:92
friend FanCall
Definition: fan.h:127
const LogString * fan_direction_to_string(FanDirection direction)
Definition: fan.cpp:9
enum ESPDEPRECATED("LegacyFanDirection members are deprecated, use FanDirection instead.", "2022.2") LegacyFanDirection
Definition: fan_state.h:9
FanCall & set_direction(FanDirection direction)
Definition: fan.h:66
bool state
Definition: fan.h:34
FanCall & set_oscillating(optional< bool > oscillating)
Definition: fan.h:54