ESPHome  2024.2.0
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 FanTurnOnTrigger : public Trigger<> {
115  public:
117  state->add_on_state_callback([this, state]() {
118  auto is_on = state->state;
119  auto should_trigger = is_on && !this->last_on_;
120  this->last_on_ = is_on;
121  if (should_trigger) {
122  this->trigger();
123  }
124  });
125  this->last_on_ = state->state;
126  }
127 
128  protected:
129  bool last_on_;
130 };
131 
132 class FanTurnOffTrigger : public Trigger<> {
133  public:
135  state->add_on_state_callback([this, state]() {
136  auto is_on = state->state;
137  auto should_trigger = !is_on && this->last_on_;
138  this->last_on_ = is_on;
139  if (should_trigger) {
140  this->trigger();
141  }
142  });
143  this->last_on_ = state->state;
144  }
145 
146  protected:
147  bool last_on_;
148 };
149 
150 class FanSpeedSetTrigger : public Trigger<> {
151  public:
153  state->add_on_state_callback([this, state]() {
154  auto speed = state->speed;
155  auto should_trigger = speed != !this->last_speed_;
156  this->last_speed_ = speed;
157  if (should_trigger) {
158  this->trigger();
159  }
160  });
161  this->last_speed_ = state->speed;
162  }
163 
164  protected:
166 };
167 
168 class FanPresetSetTrigger : public Trigger<> {
169  public:
171  state->add_on_state_callback([this, state]() {
172  auto preset_mode = state->preset_mode;
173  auto should_trigger = preset_mode != this->last_preset_mode_;
174  this->last_preset_mode_ = preset_mode;
175  if (should_trigger) {
176  this->trigger();
177  }
178  });
179  this->last_preset_mode_ = state->preset_mode;
180  }
181 
182  protected:
183  std::string last_preset_mode_;
184 };
185 
186 } // namespace fan
187 } // namespace esphome
TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(int
bool state
The current on/off state of the fan.
Definition: fan.h:110
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
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
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