ESPHome  2022.9.1
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  void play(Ts... x) override {
58  // check to see if fan supports speeds and is on
59  if (this->state_->get_traits().supported_speed_count()) {
60  if (this->state_->state) {
61  int speed = this->state_->speed + 1;
62  int supported_speed_count = this->state_->get_traits().supported_speed_count();
63  if (speed > supported_speed_count) {
64  // was running at max speed, so turn off
65  speed = 1;
66  auto call = this->state_->turn_off();
67  call.set_speed(speed);
68  call.perform();
69  } else {
70  auto call = this->state_->turn_on();
71  call.set_speed(speed);
72  call.perform();
73  }
74  } else {
75  // fan was off, so set speed to 1
76  auto call = this->state_->turn_on();
77  call.set_speed(1);
78  call.perform();
79  }
80  } else {
81  // fan doesn't support speed counts, so toggle
82  this->state_->toggle().perform();
83  }
84  }
85 
87 };
88 
89 template<typename... Ts> class FanIsOnCondition : public Condition<Ts...> {
90  public:
91  explicit FanIsOnCondition(Fan *state) : state_(state) {}
92  bool check(Ts... x) override { return this->state_->state; }
93 
94  protected:
96 };
97 template<typename... Ts> class FanIsOffCondition : public Condition<Ts...> {
98  public:
99  explicit FanIsOffCondition(Fan *state) : state_(state) {}
100  bool check(Ts... x) override { return !this->state_->state; }
101 
102  protected:
104 };
105 
106 class FanTurnOnTrigger : public Trigger<> {
107  public:
109  state->add_on_state_callback([this, state]() {
110  auto is_on = state->state;
111  auto should_trigger = is_on && !this->last_on_;
112  this->last_on_ = is_on;
113  if (should_trigger) {
114  this->trigger();
115  }
116  });
117  this->last_on_ = state->state;
118  }
119 
120  protected:
121  bool last_on_;
122 };
123 
124 class FanTurnOffTrigger : public Trigger<> {
125  public:
127  state->add_on_state_callback([this, state]() {
128  auto is_on = state->state;
129  auto should_trigger = !is_on && this->last_on_;
130  this->last_on_ = is_on;
131  if (should_trigger) {
132  this->trigger();
133  }
134  });
135  this->last_on_ = state->state;
136  }
137 
138  protected:
139  bool last_on_;
140 };
141 
142 class FanSpeedSetTrigger : public Trigger<> {
143  public:
145  state->add_on_state_callback([this, state]() {
146  auto speed = state->speed;
147  auto should_trigger = speed != !this->last_speed_;
148  this->last_speed_ = speed;
149  if (should_trigger) {
150  this->trigger();
151  }
152  });
153  this->last_speed_ = state->speed;
154  }
155 
156  protected:
158 };
159 
160 } // namespace fan
161 } // namespace esphome
TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(int
bool state
The current on/off state of the fan.
Definition: fan.h:107
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:21
void add_on_state_callback(std::function< void()> &&callback)
Register a callback that will be called each time the state changes.
Definition: fan.cpp:87
FanDirection direction
Definition: fan.h:37
void play(Ts... x) override
Definition: automation.h:39
FanCall turn_off()
Definition: fan.cpp:83
FanCall & set_speed(int speed)
Definition: fan.h:59
Base class for all automation conditions.
Definition: automation.h:74
FanCall toggle()
Definition: fan.cpp:84
FanDirection
Simple enum to represent the direction of a fan.
Definition: fan.h:20
bool check(Ts... x) override
Definition: automation.h:100
int speed
The current fan speed level.
Definition: fan.h:111
FanCall & set_oscillating(bool oscillating)
Definition: fan.h:50
bool check(Ts... x) override
Definition: automation.h:92
bool oscillating
Definition: fan.h:36
Definition: a4988.cpp:4
virtual void play(Ts... x)=0
FanCall & set_direction(FanDirection direction)
Definition: fan.h:66
bool state
Definition: fan.h:34
void play(Ts... x) override
Definition: automation.h:57
FanCall turn_on()
Definition: fan.cpp:82