ESPHome 2025.5.0
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "fan_state.h"
6
7namespace esphome {
8namespace fan {
9
10template<typename... Ts> class TurnOnAction : public Action<Ts...> {
11 public:
13
15 TEMPLATABLE_VALUE(int, speed)
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
35template<typename... Ts> class TurnOffAction : public Action<Ts...> {
36 public:
38
39 void play(Ts... x) override { this->state_->turn_off().perform(); }
40
42};
43
44template<typename... Ts> class ToggleAction : public Action<Ts...> {
45 public:
47
48 void play(Ts... x) override { this->state_->toggle().perform(); }
49
51};
52
53template<typename... Ts> class CycleSpeedAction : public Action<Ts...> {
54 public:
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
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
97template<typename... Ts> class FanIsOnCondition : public Condition<Ts...> {
98 public:
100 bool check(Ts... x) override { return this->state_->state; }
101
102 protected:
104};
105template<typename... Ts> class FanIsOffCondition : public Condition<Ts...> {
106 public:
108 bool check(Ts... x) override { return !this->state_->state; }
109
110 protected:
112};
113
114class FanStateTrigger : public Trigger<Fan *> {
115 public:
117 state->add_on_state_callback([this, state]() { this->trigger(state); });
118 }
119};
120
121class 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:
137};
138
139class 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:
155};
156
157class 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_;
164 if (should_trigger) {
165 this->trigger(direction);
166 }
167 });
168 this->last_direction_ = state->direction;
169 }
170
171 protected:
173};
174
175class 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_;
182 if (should_trigger) {
183 this->trigger(oscillating);
184 }
185 });
186 this->last_oscillating_ = state->oscillating;
187 }
188
189 protected:
191};
192
193class 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
211class 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_;
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
virtual void play(Ts... x)=0
Base class for all automation conditions.
Definition automation.h:75
TEMPLATABLE_VALUE(bool, no_off_cycle) void play(Ts... x) override
Definition automation.h:57
FanCall & set_speed(int speed)
Definition fan.h:59
FanCall turn_on()
Definition fan.cpp:111
FanCall turn_off()
Definition fan.cpp:112
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:113
FanDirection direction
The current direction of the fan.
Definition fan.h:116
std::string preset_mode
Definition fan.h:118
bool oscillating
The current oscillation state of the fan.
Definition fan.h:112
bool state
The current on/off state of the fan.
Definition fan.h:110
int speed
The current fan speed level.
Definition fan.h:114
bool check(Ts... x) override
Definition automation.h:108
bool check(Ts... x) override
Definition automation.h:100
int supported_speed_count() const
Return how many speed levels the fan has.
Definition fan_traits.h:24
void play(Ts... x) override
Definition automation.h:48
void play(Ts... x) override
Definition automation.h:39
TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(int
FanDirection direction
Definition fan.h:3
int speed
Definition fan.h:1
bool oscillating
Definition fan.h:2
uint8_t preset_mode
Definition fan.h:4
bool state
Definition fan.h:0
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:20
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5