ESPHome 2026.11.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::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 FanTurnOnTrigger final : public Trigger<> {
54 public:
57 auto is_on = this->fan_->state;
58 auto should_trigger = is_on && !this->last_on_;
59 this->last_on_ = is_on;
60 if (should_trigger) {
61 this->trigger();
62 }
63 });
64 this->last_on_ = state->state;
65 }
66
67 protected:
70};
71
72class FanTurnOffTrigger final : public Trigger<> {
73 public:
76 auto is_on = this->fan_->state;
77 auto should_trigger = !is_on && this->last_on_;
78 this->last_on_ = is_on;
79 if (should_trigger) {
80 this->trigger();
81 }
82 });
83 this->last_on_ = state->state;
84 }
85
86 protected:
89};
90
91class FanDirectionSetTrigger final : public Trigger<FanDirection> {
92 public:
95 auto direction = this->fan_->direction;
96 auto should_trigger = direction != this->last_direction_;
98 if (should_trigger) {
99 this->trigger(direction);
100 }
101 });
102 this->last_direction_ = state->direction;
103 }
104
105 protected:
108};
109
110class FanOscillatingSetTrigger final : public Trigger<bool> {
111 public:
113 state->add_on_state_callback([this]() {
114 auto oscillating = this->fan_->oscillating;
115 auto should_trigger = oscillating != this->last_oscillating_;
117 if (should_trigger) {
118 this->trigger(oscillating);
119 }
120 });
121 this->last_oscillating_ = state->oscillating;
122 }
123
124 protected:
127};
128
129class FanSpeedSetTrigger final : public Trigger<int> {
130 public:
132 state->add_on_state_callback([this]() {
133 auto speed = this->fan_->speed;
134 auto should_trigger = speed != this->last_speed_;
135 this->last_speed_ = speed;
136 if (should_trigger) {
137 this->trigger(speed);
138 }
139 });
140 this->last_speed_ = state->speed;
141 }
142
143 protected:
146};
147
148class FanPresetSetTrigger final : public Trigger<StringRef> {
149 public:
151 state->add_on_state_callback([this]() {
152 auto preset_mode = this->fan_->get_preset_mode();
153 auto should_trigger = preset_mode != this->last_preset_mode_;
155 if (should_trigger) {
156 this->trigger(preset_mode);
157 }
158 });
159 this->last_preset_mode_ = state->get_preset_mode();
160 }
161
162 protected:
165};
166
167} // 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:31
void trigger(const Ts &...x) ESPHOME_ALWAYS_INLINE
Definition automation.h:556
TEMPLATABLE_VALUE(bool, no_off_cycle) void play(const Ts &...x) override
Definition automation.h:13
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.h:119
FanCall turn_off()
Definition fan.h:120
virtual FanTraits get_traits()=0
FanCall toggle()
Definition fan.h:121
StringRef get_preset_mode() const
Get the current preset mode.
Definition fan.h:155
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
int supported_speed_count() const
Return how many speed levels the fan has.
Definition fan_traits.h:31
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
uint16_t x
Definition tt21100.cpp:5