diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index b164a1b87a..330dc5b1a1 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -5,6 +5,7 @@ #include "api_pb2.h" #include "esphome/core/automation.h" #include "esphome/core/helpers.h" +#include #include namespace esphome { @@ -36,10 +37,10 @@ template class TemplatableStringValue : public TemplatableValue class TemplatableKeyValuePair { public: - // It's safe to use const std::string& for the key parameter because keys are always - // string literals from YAML dictionary keys (e.g., "code", "event") and never - // templatable values or lambdas. Only the value parameter can be a lambda/template. - template TemplatableKeyValuePair(const std::string &key, T value) : key(key), value(value) {} + // Keys are always string literals from YAML dictionary keys (e.g., "code", "event") + // and never templatable values or lambdas. Only the value parameter can be a lambda/template. + // Using pass-by-value with std::move allows optimal performance for both lvalues and rvalues. + template TemplatableKeyValuePair(std::string key, T value) : key(std::move(key)), value(value) {} std::string key; TemplatableStringValue value; };