diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index 9dcfdec7cf..b164a1b87a 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -36,6 +36,9 @@ 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) {} std::string key; TemplatableStringValue value; @@ -47,6 +50,9 @@ 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))). + // 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);