ESPHome  2024.10.2
automation.h
Go to the documentation of this file.
1 #pragma once
2 
5 #include "fan_state.h"
6 
7 namespace esphome {
8 namespace fan {
9 
10 template<typename... Ts> class TurnOnAction : public Action<Ts...> {
11  public:
12  explicit TurnOnAction(Fan *state) : state_(state) {}
13 
17 
18  void play(Ts... x) override {
19  auto call = this->state_->turn_on();
20  if (this->oscillating_.has_value()) {
21  call.set_oscillating(this->oscillating_.value(x...));
22  }
23  if (this->speed_.has_value()) {
24  call.set_speed(this->speed_.value(x...));
25  }
26  if (this->direction_.has_value()) {
27  call.set_direction(this->direction_.value(x...));
28  }
29  call.perform();
30  }
31 
33 };
34 
35 template<typename... Ts> class TurnOffAction : public Action<Ts...> {
36  public:
37  explicit TurnOffAction(Fan *state) : state_(state) {}
38 
39  void play(Ts... x) override { this->state_->turn_off().perform(); }
40 
42 };
43 
44 template<typename... Ts> class ToggleAction : public Action<Ts...> {
45  public:
46  explicit ToggleAction(Fan *state) : state_(state) {}
47 
48  void play(Ts... x) override { this->state_->toggle().perform(); }
49 
51 };
52 
53 template<typename... Ts> class CycleSpeedAction : public Action<Ts...> {
54  public:
55  explicit CycleSpeedAction(Fan *state) : state_(state) {}
56 
57  TEMPLATABLE_VALUE(bool, no_off_cycle)
58 
59  void play(Ts... x) override {
60  // check to see if fan supports speeds and is on
61  if (this->state_->get_traits().supported_speed_count()) {
62  if (this->state_->state) {
63  int speed = this->state_->speed + 1;
64  int supported_speed_count = this->state_->get_traits().supported_speed_count();
65  bool off_speed_cycle = no_off_cycle_.value(x...);
66  if (speed > supported_speed_count && off_speed_cycle) {
67  // was running at max speed, off speed cycle enabled, so turn off
68  speed = 1;
69  auto call = this->state_->turn_off();
70  call.set_speed(speed);
71  call.perform();
72  } else if (speed > supported_speed_count && !off_speed_cycle) {
73  // was running at max speed, off speed cycle disabled, so set to lowest speed
74  auto call = this->state_->turn_on();
75  call.set_speed(1);
76  call.perform();
77  } else {
78  auto call = this->state_->turn_on();
79  call.set_speed(speed);
80  call.perform();
81  }
82  } else {
83  // fan was off, so set speed to 1
84  auto call = this->state_->turn_on();
85  call.set_speed(1);
86  call.perform();
87  }
88  } else {
89  // fan doesn't support speed counts, so toggle
90  this->state_->toggle().perform();
91  }
92  }
93 
95 };
96 
97 template<typename... Ts> class FanIsOnCondition : public Condition<Ts...> {
98  public:
99  explicit FanIsOnCondition(Fan *state) : state_(state) {}
100  bool check(Ts... x) override { return this->state_->state; }
101 
102  protected:
104 };
105 template<typename... Ts> class FanIsOffCondition : public Condition<Ts...> {
106  public:
107  explicit FanIsOffCondition(Fan *state) : state_(state) {}
108  bool check(Ts... x) override { return !this->state_->state; }
109 
110  protected:
112 };
113 
114 class FanStateTrigger : public Trigger<Fan *> {
115  public:
117  state->add_on_state_callback([this, state]() { this->trigger(state); });
118  }
119 };
120 
121 class FanTurnOnTrigger : public Trigger<> {
122  public:
124  state->add_on_state_callback([this, state]() {
125  auto is_on = state->state;
126  auto should_trigger = is_on && !this->last_on_;
127  this->last_on_ = is_on;
128  if (should_trigger) {
129  this->trigger();
130  }
131  });
132  this->last_on_ = state->state;
133  }
134 
135  protected:
136  bool last_on_;
137 };
138 
139 class FanTurnOffTrigger : public Trigger<> {
140  public:
142  state->add_on_state_callback([this, state]() {
143  auto is_on = state->state;
144  auto should_trigger = !is_on && this->last_on_;
145  this->last_on_ = is_on;
146  if (should_trigger) {
147  this->trigger();
148  }
149  });
150  this->last_on_ = state->state;
151  }
152 
153  protected:
154  bool last_on_;
155 };
156 
157 class FanDirectionSetTrigger : public Trigger<FanDirection> {
158  public:
160  state->add_on_state_callback([this, state]() {
161  auto direction = state->direction;
162  auto should_trigger = direction != this->last_direction_;
163  this->last_direction_ = direction;
164  if (should_trigger) {
165  this->trigger(direction);
166  }
167  });
168  this->last_direction_ = state->direction;
169  }
170 
171  protected:
173 };
174 
175 class FanOscillatingSetTrigger : public Trigger<bool> {
176  public:
178  state->add_on_state_callback([this, state]() {
179  auto oscillating = state->oscillating;
180  auto should_trigger = oscillating != this->last_oscillating_;
181  this->last_oscillating_ = oscillating;
182  if (should_trigger) {
183  this->trigger(oscillating);
184  }
185  });
186  this->last_oscillating_ = state->oscillating;
187  }
188 
189  protected:
191 };
192 
193 class FanSpeedSetTrigger : public Trigger<int> {
194  public:
196  state->add_on_state_callback([this, state]() {
197  auto speed = state->speed;
198  auto should_trigger = speed != this->last_speed_;
199  this->last_speed_ = speed;
200  if (should_trigger) {
201  this->trigger(speed);
202  }
203  });
204  this->last_speed_ = state->speed;
205  }
206 
207  protected:
209 };
210 
211 class FanPresetSetTrigger : public Trigger<std::string> {
212  public:
214  state->add_on_state_callback([this, state]() {
215  auto preset_mode = state->preset_mode;
216  auto should_trigger = preset_mode != this->last_preset_mode_;
217  this->last_preset_mode_ = preset_mode;
218  if (should_trigger) {
219  this->trigger(preset_mode);
220  }
221  });
222  this->last_preset_mode_ = state->preset_mode;
223  }
224 
225  protected:
226  std::string last_preset_mode_;
227 };
228 
229 } // namespace fan
230 } // namespace esphome
TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(int
bool state
The current on/off state of the fan.
Definition: fan.h:110
bool oscillating
The current oscillation state of the fan.
Definition: fan.h:112
FanDirection direction
The current direction of the fan.
Definition: fan.h:116
uint16_t x
Definition: tt21100.cpp:17
virtual FanTraits get_traits()=0
void play(Ts... x) override
Definition: automation.h:48
int speed
Definition: fan.h:35
int supported_speed_count() const
Return how many speed levels the fan has.
Definition: fan_traits.h:24
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
TEMPLATABLE_VALUE(bool, no_off_cycle) void play(Ts... x) override
Definition: automation.h:57
FanDirection direction
Definition: fan.h:37
void play(Ts... x) override
Definition: automation.h:39
FanCall turn_off()
Definition: fan.cpp:112
FanCall & set_speed(int speed)
Definition: fan.h:59
std::string preset_mode
Definition: fan.h:118
Base class for all automation conditions.
Definition: automation.h:74
FanCall toggle()
Definition: fan.cpp:113
FanDirection
Simple enum to represent the direction of a fan.
Definition: fan.h:20
bool check(Ts... x) override
Definition: automation.h:108
int speed
The current fan speed level.
Definition: fan.h:114
FanCall & set_oscillating(bool oscillating)
Definition: fan.h:50
bool check(Ts... x) override
Definition: automation.h:100
bool oscillating
Definition: fan.h:36
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint8_t preset_mode
Definition: fan.h:38
virtual void play(Ts... x)=0
FanCall & set_direction(FanDirection direction)
Definition: fan.h:66
bool state
Definition: fan.h:34
FanCall turn_on()
Definition: fan.cpp:111