ESPHome 2025.12.1
Loading...
Searching...
No Matches
automation.h
Go to the documentation of this file.
1#pragma once
2
6
7namespace esphome::number {
8
9class NumberStateTrigger : public Trigger<float> {
10 public:
11 explicit NumberStateTrigger(Number *parent) {
12 parent->add_on_state_callback([this](float value) { this->trigger(value); });
13 }
14};
15
16template<typename... Ts> class NumberSetAction : public Action<Ts...> {
17 public:
18 NumberSetAction(Number *number) : number_(number) {}
19 TEMPLATABLE_VALUE(float, value)
20
21 void play(const Ts &...x) override {
22 auto call = this->number_->make_call();
23 call.set_value(this->value_.value(x...));
24 call.perform();
25 }
26
27 protected:
29};
30
31template<typename... Ts> class NumberOperationAction : public Action<Ts...> {
32 public:
33 explicit NumberOperationAction(Number *number) : number_(number) {}
35 TEMPLATABLE_VALUE(bool, cycle)
36
37 void play(const Ts &...x) override {
38 auto call = this->number_->make_call();
39 call.with_operation(this->operation_.value(x...));
40 if (this->cycle_.has_value()) {
41 call.with_cycle(this->cycle_.value(x...));
42 }
43 call.perform();
44 }
45
46 protected:
48};
49
50class ValueRangeTrigger : public Trigger<float>, public Component {
51 public:
52 explicit ValueRangeTrigger(Number *parent) : parent_(parent) {}
53
54 template<typename V> void set_min(V min) { this->min_ = min; }
55 template<typename V> void set_max(V max) { this->max_ = max; }
56
57 void setup() override;
58 float get_setup_priority() const override;
59
60 protected:
61 void on_state_(float state);
62
65 bool previous_in_range_{false};
68};
69
70template<typename... Ts> class NumberInRangeCondition : public Condition<Ts...> {
71 public:
73
74 void set_min(float min) { this->min_ = min; }
75 void set_max(float max) { this->max_ = max; }
76 bool check(const Ts &...x) override {
77 const float state = this->parent_->state;
78 if (std::isnan(this->min_)) {
79 return state <= this->max_;
80 } else if (std::isnan(this->max_)) {
81 return state >= this->min_;
82 } else {
83 return this->min_ <= state && state <= this->max_;
84 }
85 }
86
87 protected:
89 float min_{NAN};
90 float max_{NAN};
91};
92
93} // namespace esphome::number
virtual void play(const Ts &...x)=0
Base class for all automation conditions.
Definition automation.h:183
void trigger(const Ts &...x)
Definition automation.h:204
NumberCall & with_operation(NumberOperation operation)
NumberCall & set_value(float value)
Base-class for all numbers.
Definition number.h:29
NumberCall make_call()
Definition number.h:35
void add_on_state_callback(std::function< void(float)> &&callback)
Definition number.cpp:41
bool check(const Ts &...x) override
Definition automation.h:76
cycle void play(const Ts &...x) override
Definition automation.h:37
TEMPLATABLE_VALUE(NumberOperation, operation) TEMPLATABLE_VALUE(bool
TEMPLATABLE_VALUE(float, value) void play(const Ts &...x) override
Definition automation.h:19
TemplatableValue< float, float > min_
Definition automation.h:66
float get_setup_priority() const override
TemplatableValue< float, float > max_
Definition automation.h:67
bool state
Definition fan.h:0
uint16_t x
Definition tt21100.cpp:5