ESPHome 2025.11.0
Loading...
Searching...
No Matches
custom_api_device.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include "api_server.h"
5#ifdef USE_API
6#ifdef USE_API_SERVICES
7#include "user_services.h"
8#endif
9namespace esphome::api {
10
11#ifdef USE_API_SERVICES
12template<typename T, typename... Ts> class CustomAPIDeviceService : public UserServiceDynamic<Ts...> {
13 public:
14 CustomAPIDeviceService(const std::string &name, const std::array<std::string, sizeof...(Ts)> &arg_names, T *obj,
15 void (T::*callback)(Ts...))
16 : UserServiceDynamic<Ts...>(name, arg_names), obj_(obj), callback_(callback) {}
17
18 protected:
19 void execute(Ts... x) override { (this->obj_->*this->callback_)(x...); } // NOLINT
20
21 T *obj_;
22 void (T::*callback_)(Ts...);
23};
24#endif // USE_API_SERVICES
25
27 public:
29 bool is_connected() const { return global_api_server->is_connected(); }
30
52#ifdef USE_API_SERVICES
53 template<typename T, typename... Ts>
54 void register_service(void (T::*callback)(Ts...), const std::string &name,
55 const std::array<std::string, sizeof...(Ts)> &arg_names) {
56#ifdef USE_API_CUSTOM_SERVICES
57 auto *service = new CustomAPIDeviceService<T, Ts...>(name, arg_names, (T *) this, callback); // NOLINT
59#else
60 static_assert(
61 sizeof(T) == 0,
62 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
63#endif
64 }
65#else
66 template<typename T, typename... Ts>
67 void register_service(void (T::*callback)(Ts...), const std::string &name,
68 const std::array<std::string, sizeof...(Ts)> &arg_names) {
69 static_assert(
70 sizeof(T) == 0,
71 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
72 }
73#endif
74
93#ifdef USE_API_SERVICES
94 template<typename T> void register_service(void (T::*callback)(), const std::string &name) {
95#ifdef USE_API_CUSTOM_SERVICES
96 auto *service = new CustomAPIDeviceService<T>(name, {}, (T *) this, callback); // NOLINT
98#else
99 static_assert(
100 sizeof(T) == 0,
101 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
102#endif
103 }
104#else
105 template<typename T> void register_service(void (T::*callback)(), const std::string &name) {
106 static_assert(
107 sizeof(T) == 0,
108 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
109 }
110#endif
111
112#ifdef USE_API_HOMEASSISTANT_STATES
132 template<typename T>
133 void subscribe_homeassistant_state(void (T::*callback)(std::string), const std::string &entity_id,
134 const std::string &attribute = "") {
135 auto f = std::bind(callback, (T *) this, std::placeholders::_1);
137 }
138
158 template<typename T>
159 void subscribe_homeassistant_state(void (T::*callback)(std::string, std::string), const std::string &entity_id,
160 const std::string &attribute = "") {
161 auto f = std::bind(callback, (T *) this, entity_id, std::placeholders::_1);
163 }
164#else
165 template<typename T>
166 void subscribe_homeassistant_state(void (T::*callback)(std::string), const std::string &entity_id,
167 const std::string &attribute = "") {
168 static_assert(sizeof(T) == 0,
169 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
170 "of your YAML configuration");
171 }
172
173 template<typename T>
174 void subscribe_homeassistant_state(void (T::*callback)(std::string, std::string), const std::string &entity_id,
175 const std::string &attribute = "") {
176 static_assert(sizeof(T) == 0,
177 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
178 "of your YAML configuration");
179 }
180#endif
181
182#ifdef USE_API_HOMEASSISTANT_SERVICES
193 void call_homeassistant_service(const std::string &service_name) {
195 resp.set_service(StringRef(service_name));
197 }
198
213 void call_homeassistant_service(const std::string &service_name, const std::map<std::string, std::string> &data) {
215 resp.set_service(StringRef(service_name));
216 resp.data.init(data.size());
217 for (auto &it : data) {
218 auto &kv = resp.data.emplace_back();
219 kv.set_key(StringRef(it.first));
220 kv.value = it.second;
221 }
223 }
224
235 void fire_homeassistant_event(const std::string &event_name) {
237 resp.set_service(StringRef(event_name));
238 resp.is_event = true;
240 }
241
255 void fire_homeassistant_event(const std::string &service_name, const std::map<std::string, std::string> &data) {
257 resp.set_service(StringRef(service_name));
258 resp.is_event = true;
259 resp.data.init(data.size());
260 for (auto &it : data) {
261 auto &kv = resp.data.emplace_back();
262 kv.set_key(StringRef(it.first));
263 kv.value = it.second;
264 }
266 }
267#else
268 template<typename T = void> void call_homeassistant_service(const std::string &service_name) {
269 static_assert(sizeof(T) == 0, "call_homeassistant_service() requires 'homeassistant_services: true' in the 'api:' "
270 "section of your YAML configuration");
271 }
272
273 template<typename T = void>
274 void call_homeassistant_service(const std::string &service_name, const std::map<std::string, std::string> &data) {
275 static_assert(sizeof(T) == 0, "call_homeassistant_service() requires 'homeassistant_services: true' in the 'api:' "
276 "section of your YAML configuration");
277 }
278
279 template<typename T = void> void fire_homeassistant_event(const std::string &event_name) {
280 static_assert(sizeof(T) == 0, "fire_homeassistant_event() requires 'homeassistant_services: true' in the 'api:' "
281 "section of your YAML configuration");
282 }
283
284 template<typename T = void>
285 void fire_homeassistant_event(const std::string &service_name, const std::map<std::string, std::string> &data) {
286 static_assert(sizeof(T) == 0, "fire_homeassistant_event() requires 'homeassistant_services: true' in the 'api:' "
287 "section of your YAML configuration");
288 }
289#endif
290};
291
292} // namespace esphome::api
293#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
void register_user_service(UserServiceDescriptor *descriptor)
Definition api_server.h:133
void send_homeassistant_action(const HomeassistantActionRequest &call)
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
void subscribe_homeassistant_state(void(T::*callback)(std::string), const std::string &entity_id, const std::string &attribute="")
Subscribe to the state (or attribute state) of an entity from Home Assistant.
void fire_homeassistant_event(const std::string &service_name, const std::map< std::string, std::string > &data)
void register_service(void(T::*callback)(Ts...), const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names)
Register a custom native API service that will show up in Home Assistant.
void fire_homeassistant_event(const std::string &event_name)
void call_homeassistant_service(const std::string &service_name)
Call a Home Assistant service from ESPHome.
void register_service(void(T::*callback)(), const std::string &name)
Register a custom native API service that will show up in Home Assistant.
void fire_homeassistant_event(const std::string &event_name)
Fire an ESPHome event in Home Assistant.
bool is_connected() const
Return if a client (such as Home Assistant) is connected to the native API.
void fire_homeassistant_event(const std::string &service_name, const std::map< std::string, std::string > &data)
Fire an ESPHome event in Home Assistant.
void call_homeassistant_service(const std::string &service_name)
void subscribe_homeassistant_state(void(T::*callback)(std::string, std::string), const std::string &entity_id, const std::string &attribute="")
Subscribe to the state (or attribute state) of an entity from Home Assistant.
void call_homeassistant_service(const std::string &service_name, const std::map< std::string, std::string > &data)
Call a Home Assistant service from ESPHome.
void call_homeassistant_service(const std::string &service_name, const std::map< std::string, std::string > &data)
CustomAPIDeviceService(const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names, T *obj, void(T::*callback)(Ts...))
FixedVector< HomeassistantServiceMap > data
Definition api_pb2.h:1113
void set_service(const StringRef &ref)
Definition api_pb2.h:1112
APIServer * global_api_server
uint16_t x
Definition tt21100.cpp:5