diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index 330dc5b1a1..a588441f76 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -51,14 +51,16 @@ template class HomeAssistantServiceCallAction : public Action void set_service(T service) { this->service_ = service; } - // These methods use const std::string& for keys because keys are always string literals - // from the Python code generation (e.g., cg.add(var.add_data("tag_id", templ))). + // 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. - template void add_data(const std::string &key, T value) { this->data_.emplace_back(key, value); } - template void add_data_template(const std::string &key, T value) { - this->data_template_.emplace_back(key, value); + // Using pass-by-value allows the compiler to optimize for both lvalues and rvalues. + template void add_data(std::string key, T value) { this->data_.emplace_back(std::move(key), value); } + template void add_data_template(std::string key, T value) { + this->data_template_.emplace_back(std::move(key), value); + } + template void add_variable(std::string key, T value) { + this->variables_.emplace_back(std::move(key), value); } - template void add_variable(const std::string &key, T value) { this->variables_.emplace_back(key, value); } void play(Ts... x) override { HomeassistantServiceResponse resp;