ESPHome  2024.11.1
homeassistant_service.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "api_server.h"
4 #ifdef USE_API
5 #include "api_pb2.h"
6 #include "esphome/core/helpers.h"
8 #include <vector>
9 
10 namespace esphome {
11 namespace api {
12 
13 template<typename... X> class TemplatableStringValue : public TemplatableValue<std::string, X...> {
14  public:
16 
17  template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
18  TemplatableStringValue(F value) : TemplatableValue<std::string, X...>(value) {}
19 
20  template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
22  : TemplatableValue<std::string, X...>([f](X... x) -> std::string { return to_string(f(x...)); }) {}
23 };
24 
25 template<typename... Ts> class TemplatableKeyValuePair {
26  public:
27  template<typename T> TemplatableKeyValuePair(std::string key, T value) : key(std::move(key)), value(value) {}
28  std::string key;
30 };
31 
32 template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts...> {
33  public:
34  explicit HomeAssistantServiceCallAction(APIServer *parent, bool is_event) : parent_(parent), is_event_(is_event) {}
35 
36  template<typename T> void set_service(T service) { this->service_ = service; }
37 
38  template<typename T> void add_data(std::string key, T value) {
39  this->data_.push_back(TemplatableKeyValuePair<Ts...>(key, value));
40  }
41  template<typename T> void add_data_template(std::string key, T value) {
42  this->data_template_.push_back(TemplatableKeyValuePair<Ts...>(key, value));
43  }
44  template<typename T> void add_variable(std::string key, T value) {
45  this->variables_.push_back(TemplatableKeyValuePair<Ts...>(key, value));
46  }
47 
48  void play(Ts... x) override {
50  resp.service = this->service_.value(x...);
51  resp.is_event = this->is_event_;
52  for (auto &it : this->data_) {
54  kv.key = it.key;
55  kv.value = it.value.value(x...);
56  resp.data.push_back(kv);
57  }
58  for (auto &it : this->data_template_) {
60  kv.key = it.key;
61  kv.value = it.value.value(x...);
62  resp.data_template.push_back(kv);
63  }
64  for (auto &it : this->variables_) {
66  kv.key = it.key;
67  kv.value = it.value.value(x...);
68  resp.variables.push_back(kv);
69  }
70  this->parent_->send_homeassistant_service_call(resp);
71  }
72 
73  protected:
75  bool is_event_;
76  TemplatableStringValue<Ts...> service_{};
77  std::vector<TemplatableKeyValuePair<Ts...>> data_;
79  std::vector<TemplatableKeyValuePair<Ts...>> variables_;
80 };
81 
82 } // namespace api
83 } // namespace esphome
84 #endif
std::vector< TemplatableKeyValuePair< Ts... > > data_
TemplatableStringValue< Ts... > value
uint16_t x
Definition: tt21100.cpp:17
std::vector< TemplatableKeyValuePair< Ts... > > data_template_
typename std::enable_if< B, T >::type enable_if_t
Definition: helpers.h:86
STL namespace.
HomeAssistantServiceCallAction(APIServer *parent, bool is_event)
std::vector< HomeassistantServiceMap > variables
Definition: api_pb2.h:819
std::vector< HomeassistantServiceMap > data_template
Definition: api_pb2.h:818
TemplatableKeyValuePair(std::string key, T value)
void add_data_template(std::string key, T value)
std::string to_string(int value)
Definition: helpers.cpp:81
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< TemplatableKeyValuePair< Ts... > > variables_
std::vector< HomeassistantServiceMap > data
Definition: api_pb2.h:817