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