ESPHome 2025.9.0-dev
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 UserServiceBase<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 : UserServiceBase<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 auto *service = new CustomAPIDeviceService<T, Ts...>(name, arg_names, (T *) this, callback); // NOLINT
58 }
59#else
60 template<typename T, typename... Ts>
61 void register_service(void (T::*callback)(Ts...), const std::string &name,
62 const std::array<std::string, sizeof...(Ts)> &arg_names) {
63 static_assert(
64 sizeof(T) == 0,
65 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
66 }
67#endif
68
87#ifdef USE_API_SERVICES
88 template<typename T> void register_service(void (T::*callback)(), const std::string &name) {
89 auto *service = new CustomAPIDeviceService<T>(name, {}, (T *) this, callback); // NOLINT
91 }
92#else
93 template<typename T> void register_service(void (T::*callback)(), const std::string &name) {
94 static_assert(
95 sizeof(T) == 0,
96 "register_service() requires 'custom_services: true' in the 'api:' section of your YAML configuration");
97 }
98#endif
99
100#ifdef USE_API_HOMEASSISTANT_STATES
120 template<typename T>
121 void subscribe_homeassistant_state(void (T::*callback)(std::string), const std::string &entity_id,
122 const std::string &attribute = "") {
123 auto f = std::bind(callback, (T *) this, std::placeholders::_1);
125 }
126
146 template<typename T>
147 void subscribe_homeassistant_state(void (T::*callback)(std::string, std::string), const std::string &entity_id,
148 const std::string &attribute = "") {
149 auto f = std::bind(callback, (T *) this, entity_id, std::placeholders::_1);
151 }
152#else
153 template<typename T>
154 void subscribe_homeassistant_state(void (T::*callback)(std::string), const std::string &entity_id,
155 const std::string &attribute = "") {
156 static_assert(sizeof(T) == 0,
157 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
158 "of your YAML configuration");
159 }
160
161 template<typename T>
162 void subscribe_homeassistant_state(void (T::*callback)(std::string, std::string), const std::string &entity_id,
163 const std::string &attribute = "") {
164 static_assert(sizeof(T) == 0,
165 "subscribe_homeassistant_state() requires 'homeassistant_states: true' in the 'api:' section "
166 "of your YAML configuration");
167 }
168#endif
169
170#ifdef USE_API_HOMEASSISTANT_SERVICES
181 void call_homeassistant_service(const std::string &service_name) {
183 resp.set_service(StringRef(service_name));
185 }
186
201 void call_homeassistant_service(const std::string &service_name, const std::map<std::string, std::string> &data) {
203 resp.set_service(StringRef(service_name));
204 for (auto &it : data) {
205 resp.data.emplace_back();
206 auto &kv = resp.data.back();
207 kv.set_key(StringRef(it.first));
208 kv.value = it.second;
209 }
211 }
212
223 void fire_homeassistant_event(const std::string &event_name) {
225 resp.set_service(StringRef(event_name));
226 resp.is_event = true;
228 }
229
243 void fire_homeassistant_event(const std::string &service_name, const std::map<std::string, std::string> &data) {
245 resp.set_service(StringRef(service_name));
246 resp.is_event = true;
247 for (auto &it : data) {
248 resp.data.emplace_back();
249 auto &kv = resp.data.back();
250 kv.set_key(StringRef(it.first));
251 kv.value = it.second;
252 }
254 }
255#else
256 template<typename T = void> void call_homeassistant_service(const std::string &service_name) {
257 static_assert(sizeof(T) == 0, "call_homeassistant_service() requires 'homeassistant_services: true' in the 'api:' "
258 "section of your YAML configuration");
259 }
260
261 template<typename T = void>
262 void call_homeassistant_service(const std::string &service_name, const std::map<std::string, std::string> &data) {
263 static_assert(sizeof(T) == 0, "call_homeassistant_service() requires 'homeassistant_services: true' in the 'api:' "
264 "section of your YAML configuration");
265 }
266
267 template<typename T = void> void fire_homeassistant_event(const std::string &event_name) {
268 static_assert(sizeof(T) == 0, "fire_homeassistant_event() requires 'homeassistant_services: true' in the 'api:' "
269 "section of your YAML configuration");
270 }
271
272 template<typename T = void>
273 void fire_homeassistant_event(const std::string &service_name, const std::map<std::string, std::string> &data) {
274 static_assert(sizeof(T) == 0, "fire_homeassistant_event() requires 'homeassistant_services: true' in the 'api:' "
275 "section of your YAML configuration");
276 }
277#endif
278};
279
280} // namespace esphome::api
281#endif
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
void register_user_service(UserServiceDescriptor *descriptor)
Definition api_server.h:113
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...))
void set_service(const StringRef &ref)
Definition api_pb2.h:1095
std::vector< HomeassistantServiceMap > data
Definition api_pb2.h:1096
APIServer * global_api_server
uint16_t x
Definition tt21100.cpp:5