1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-08 04:43:46 +01:00

Merge branch 'number_call_logging' into integration

This commit is contained in:
J. Nick Koston
2025-10-02 16:45:00 +02:00
2 changed files with 28 additions and 12 deletions

View File

@@ -7,6 +7,17 @@ namespace number {
static const char *const TAG = "number"; static const char *const TAG = "number";
// Helper functions to reduce code size for logging
void NumberCall::log_perform_warning_(const LogString *message) {
ESP_LOGW(TAG, "'%s': %s", this->parent_->get_name().c_str(), LOG_STR_ARG(message));
}
void NumberCall::log_perform_warning_value_range_(const LogString *comparison, const LogString *limit_type, float val,
float limit) {
ESP_LOGW(TAG, "'%s': %f %s %s %f", this->parent_->get_name().c_str(), val, LOG_STR_ARG(comparison),
LOG_STR_ARG(limit_type), limit);
}
NumberCall &NumberCall::set_value(float value) { return this->with_operation(NUMBER_OP_SET).with_value(value); } NumberCall &NumberCall::set_value(float value) { return this->with_operation(NUMBER_OP_SET).with_value(value); }
NumberCall &NumberCall::number_increment(bool cycle) { NumberCall &NumberCall::number_increment(bool cycle) {
@@ -42,7 +53,7 @@ void NumberCall::perform() {
const auto &traits = parent->traits; const auto &traits = parent->traits;
if (this->operation_ == NUMBER_OP_NONE) { if (this->operation_ == NUMBER_OP_NONE) {
ESP_LOGW(TAG, "'%s' - NumberCall performed without selecting an operation", name); this->log_perform_warning_(LOG_STR("No operation"));
return; return;
} }
@@ -51,28 +62,28 @@ void NumberCall::perform() {
float max_value = traits.get_max_value(); float max_value = traits.get_max_value();
if (this->operation_ == NUMBER_OP_SET) { if (this->operation_ == NUMBER_OP_SET) {
ESP_LOGD(TAG, "'%s' - Setting number value", name); ESP_LOGD(TAG, "'%s': Setting value", name);
if (!this->value_.has_value() || std::isnan(*this->value_)) { if (!this->value_.has_value() || std::isnan(*this->value_)) {
ESP_LOGW(TAG, "'%s' - No value set for NumberCall", name); this->log_perform_warning_(LOG_STR("No value"));
return; return;
} }
target_value = this->value_.value(); target_value = this->value_.value();
} else if (this->operation_ == NUMBER_OP_TO_MIN) { } else if (this->operation_ == NUMBER_OP_TO_MIN) {
if (std::isnan(min_value)) { if (std::isnan(min_value)) {
ESP_LOGW(TAG, "'%s' - Can't set to min value through NumberCall: no min_value defined", name); this->log_perform_warning_(LOG_STR("min undefined"));
} else { } else {
target_value = min_value; target_value = min_value;
} }
} else if (this->operation_ == NUMBER_OP_TO_MAX) { } else if (this->operation_ == NUMBER_OP_TO_MAX) {
if (std::isnan(max_value)) { if (std::isnan(max_value)) {
ESP_LOGW(TAG, "'%s' - Can't set to max value through NumberCall: no max_value defined", name); this->log_perform_warning_(LOG_STR("max undefined"));
} else { } else {
target_value = max_value; target_value = max_value;
} }
} else if (this->operation_ == NUMBER_OP_INCREMENT) { } else if (this->operation_ == NUMBER_OP_INCREMENT) {
ESP_LOGD(TAG, "'%s' - Increment number, with%s cycling", name, this->cycle_ ? "" : "out"); ESP_LOGD(TAG, "'%s': Increment with%s cycling", name, this->cycle_ ? "" : "out");
if (!parent->has_state()) { if (!parent->has_state()) {
ESP_LOGW(TAG, "'%s' - Can't increment number through NumberCall: no active state to modify", name); this->log_perform_warning_(LOG_STR("Can't increment, no state"));
return; return;
} }
auto step = traits.get_step(); auto step = traits.get_step();
@@ -85,9 +96,9 @@ void NumberCall::perform() {
} }
} }
} else if (this->operation_ == NUMBER_OP_DECREMENT) { } else if (this->operation_ == NUMBER_OP_DECREMENT) {
ESP_LOGD(TAG, "'%s' - Decrement number, with%s cycling", name, this->cycle_ ? "" : "out"); ESP_LOGD(TAG, "'%s': Decrement with%s cycling", name, this->cycle_ ? "" : "out");
if (!parent->has_state()) { if (!parent->has_state()) {
ESP_LOGW(TAG, "'%s' - Can't decrement number through NumberCall: no active state to modify", name); this->log_perform_warning_(LOG_STR("Can't decrement, no state"));
return; return;
} }
auto step = traits.get_step(); auto step = traits.get_step();
@@ -102,15 +113,15 @@ void NumberCall::perform() {
} }
if (target_value < min_value) { if (target_value < min_value) {
ESP_LOGW(TAG, "'%s' - Value %f must not be less than minimum %f", name, target_value, min_value); this->log_perform_warning_value_range_(LOG_STR("<"), LOG_STR("min"), target_value, min_value);
return; return;
} }
if (target_value > max_value) { if (target_value > max_value) {
ESP_LOGW(TAG, "'%s' - Value %f must not be greater than maximum %f", name, target_value, max_value); this->log_perform_warning_value_range_(LOG_STR(">"), LOG_STR("max"), target_value, max_value);
return; return;
} }
ESP_LOGD(TAG, " New number value: %f", target_value); ESP_LOGD(TAG, " New value: %f", target_value);
this->parent_->control(target_value); this->parent_->control(target_value);
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "esphome/core/helpers.h" #include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "number_traits.h" #include "number_traits.h"
namespace esphome { namespace esphome {
@@ -33,6 +34,10 @@ class NumberCall {
NumberCall &with_cycle(bool cycle); NumberCall &with_cycle(bool cycle);
protected: protected:
void log_perform_warning_(const LogString *message);
void log_perform_warning_value_range_(const LogString *comparison, const LogString *limit_type, float val,
float limit);
Number *const parent_; Number *const parent_;
NumberOperation operation_{NUMBER_OP_NONE}; NumberOperation operation_{NUMBER_OP_NONE};
optional<float> value_; optional<float> value_;