ESPHome 2025.5.0
Loading...
Searching...
No Matches
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"
8#include <vector>
9
10namespace esphome {
11namespace api {
12
13template<typename... X> class TemplatableStringValue : public TemplatableValue<std::string, X...> {
14 public:
15 TemplatableStringValue() : TemplatableValue<std::string, X...>() {}
16
17 template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
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
25template<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
32template<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:
76 TemplatableStringValue<Ts...> service_{};
77 std::vector<TemplatableKeyValuePair<Ts...>> data_;
80};
81
82} // namespace api
83} // namespace esphome
84#endif
HomeAssistantServiceCallAction(APIServer *parent, bool is_event)
std::vector< TemplatableKeyValuePair< Ts... > > variables_
std::vector< TemplatableKeyValuePair< Ts... > > data_template_
std::vector< TemplatableKeyValuePair< Ts... > > data_
std::vector< HomeassistantServiceMap > data_template
Definition api_pb2.h:892
std::vector< HomeassistantServiceMap > data
Definition api_pb2.h:891
std::vector< HomeassistantServiceMap > variables
Definition api_pb2.h:893
TemplatableKeyValuePair(std::string key, T value)
TemplatableStringValue< Ts... > value
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
typename std::enable_if< B, T >::type enable_if_t
Definition helpers.h:94
uint16_t x
Definition tt21100.cpp:5