ESPHome  2024.12.2
automation.h
Go to the documentation of this file.
1 #pragma once
2 
6 
7 namespace esphome {
8 namespace number {
9 
10 class 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 
17 template<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 
32 template<typename... Ts> class NumberOperationAction : public Action<Ts...> {
33  public:
34  explicit NumberOperationAction(Number *number) : number_(number) {}
35  TEMPLATABLE_VALUE(NumberOperation, operation)
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 
51 class 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 
71 template<typename... Ts> class NumberInRangeCondition : public Condition<Ts...> {
72  public:
73  NumberInRangeCondition(Number *parent) : parent_(parent) {}
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
void setup()
void add_on_state_callback(std::function< void(float)> &&callback)
Definition: number.cpp:16
TEMPLATABLE_VALUE(float, value) void play(Ts... x) override
Definition: automation.h:20
uint16_t x
Definition: tt21100.cpp:17
void trigger(Ts... x)
Inform the parent automation that the event has triggered.
Definition: automation.h:95
Base class for all automation conditions.
Definition: automation.h:74
Base-class for all numbers.
Definition: number.h:39
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
NumberSetAction(Number *number)
Definition: automation.h:19
bool state
Definition: fan.h:34