ESPHome 2025.9.0-dev
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#ifdef USE_API_HOMEASSISTANT_SERVICES
6#include "api_pb2.h"
9#include <vector>
10
11namespace esphome::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(char *val) {
20 return val ? std::string(val) : std::string();
21 } // For lambdas returning char* (e.g., itoa)
22 static std::string value_to_string(const char *val) { return std::string(val); } // For lambdas returning .c_str()
23 static std::string value_to_string(const std::string &val) { return val; }
24 static std::string value_to_string(std::string &&val) { return std::move(val); }
25
26 public:
27 TemplatableStringValue() : TemplatableValue<std::string, X...>() {}
28
29 template<typename F, enable_if_t<!is_invocable<F, X...>::value, int> = 0>
31
32 template<typename F, enable_if_t<is_invocable<F, X...>::value, int> = 0>
34 : TemplatableValue<std::string, X...>([f](X... x) -> std::string { return value_to_string(f(x...)); }) {}
35};
36
37template<typename... Ts> class TemplatableKeyValuePair {
38 public:
39 // Keys are always string literals from YAML dictionary keys (e.g., "code", "event")
40 // and never templatable values or lambdas. Only the value parameter can be a lambda/template.
41 // Using pass-by-value with std::move allows optimal performance for both lvalues and rvalues.
42 template<typename T> TemplatableKeyValuePair(std::string key, T value) : key(std::move(key)), value(value) {}
43 std::string key;
45};
46
47template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts...> {
48 public:
49 explicit HomeAssistantServiceCallAction(APIServer *parent, bool is_event) : parent_(parent), is_event_(is_event) {}
50
51 template<typename T> void set_service(T service) { this->service_ = service; }
52
53 // Keys are always string literals from the Python code generation (e.g., cg.add(var.add_data("tag_id", templ))).
54 // The value parameter can be a lambda/template, but keys are never templatable.
55 // Using pass-by-value allows the compiler to optimize for both lvalues and rvalues.
56 template<typename T> void add_data(std::string key, T value) { this->data_.emplace_back(std::move(key), value); }
57 template<typename T> void add_data_template(std::string key, T value) {
58 this->data_template_.emplace_back(std::move(key), value);
59 }
60 template<typename T> void add_variable(std::string key, T value) {
61 this->variables_.emplace_back(std::move(key), value);
62 }
63
64 void play(Ts... x) override {
66 std::string service_value = this->service_.value(x...);
67 resp.set_service(StringRef(service_value));
68 resp.is_event = this->is_event_;
69 for (auto &it : this->data_) {
70 resp.data.emplace_back();
71 auto &kv = resp.data.back();
72 kv.set_key(StringRef(it.key));
73 kv.value = it.value.value(x...);
74 }
75 for (auto &it : this->data_template_) {
76 resp.data_template.emplace_back();
77 auto &kv = resp.data_template.back();
78 kv.set_key(StringRef(it.key));
79 kv.value = it.value.value(x...);
80 }
81 for (auto &it : this->variables_) {
82 resp.variables.emplace_back();
83 auto &kv = resp.variables.back();
84 kv.set_key(StringRef(it.key));
85 kv.value = it.value.value(x...);
86 }
87 this->parent_->send_homeassistant_service_call(resp);
88 }
89
90 protected:
93 TemplatableStringValue<Ts...> service_{};
94 std::vector<TemplatableKeyValuePair<Ts...>> data_;
97};
98
99} // namespace esphome::api
100#endif
101#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
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:1097
void set_service(const StringRef &ref)
Definition api_pb2.h:1095
std::vector< HomeassistantServiceMap > data
Definition api_pb2.h:1096
std::vector< HomeassistantServiceMap > variables
Definition api_pb2.h:1098
TemplatableKeyValuePair(std::string key, T value)
TemplatableStringValue< Ts... > value
mopeka_std_values val[4]
uint16_t x
Definition tt21100.cpp:5