ESPHome 2025.5.0
Loading...
Searching...
No Matches
number_call.cpp
Go to the documentation of this file.
1#include "number_call.h"
2#include "number.h"
3#include "esphome/core/log.h"
4
5namespace esphome {
6namespace number {
7
8static const char *const TAG = "number";
9
11
15
19
21
23
25 this->operation_ = operation;
26 return *this;
27}
28
30 this->value_ = value;
31 return *this;
32}
33
35 this->cycle_ = cycle;
36 return *this;
37}
38
40 auto *parent = this->parent_;
41 const auto *name = parent->get_name().c_str();
42 const auto &traits = parent->traits;
43
44 if (this->operation_ == NUMBER_OP_NONE) {
45 ESP_LOGW(TAG, "'%s' - NumberCall performed without selecting an operation", name);
46 return;
47 }
48
49 float target_value = NAN;
50 float min_value = traits.get_min_value();
51 float max_value = traits.get_max_value();
52
53 if (this->operation_ == NUMBER_OP_SET) {
54 ESP_LOGD(TAG, "'%s' - Setting number value", name);
55 if (!this->value_.has_value() || std::isnan(*this->value_)) {
56 ESP_LOGW(TAG, "'%s' - No value set for NumberCall", name);
57 return;
58 }
59 target_value = this->value_.value();
60 } else if (this->operation_ == NUMBER_OP_TO_MIN) {
61 if (std::isnan(min_value)) {
62 ESP_LOGW(TAG, "'%s' - Can't set to min value through NumberCall: no min_value defined", name);
63 } else {
64 target_value = min_value;
65 }
66 } else if (this->operation_ == NUMBER_OP_TO_MAX) {
67 if (std::isnan(max_value)) {
68 ESP_LOGW(TAG, "'%s' - Can't set to max value through NumberCall: no max_value defined", name);
69 } else {
70 target_value = max_value;
71 }
72 } else if (this->operation_ == NUMBER_OP_INCREMENT) {
73 ESP_LOGD(TAG, "'%s' - Increment number, with%s cycling", name, this->cycle_ ? "" : "out");
74 if (!parent->has_state()) {
75 ESP_LOGW(TAG, "'%s' - Can't increment number through NumberCall: no active state to modify", name);
76 return;
77 }
78 auto step = traits.get_step();
79 target_value = parent->state + (std::isnan(step) ? 1 : step);
80 if (target_value > max_value) {
81 if (this->cycle_ && !std::isnan(min_value)) {
82 target_value = min_value;
83 } else {
84 target_value = max_value;
85 }
86 }
87 } else if (this->operation_ == NUMBER_OP_DECREMENT) {
88 ESP_LOGD(TAG, "'%s' - Decrement number, with%s cycling", name, this->cycle_ ? "" : "out");
89 if (!parent->has_state()) {
90 ESP_LOGW(TAG, "'%s' - Can't decrement number through NumberCall: no active state to modify", name);
91 return;
92 }
93 auto step = traits.get_step();
94 target_value = parent->state - (std::isnan(step) ? 1 : step);
95 if (target_value < min_value) {
96 if (this->cycle_ && !std::isnan(max_value)) {
97 target_value = max_value;
98 } else {
99 target_value = min_value;
100 }
101 }
102 }
103
104 if (target_value < min_value) {
105 ESP_LOGW(TAG, "'%s' - Value %f must not be less than minimum %f", name, target_value, min_value);
106 return;
107 }
108 if (target_value > max_value) {
109 ESP_LOGW(TAG, "'%s' - Value %f must not be greater than maximum %f", name, target_value, max_value);
110 return;
111 }
112
113 ESP_LOGD(TAG, " New number value: %f", target_value);
114 this->parent_->control(target_value);
115}
116
117} // namespace number
118} // namespace esphome
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:68
NumberCall & with_cycle(bool cycle)
NumberCall & number_decrement(bool cycle)
NumberCall & with_operation(NumberOperation operation)
NumberCall & number_increment(bool cycle)
NumberCall & set_value(float value)
NumberCall & with_value(float value)
NumberOperation operation_
Definition number_call.h:37
optional< float > value_
Definition number_call.h:38
virtual void control(float value)=0
Set the value of the number, this is a virtual method that each number integration must implement.
bool has_value() const
Definition optional.h:87
value_type const & value() const
Definition optional.h:89
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7