From a5c955f9a581b6fa76a6910cf34f7e95b1fda44f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 15 Oct 2025 16:16:27 -1000 Subject: [PATCH 1/3] bot comments --- .../components/api/homeassistant_service.h | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index 4adda47b71..21f75f0cab 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -97,28 +97,22 @@ template class HomeAssistantServiceCallAction : public Action void set_service(T service) { this->service_ = service; } - // Initialize FixedVector members - called from Python codegen with compile-time known sizes + // Initialize FixedVector members - called from Python codegen with compile-time known sizes. + // Must be called before any add_* methods; capacity must match the number of subsequent add_* calls. void init_data(size_t count) { this->data_.init(count); } void init_data_template(size_t count) { this->data_template_.init(count); } void init_variables(size_t count) { this->variables_.init(count); } // Keys are always string literals from the Python code generation (e.g., cg.add(var.add_data("tag_id", templ))). // The value parameter can be a lambda/template, but keys are never templatable. - // Using pass-by-value allows the compiler to optimize for both lvalues and rvalues. - template void add_data(std::string key, T value) { - auto &kv = this->data_.emplace_back(); - kv.key = std::move(key); - kv.value = value; + template void add_data(K &&key, V &&value) { + this->add_kv(this->data_, std::forward(key), std::forward(value)); } - template void add_data_template(std::string key, T value) { - auto &kv = this->data_template_.emplace_back(); - kv.key = std::move(key); - kv.value = value; + template void add_data_template(K &&key, V &&value) { + this->add_kv(this->data_template_, std::forward(key), std::forward(value)); } - template void add_variable(std::string key, T value) { - auto &kv = this->variables_.emplace_back(); - kv.key = std::move(key); - kv.value = value; + template void add_variable(K &&key, V &&value) { + this->add_kv(this->variables_, std::forward(key), std::forward(value)); } #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES @@ -191,6 +185,13 @@ template class HomeAssistantServiceCallAction : public Action void add_kv(FixedVector> &vec, K &&key, V &&value) { + auto &kv = vec.emplace_back(); + kv.key = std::forward(key); + kv.value = std::forward(value); + } + template static void populate_service_map(VectorType &dest, SourceType &source, Ts... x) { dest.init(source.size()); From 0a7a3bae8b059bf8271f37892cdffed1cd3a4caa Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 15 Oct 2025 16:19:22 -1000 Subject: [PATCH 2/3] protect --- esphome/components/api/homeassistant_service.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index 21f75f0cab..d13c4d70b8 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -106,13 +106,13 @@ template class HomeAssistantServiceCallAction : public Action void add_data(K &&key, V &&value) { - this->add_kv(this->data_, std::forward(key), std::forward(value)); + this->add_kv_(this->data_, std::forward(key), std::forward(value)); } template void add_data_template(K &&key, V &&value) { - this->add_kv(this->data_template_, std::forward(key), std::forward(value)); + this->add_kv_(this->data_template_, std::forward(key), std::forward(value)); } template void add_variable(K &&key, V &&value) { - this->add_kv(this->variables_, std::forward(key), std::forward(value)); + this->add_kv_(this->variables_, std::forward(key), std::forward(value)); } #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES From 295ac4b1b8a235de4dc5fb603347d50dadb2ca1d Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 15 Oct 2025 16:19:36 -1000 Subject: [PATCH 3/3] protect --- esphome/components/api/homeassistant_service.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index d13c4d70b8..4343fcd0bb 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -186,7 +186,7 @@ template class HomeAssistantServiceCallAction : public Action void add_kv(FixedVector> &vec, K &&key, V &&value) { + template void add_kv_(FixedVector> &vec, K &&key, V &&value) { auto &kv = vec.emplace_back(); kv.key = std::forward(key); kv.value = std::forward(value);