ESPHome  2024.11.0
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  FanCall &set_preset_mode(const std::string &preset_mode) {
76  this->preset_mode_ = 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 
90  optional<FanDirection> direction_{};
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 
107 class 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 
120  FanCall turn_on();
121  FanCall turn_off();
122  FanCall toggle();
123  FanCall make_call();
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 
140  optional<FanRestoreState> restore_state_();
141  void save_state_();
142 
143  void dump_traits_(const char *tag, const char *prefix);
144 
145  CallbackManager<void()> state_callback_{};
148 };
149 
150 } // namespace fan
151 } // namespace esphome
optional< bool > oscillating_
Definition: fan.h:88
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:89
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:87
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:133
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:147
FanCall & set_state(optional< bool > binary_state)
Definition: fan.h:45
ESPPreferenceObject rtc_
Definition: fan.h:146
FanCall & set_state(bool binary_state)
Definition: fan.h:41
FanCall & set_preset_mode(const std::string &preset_mode)
Definition: fan.h:75
bool oscillating
Definition: fan.h:36
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
FanDirection direction
Definition: fan.h:98
friend FanCall
Definition: fan.h:136
uint8_t preset_mode
Definition: fan.h:38
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
std::string get_preset_mode() const
Definition: fan.h:79
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