From 5374df73ed58d758d52a1ea3b3dd9af4801c34af Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 2 Oct 2025 16:18:06 +0200 Subject: [PATCH 1/3] number --- esphome/components/number/number_call.cpp | 39 ++++++++++++++++------- esphome/components/number/number_call.h | 6 ++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/esphome/components/number/number_call.cpp b/esphome/components/number/number_call.cpp index 4219f85328..122cb04470 100644 --- a/esphome/components/number/number_call.cpp +++ b/esphome/components/number/number_call.cpp @@ -7,6 +7,21 @@ namespace 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); +} + +void NumberCall::log_perform_warning_two_strings_(const LogString *prefix, const LogString *suffix) { + ESP_LOGW(TAG, "'%s': %s %s", this->parent_->get_name().c_str(), LOG_STR_ARG(prefix), LOG_STR_ARG(suffix)); +} + NumberCall &NumberCall::set_value(float value) { return this->with_operation(NUMBER_OP_SET).with_value(value); } NumberCall &NumberCall::number_increment(bool cycle) { @@ -42,7 +57,7 @@ void NumberCall::perform() { const auto &traits = parent->traits; 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; } @@ -51,28 +66,28 @@ void NumberCall::perform() { float max_value = traits.get_max_value(); 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_)) { - ESP_LOGW(TAG, "'%s' - No value set for NumberCall", name); + this->log_perform_warning_(LOG_STR("No value")); return; } target_value = this->value_.value(); } else if (this->operation_ == NUMBER_OP_TO_MIN) { 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_two_strings_(LOG_STR("min"), LOG_STR("value undefined")); } else { target_value = min_value; } } else if (this->operation_ == NUMBER_OP_TO_MAX) { 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_two_strings_(LOG_STR("max"), LOG_STR("value undefined")); } else { target_value = max_value; } } 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()) { - ESP_LOGW(TAG, "'%s' - Can't increment number through NumberCall: no active state to modify", name); + this->log_perform_warning_two_strings_(LOG_STR("Can't increment,"), LOG_STR("no state")); return; } auto step = traits.get_step(); @@ -85,9 +100,9 @@ void NumberCall::perform() { } } } 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()) { - ESP_LOGW(TAG, "'%s' - Can't decrement number through NumberCall: no active state to modify", name); + this->log_perform_warning_two_strings_(LOG_STR("Can't decrement,"), LOG_STR("no state")); return; } auto step = traits.get_step(); @@ -102,15 +117,15 @@ void NumberCall::perform() { } 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; } 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; } - ESP_LOGD(TAG, " New number value: %f", target_value); + ESP_LOGD(TAG, " New value: %f", target_value); this->parent_->control(target_value); } diff --git a/esphome/components/number/number_call.h b/esphome/components/number/number_call.h index bd50170be5..a23f6b8e5a 100644 --- a/esphome/components/number/number_call.h +++ b/esphome/components/number/number_call.h @@ -1,6 +1,7 @@ #pragma once #include "esphome/core/helpers.h" +#include "esphome/core/log.h" #include "number_traits.h" namespace esphome { @@ -33,6 +34,11 @@ class NumberCall { NumberCall &with_cycle(bool cycle); 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); + void log_perform_warning_two_strings_(const LogString *prefix, const LogString *suffix); + Number *const parent_; NumberOperation operation_{NUMBER_OP_NONE}; optional value_; From 1fc4d1acfb8acdce18914a3b3eebb429b555d3ab Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 2 Oct 2025 16:20:42 +0200 Subject: [PATCH 2/3] number --- esphome/components/number/number_call.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/esphome/components/number/number_call.cpp b/esphome/components/number/number_call.cpp index 122cb04470..024908ba29 100644 --- a/esphome/components/number/number_call.cpp +++ b/esphome/components/number/number_call.cpp @@ -74,20 +74,20 @@ void NumberCall::perform() { target_value = this->value_.value(); } else if (this->operation_ == NUMBER_OP_TO_MIN) { if (std::isnan(min_value)) { - this->log_perform_warning_two_strings_(LOG_STR("min"), LOG_STR("value undefined")); + this->log_perform_warning_two_strings_(LOG_STR("min"), LOG_STR("undefined")); } else { target_value = min_value; } } else if (this->operation_ == NUMBER_OP_TO_MAX) { if (std::isnan(max_value)) { - this->log_perform_warning_two_strings_(LOG_STR("max"), LOG_STR("value undefined")); + this->log_perform_warning_two_strings_(LOG_STR("max"), LOG_STR("undefined")); } else { target_value = max_value; } } else if (this->operation_ == NUMBER_OP_INCREMENT) { - ESP_LOGD(TAG, "'%s': Increment, with%s cycling", name, this->cycle_ ? "" : "out"); + ESP_LOGD(TAG, "'%s': Increment with%s cycling", name, this->cycle_ ? "" : "out"); if (!parent->has_state()) { - this->log_perform_warning_two_strings_(LOG_STR("Can't increment,"), LOG_STR("no state")); + this->log_perform_warning_two_strings_(LOG_STR("Can't increment"), LOG_STR("no state")); return; } auto step = traits.get_step(); @@ -100,9 +100,9 @@ void NumberCall::perform() { } } } else if (this->operation_ == NUMBER_OP_DECREMENT) { - ESP_LOGD(TAG, "'%s': Decrement, with%s cycling", name, this->cycle_ ? "" : "out"); + ESP_LOGD(TAG, "'%s': Decrement with%s cycling", name, this->cycle_ ? "" : "out"); if (!parent->has_state()) { - this->log_perform_warning_two_strings_(LOG_STR("Can't decrement,"), LOG_STR("no state")); + this->log_perform_warning_two_strings_(LOG_STR("Can't decrement"), LOG_STR("no state")); return; } auto step = traits.get_step(); From 8931dcf409b06d3c04a12f81d8e01a064d3b7845 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 2 Oct 2025 16:33:22 +0200 Subject: [PATCH 3/3] reduce --- esphome/components/number/number_call.cpp | 12 ++++-------- esphome/components/number/number_call.h | 1 - 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/esphome/components/number/number_call.cpp b/esphome/components/number/number_call.cpp index 024908ba29..669dd65184 100644 --- a/esphome/components/number/number_call.cpp +++ b/esphome/components/number/number_call.cpp @@ -18,10 +18,6 @@ void NumberCall::log_perform_warning_value_range_(const LogString *comparison, c LOG_STR_ARG(limit_type), limit); } -void NumberCall::log_perform_warning_two_strings_(const LogString *prefix, const LogString *suffix) { - ESP_LOGW(TAG, "'%s': %s %s", this->parent_->get_name().c_str(), LOG_STR_ARG(prefix), LOG_STR_ARG(suffix)); -} - NumberCall &NumberCall::set_value(float value) { return this->with_operation(NUMBER_OP_SET).with_value(value); } NumberCall &NumberCall::number_increment(bool cycle) { @@ -74,20 +70,20 @@ void NumberCall::perform() { target_value = this->value_.value(); } else if (this->operation_ == NUMBER_OP_TO_MIN) { if (std::isnan(min_value)) { - this->log_perform_warning_two_strings_(LOG_STR("min"), LOG_STR("undefined")); + this->log_perform_warning_(LOG_STR("min undefined")); } else { target_value = min_value; } } else if (this->operation_ == NUMBER_OP_TO_MAX) { if (std::isnan(max_value)) { - this->log_perform_warning_two_strings_(LOG_STR("max"), LOG_STR("undefined")); + this->log_perform_warning_(LOG_STR("max undefined")); } else { target_value = max_value; } } else if (this->operation_ == NUMBER_OP_INCREMENT) { ESP_LOGD(TAG, "'%s': Increment with%s cycling", name, this->cycle_ ? "" : "out"); if (!parent->has_state()) { - this->log_perform_warning_two_strings_(LOG_STR("Can't increment"), LOG_STR("no state")); + this->log_perform_warning_(LOG_STR("Can't increment, no state")); return; } auto step = traits.get_step(); @@ -102,7 +98,7 @@ void NumberCall::perform() { } else if (this->operation_ == NUMBER_OP_DECREMENT) { ESP_LOGD(TAG, "'%s': Decrement with%s cycling", name, this->cycle_ ? "" : "out"); if (!parent->has_state()) { - this->log_perform_warning_two_strings_(LOG_STR("Can't decrement"), LOG_STR("no state")); + this->log_perform_warning_(LOG_STR("Can't decrement, no state")); return; } auto step = traits.get_step(); diff --git a/esphome/components/number/number_call.h b/esphome/components/number/number_call.h index a23f6b8e5a..807207f0ec 100644 --- a/esphome/components/number/number_call.h +++ b/esphome/components/number/number_call.h @@ -37,7 +37,6 @@ class NumberCall { void log_perform_warning_(const LogString *message); void log_perform_warning_value_range_(const LogString *comparison, const LogString *limit_type, float val, float limit); - void log_perform_warning_two_strings_(const LogString *prefix, const LogString *suffix); Number *const parent_; NumberOperation operation_{NUMBER_OP_NONE};