ESPHome 2025.7.1
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 private:
15 // Helper to convert value to string - handles the case where value is already a string
16 template<typename T> static std::string value_to_string(T &&val) { return to_string(std::forward<T>(val)); }
17
18 // Overloads for string types - needed because std::to_string doesn't support them
19 static std::string value_to_string(const char *val) { return std::string(val); } // For lambdas returning .c_str()
20 static std::string value_to_string(const std::string &val) { return val; }
21 static std::string value_to_string(std::string &&val) { return std::move(val); }
22
23 public:
24 TemplatableStringValue() : TemplatableValue<std::string, X...>() {}
25
26 template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
28
29 template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
31 : TemplatableValue<std::string, X...>([f](X... x) -> std::string { return value_to_string(f(x...)); }) {}
32};
33
34template<typename... Ts> class TemplatableKeyValuePair {
35 public:
36 template<typename T> TemplatableKeyValuePair(std::string key, T value) : key(std::move(key)), value(value) {}
37 std::string key;
39};
40
41template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts...> {
42 public:
43 explicit HomeAssistantServiceCallAction(APIServer *parent, bool is_event) : parent_(parent), is_event_(is_event) {}
44
45 template<typename T> void set_service(T service) { this->service_ = service; }
46
47 template<typename T> void add_data(std::string key, T value) {
48 this->data_.push_back(TemplatableKeyValuePair<Ts...>(key, value));
49 }
50 template<typename T> void add_data_template(std::string key, T value) {
51 this->data_template_.push_back(TemplatableKeyValuePair<Ts...>(key, value));
52 }
53 template<typename T> void add_variable(std::string key, T value) {
54 this->variables_.push_back(TemplatableKeyValuePair<Ts...>(key, value));
55 }
56
57 void play(Ts... x) override {
59 resp.service = this->service_.value(x...);
60 resp.is_event = this->is_event_;
61 for (auto &it : this->data_) {
63 kv.key = it.key;
64 kv.value = it.value.value(x...);
65 resp.data.push_back(kv);
66 }
67 for (auto &it : this->data_template_) {
69 kv.key = it.key;
70 kv.value = it.value.value(x...);
71 resp.data_template.push_back(kv);
72 }
73 for (auto &it : this->variables_) {
75 kv.key = it.key;
76 kv.value = it.value.value(x...);
77 resp.variables.push_back(kv);
78 }
79 this->parent_->send_homeassistant_service_call(resp);
80 }
81
82 protected:
85 TemplatableStringValue<Ts...> service_{};
86 std::vector<TemplatableKeyValuePair<Ts...>> data_;
89};
90
91} // namespace api
92} // namespace esphome
93#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:1050
std::vector< HomeassistantServiceMap > data
Definition api_pb2.h:1049
std::vector< HomeassistantServiceMap > variables
Definition api_pb2.h:1051
TemplatableKeyValuePair(std::string key, T value)
TemplatableStringValue< Ts... > value
mopeka_std_values val[4]
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5