ESPHome 2026.5.0-dev
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "fan.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(const 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(const 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(const 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(const 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(const Ts &...x) override { return this->state_->state; }
101
102 protected:
104};
105template<typename... Ts> class FanIsOffCondition : public Condition<Ts...> {
106 public:
108 bool check(const 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]() { this->trigger(this->fan_); });
118 }
119
120 protected:
122};
123
124class FanTurnOnTrigger : public Trigger<> {
125 public:
127 state->add_on_state_callback([this]() {
128 auto is_on = this->fan_->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:
141};
142
143class FanTurnOffTrigger : public Trigger<> {
144 public:
146 state->add_on_state_callback([this]() {
147 auto is_on = this->fan_->state;
148 auto should_trigger = !is_on && this->last_on_;
149 this->last_on_ = is_on;
150 if (should_trigger) {
151 this->trigger();
152 }
153 });
154 this->last_on_ = state->state;
155 }
156
157 protected:
160};
161
162class FanDirectionSetTrigger : public Trigger<FanDirection> {
163 public:
165 state->add_on_state_callback([this]() {
166 auto direction = this->fan_->direction;
167 auto should_trigger = direction != this->last_direction_;
169 if (should_trigger) {
170 this->trigger(direction);
171 }
172 });
173 this->last_direction_ = state->direction;
174 }
175
176 protected:
179};
180
181class FanOscillatingSetTrigger : public Trigger<bool> {
182 public:
184 state->add_on_state_callback([this]() {
185 auto oscillating = this->fan_->oscillating;
186 auto should_trigger = oscillating != this->last_oscillating_;
188 if (should_trigger) {
189 this->trigger(oscillating);
190 }
191 });
192 this->last_oscillating_ = state->oscillating;
193 }
194
195 protected:
198};
199
200class FanSpeedSetTrigger : public Trigger<int> {
201 public:
203 state->add_on_state_callback([this]() {
204 auto speed = this->fan_->speed;
205 auto should_trigger = speed != this->last_speed_;
206 this->last_speed_ = speed;
207 if (should_trigger) {
208 this->trigger(speed);
209 }
210 });
211 this->last_speed_ = state->speed;
212 }
213
214 protected:
217};
218
219class FanPresetSetTrigger : public Trigger<StringRef> {
220 public:
222 state->add_on_state_callback([this]() {
223 auto preset_mode = this->fan_->get_preset_mode();
224 auto should_trigger = preset_mode != this->last_preset_mode_;
226 if (should_trigger) {
227 this->trigger(preset_mode);
228 }
229 });
230 this->last_preset_mode_ = state->get_preset_mode();
231 }
232
233 protected:
236};
237
238} // namespace fan
239} // namespace esphome
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:459
StringRef is a reference to a string owned by something else.
Definition string_ref.h:26
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:482
TEMPLATABLE_VALUE(bool, no_off_cycle) void play(const Ts &...x) override
Definition automation.h:57
FanCall & set_speed(int speed)
Definition fan.h:60
void add_on_state_callback(F &&callback)
Register a callback that will be called each time the state changes.
Definition fan.h:125
FanCall turn_on()
Definition fan.cpp:157
FanCall turn_off()
Definition fan.cpp:158
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.cpp:159
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:148
FanDirection direction
The current direction of the fan.
Definition fan.h:117
bool oscillating
The current oscillation state of the fan.
Definition fan.h:113
bool state
The current on/off state of the fan.
Definition fan.h:111
int speed
The current fan speed level.
Definition fan.h:115
bool check(const Ts &...x) override
Definition automation.h:108
bool check(const Ts &...x) override
Definition automation.h:100
int supported_speed_count() const
Return how many speed levels the fan has.
Definition fan_traits.h:31
void play(const Ts &...x) override
Definition automation.h:48
void play(const Ts &...x) override
Definition automation.h:39
TEMPLATABLE_VALUE(bool, oscillating) TEMPLATABLE_VALUE(int
FanDirection direction
Definition fan.h:5
int speed
Definition fan.h:3
bool oscillating
Definition fan.h:4
uint8_t preset_mode
Definition fan.h:6
bool state
Definition fan.h:2
FanDirection
Simple enum to represent the direction of a fan.
Definition fan.h:21
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
if(written< 0)
Definition helpers.h:1091
uint16_t x
Definition tt21100.cpp:5