ESPHome 2025.12.1
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::number {
6
7static const char *const TAG = "number";
8
9// Helper functions to reduce code size for logging
11 ESP_LOGW(TAG, "'%s': %s", this->parent_->get_name().c_str(), LOG_STR_ARG(message));
12}
13
14void NumberCall::log_perform_warning_value_range_(const LogString *comparison, const LogString *limit_type, float val,
15 float limit) {
16 ESP_LOGW(TAG, "'%s': %f %s %s %f", this->parent_->get_name().c_str(), val, LOG_STR_ARG(comparison),
17 LOG_STR_ARG(limit_type), limit);
18}
19
21
25
29
31
33
35 this->operation_ = operation;
36 return *this;
37}
38
40 this->value_ = value;
41 return *this;
42}
43
45 this->cycle_ = cycle;
46 return *this;
47}
48
50 auto *parent = this->parent_;
51 const auto *name = parent->get_name().c_str();
52 const auto &traits = parent->traits;
53
54 if (this->operation_ == NUMBER_OP_NONE) {
55 this->log_perform_warning_(LOG_STR("No operation"));
56 return;
57 }
58
59 float target_value = NAN;
60 float min_value = traits.get_min_value();
61 float max_value = traits.get_max_value();
62
63 if (this->operation_ == NUMBER_OP_SET) {
64 ESP_LOGD(TAG, "'%s': Setting value", name);
65 if (!this->value_.has_value() || std::isnan(*this->value_)) {
66 this->log_perform_warning_(LOG_STR("No value"));
67 return;
68 }
69 target_value = this->value_.value();
70 } else if (this->operation_ == NUMBER_OP_TO_MIN) {
71 if (std::isnan(min_value)) {
72 this->log_perform_warning_(LOG_STR("min undefined"));
73 } else {
74 target_value = min_value;
75 }
76 } else if (this->operation_ == NUMBER_OP_TO_MAX) {
77 if (std::isnan(max_value)) {
78 this->log_perform_warning_(LOG_STR("max undefined"));
79 } else {
80 target_value = max_value;
81 }
82 } else if (this->operation_ == NUMBER_OP_INCREMENT) {
83 ESP_LOGD(TAG, "'%s': Increment with%s cycling", name, this->cycle_ ? "" : "out");
84 if (!parent->has_state()) {
85 this->log_perform_warning_(LOG_STR("Can't increment, no state"));
86 return;
87 }
88 auto step = traits.get_step();
89 target_value = parent->state + (std::isnan(step) ? 1 : step);
90 if (target_value > max_value) {
91 if (this->cycle_ && !std::isnan(min_value)) {
92 target_value = min_value;
93 } else {
94 target_value = max_value;
95 }
96 }
97 } else if (this->operation_ == NUMBER_OP_DECREMENT) {
98 ESP_LOGD(TAG, "'%s': Decrement with%s cycling", name, this->cycle_ ? "" : "out");
99 if (!parent->has_state()) {
100 this->log_perform_warning_(LOG_STR("Can't decrement, no state"));
101 return;
102 }
103 auto step = traits.get_step();
104 target_value = parent->state - (std::isnan(step) ? 1 : step);
105 if (target_value < min_value) {
106 if (this->cycle_ && !std::isnan(max_value)) {
107 target_value = max_value;
108 } else {
109 target_value = min_value;
110 }
111 }
112 }
113
114 if (target_value < min_value) {
115 this->log_perform_warning_value_range_(LOG_STR("<"), LOG_STR("min"), target_value, min_value);
116 return;
117 }
118 if (target_value > max_value) {
119 this->log_perform_warning_value_range_(LOG_STR(">"), LOG_STR("max"), target_value, max_value);
120 return;
121 }
122
123 ESP_LOGD(TAG, " New value: %f", target_value);
124 this->parent_->control(target_value);
125}
126
127} // namespace esphome::number
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:41
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]