ESPHome 2026.10.2
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
5#include "fan.h"
6
7namespace esphome::fan {
8
9template<typename... Ts> class CycleSpeedAction final : public Action<Ts...> {
10 public:
12
13 TEMPLATABLE_VALUE(bool, no_off_cycle)
14
15 void play(const Ts &...x) override {
16 // check to see if fan supports speeds and is on
18 if (this->state_->state) {
19 int speed = this->state_->speed + 1;
20 int supported_speed_count = this->state_->get_traits().supported_speed_count();
21 bool off_speed_cycle = no_off_cycle_.value(x...);
22 if (speed > supported_speed_count && off_speed_cycle) {
23 // was running at max speed, off speed cycle enabled, so turn off
24 speed = 1;
25 auto call = this->state_->turn_off();
26 call.set_speed(speed);
27 call.perform();
28 } else if (speed > supported_speed_count && !off_speed_cycle) {
29 // was running at max speed, off speed cycle disabled, so set to lowest speed
30 auto call = this->state_->turn_on();
31 call.set_speed(1);
32 call.perform();
33 } else {
34 auto call = this->state_->turn_on();
35 call.set_speed(speed);
36 call.perform();
37 }
38 } else {
39 // fan was off, so set speed to 1
40 auto call = this->state_->turn_on();
41 call.set_speed(1);
42 call.perform();
43 }
44 } else {
45 // fan doesn't support speed counts, so toggle
46 this->state_->toggle().perform();
47 }
48 }
49
51};
52
53class FanStateTrigger final : public Trigger<Fan *> {
54 public:
56 state->add_on_state_callback([this]() { this->trigger(this->fan_); });
57 }
58
59 protected:
61};
62
63class FanTurnOnTrigger final : public Trigger<> {
64 public:
67 auto is_on = this->fan_->state;
68 auto should_trigger = is_on && !this->last_on_;
69 this->last_on_ = is_on;
70 if (should_trigger) {
71 this->trigger();
72 }
73 });
74 this->last_on_ = state->state;
75 }
76
77 protected:
80};
81
82class FanTurnOffTrigger final : public Trigger<> {
83 public:
86 auto is_on = this->fan_->state;
87 auto should_trigger = !is_on && this->last_on_;
88 this->last_on_ = is_on;
89 if (should_trigger) {
90 this->trigger();
91 }
92 });
93 this->last_on_ = state->state;
94 }
95
96 protected:
99};
100
101class FanDirectionSetTrigger final : public Trigger<FanDirection> {
102 public:
104 state->add_on_state_callback([this]() {
105 auto direction = this->fan_->direction;
106 auto should_trigger = direction != this->last_direction_;
108 if (should_trigger) {
109 this->trigger(direction);
110 }
111 });
112 this->last_direction_ = state->direction;
113 }
114
115 protected:
118};
119
120class FanOscillatingSetTrigger final : public Trigger<bool> {
121 public:
123 state->add_on_state_callback([this]() {
124 auto oscillating = this->fan_->oscillating;
125 auto should_trigger = oscillating != this->last_oscillating_;
127 if (should_trigger) {
128 this->trigger(oscillating);
129 }
130 });
131 this->last_oscillating_ = state->oscillating;
132 }
133
134 protected:
137};
138
139class FanSpeedSetTrigger final : public Trigger<int> {
140 public:
142 state->add_on_state_callback([this]() {
143 auto speed = this->fan_->speed;
144 auto should_trigger = speed != this->last_speed_;
145 this->last_speed_ = speed;
146 if (should_trigger) {
147 this->trigger(speed);
148 }
149 });
150 this->last_speed_ = state->speed;
151 }
152
153 protected:
156};
157
158class FanPresetSetTrigger final : public Trigger<StringRef> {
159 public:
161 state->add_on_state_callback([this]() {
162 auto preset_mode = this->fan_->get_preset_mode();
163 auto should_trigger = preset_mode != this->last_preset_mode_;
165 if (should_trigger) {
166 this->trigger(preset_mode);
167 }
168 });
169 this->last_preset_mode_ = state->get_preset_mode();
170 }
171
172 protected:
175};
176
177} // namespace esphome::fan
virtual void play(const Ts &...x)=0
StringRef is a reference to a string owned by something else.
Definition string_ref.h:30
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:528
TEMPLATABLE_VALUE(bool, no_off_cycle) void play(const Ts &...x) override
Definition automation.h:13
FanCall & set_speed(int speed)
Definition fan.h:59
void add_on_state_callback(F &&callback)
Register a callback that will be called each time the state changes.
Definition fan.h:124
FanCall turn_on()
Definition fan.h:118
FanCall turn_off()
Definition fan.h:119
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.h:120
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:147
FanDirection direction
The current direction of the fan.
Definition fan.h:116
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
int supported_speed_count() const
Return how many speed levels the fan has.
Definition fan_traits.h:29
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:20
uint16_t x
Definition tt21100.cpp:5