#pragma once #include "esphome/core/helpers.h" #include "esphome/core/automation.h" #include "api_pb2.h" #include "api_server.h" namespace esphome { namespace api { template class TemplatableKeyValuePair { public: template TemplatableKeyValuePair(std::string key, T value) : key(std::move(key)), value(value) {} std::string key; TemplatableStringValue value; }; template class HomeAssistantServiceCallAction : public Action { public: explicit HomeAssistantServiceCallAction(APIServer *parent, bool is_event) : parent_(parent), is_event_(is_event) {} TEMPLATABLE_STRING_VALUE(service); template void add_data(std::string key, T value) { this->data_.push_back(TemplatableKeyValuePair(key, value)); } template void add_data_template(std::string key, T value) { this->data_template_.push_back(TemplatableKeyValuePair(key, value)); } template void add_variable(std::string key, T value) { this->variables_.push_back(TemplatableKeyValuePair(key, value)); } void play(Ts... x) override { HomeassistantServiceResponse resp; resp.service = this->service_.value(x...); resp.is_event = this->is_event_; for (auto &it : this->data_) { HomeassistantServiceMap kv; kv.key = it.key; kv.value = it.value.value(x...); resp.data.push_back(kv); } for (auto &it : this->data_template_) { HomeassistantServiceMap kv; kv.key = it.key; kv.value = it.value.value(x...); resp.data_template.push_back(kv); } for (auto &it : this->variables_) { HomeassistantServiceMap kv; kv.key = it.key; kv.value = it.value.value(x...); resp.variables.push_back(kv); } this->parent_->send_homeassistant_service_call(resp); } protected: APIServer *parent_; bool is_event_; std::vector> data_; std::vector> data_template_; std::vector> variables_; }; } // namespace api } // namespace esphome