ESPHome 2025.10.3
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
10// Helper functions to reduce code size for logging
12 ESP_LOGW(TAG, "'%s': %s", this->parent_->get_name().c_str(), LOG_STR_ARG(message));
13}
14
15void NumberCall::log_perform_warning_value_range_(const LogString *comparison, const LogString *limit_type, float val,
16 float limit) {
17 ESP_LOGW(TAG, "'%s': %f %s %s %f", this->parent_->get_name().c_str(), val, LOG_STR_ARG(comparison),
18 LOG_STR_ARG(limit_type), limit);
19}
20
22
26
30
32
34
36 this->operation_ = operation;
37 return *this;
38}
39
41 this->value_ = value;
42 return *this;
43}
44
46 this->cycle_ = cycle;
47 return *this;
48}
49
51 auto *parent = this->parent_;
52 const auto *name = parent->get_name().c_str();
53 const auto &traits = parent->traits;
54
55 if (this->operation_ == NUMBER_OP_NONE) {
56 this->log_perform_warning_(LOG_STR("No operation"));
57 return;
58 }
59
60 float target_value = NAN;
61 float min_value = traits.get_min_value();
62 float max_value = traits.get_max_value();
63
64 if (this->operation_ == NUMBER_OP_SET) {
65 ESP_LOGD(TAG, "'%s': Setting value", name);
66 if (!this->value_.has_value() || std::isnan(*this->value_)) {
67 this->log_perform_warning_(LOG_STR("No value"));
68 return;
69 }
70 target_value = this->value_.value();
71 } else if (this->operation_ == NUMBER_OP_TO_MIN) {
72 if (std::isnan(min_value)) {
73 this->log_perform_warning_(LOG_STR("min undefined"));
74 } else {
75 target_value = min_value;
76 }
77 } else if (this->operation_ == NUMBER_OP_TO_MAX) {
78 if (std::isnan(max_value)) {
79 this->log_perform_warning_(LOG_STR("max undefined"));
80 } else {
81 target_value = max_value;
82 }
83 } else if (this->operation_ == NUMBER_OP_INCREMENT) {
84 ESP_LOGD(TAG, "'%s': Increment with%s cycling", name, this->cycle_ ? "" : "out");
85 if (!parent->has_state()) {
86 this->log_perform_warning_(LOG_STR("Can't increment, no state"));
87 return;
88 }
89 auto step = traits.get_step();
90 target_value = parent->state + (std::isnan(step) ? 1 : step);
91 if (target_value > max_value) {
92 if (this->cycle_ && !std::isnan(min_value)) {
93 target_value = min_value;
94 } else {
95 target_value = max_value;
96 }
97 }
98 } else if (this->operation_ == NUMBER_OP_DECREMENT) {
99 ESP_LOGD(TAG, "'%s': Decrement with%s cycling", name, this->cycle_ ? "" : "out");
100 if (!parent->has_state()) {
101 this->log_perform_warning_(LOG_STR("Can't decrement, no state"));
102 return;
103 }
104 auto step = traits.get_step();
105 target_value = parent->state - (std::isnan(step) ? 1 : step);
106 if (target_value < min_value) {
107 if (this->cycle_ && !std::isnan(max_value)) {
108 target_value = max_value;
109 } else {
110 target_value = min_value;
111 }
112 }
113 }
114
115 if (target_value < min_value) {
116 this->log_perform_warning_value_range_(LOG_STR("<"), LOG_STR("min"), target_value, min_value);
117 return;
118 }
119 if (target_value > max_value) {
120 this->log_perform_warning_value_range_(LOG_STR(">"), LOG_STR("max"), target_value, max_value);
121 return;
122 }
123
124 ESP_LOGD(TAG, " New value: %f", target_value);
125 this->parent_->control(target_value);
126}
127
128} // namespace number
129} // namespace esphome
const StringRef & get_name() const
constexpr const char * c_str() const
Definition string_ref.h:69
NumberCall & with_cycle(bool cycle)
NumberCall & number_decrement(bool cycle)
NumberCall & with_operation(NumberOperation operation)
NumberCall & number_increment(bool cycle)
void log_perform_warning_(const LogString *message)
NumberCall & set_value(float value)
NumberCall & with_value(float value)
NumberOperation operation_
Definition number_call.h:42
void log_perform_warning_value_range_(const LogString *comparison, const LogString *limit_type, float val, float limit)
optional< float > value_
Definition number_call.h:43
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:92
value_type const & value() const
Definition optional.h:94
const char * message
Definition component.cpp:38
mopeka_std_values val[4]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7