ESPHome  2024.3.1
custom_api_device.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include "user_services.h"
5 #include "api_server.h"
6 
7 namespace esphome {
8 namespace api {
9 
10 template<typename T, typename... Ts> class CustomAPIDeviceService : public UserServiceBase<Ts...> {
11  public:
12  CustomAPIDeviceService(const std::string &name, const std::array<std::string, sizeof...(Ts)> &arg_names, T *obj,
13  void (T::*callback)(Ts...))
14  : UserServiceBase<Ts...>(name, arg_names), obj_(obj), callback_(callback) {}
15 
16  protected:
17  void execute(Ts... x) override { (this->obj_->*this->callback_)(x...); } // NOLINT
18 
19  T *obj_;
20  void (T::*callback_)(Ts...);
21 };
22 
24  public:
26  bool is_connected() const { return global_api_server->is_connected(); }
27 
49  template<typename T, typename... Ts>
50  void register_service(void (T::*callback)(Ts...), const std::string &name,
51  const std::array<std::string, sizeof...(Ts)> &arg_names) {
52  auto *service = new CustomAPIDeviceService<T, Ts...>(name, arg_names, (T *) this, callback); // NOLINT
54  }
55 
74  template<typename T> void register_service(void (T::*callback)(), const std::string &name) {
75  auto *service = new CustomAPIDeviceService<T>(name, {}, (T *) this, callback); // NOLINT
77  }
78 
98  template<typename T>
99  void subscribe_homeassistant_state(void (T::*callback)(std::string), const std::string &entity_id,
100  const std::string &attribute = "") {
101  auto f = std::bind(callback, (T *) this, std::placeholders::_1);
103  }
104 
124  template<typename T>
125  void subscribe_homeassistant_state(void (T::*callback)(std::string, std::string), const std::string &entity_id,
126  const std::string &attribute = "") {
127  auto f = std::bind(callback, (T *) this, entity_id, std::placeholders::_1);
129  }
130 
141  void call_homeassistant_service(const std::string &service_name) {
143  resp.service = service_name;
145  }
146 
161  void call_homeassistant_service(const std::string &service_name, const std::map<std::string, std::string> &data) {
163  resp.service = service_name;
164  for (auto &it : data) {
166  kv.key = it.first;
167  kv.value = it.second;
168  resp.data.push_back(kv);
169  }
171  }
172 
183  void fire_homeassistant_event(const std::string &event_name) {
185  resp.service = event_name;
186  resp.is_event = true;
188  }
189 
203  void fire_homeassistant_event(const std::string &service_name, const std::map<std::string, std::string> &data) {
205  resp.service = service_name;
206  resp.is_event = true;
207  for (auto &it : data) {
209  kv.key = it.first;
210  kv.value = it.second;
211  resp.data.push_back(kv);
212  }
214  }
215 };
216 
217 } // namespace api
218 } // namespace esphome
bool is_connected() const
Return if a client (such as Home Assistant) is connected to the native API.
const char * name
Definition: stm32flash.h:78
bool is_connected() const
Definition: api_server.cpp:336
CustomAPIDeviceService(const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names, T *obj, void(T::*callback)(Ts...))
uint16_t x
Definition: tt21100.cpp:17
void send_homeassistant_service_call(const HomeassistantServiceResponse &call)
Definition: api_server.cpp:308
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 fire_homeassistant_event(const std::string &event_name)
Fire an ESPHome event in Home Assistant.
void call_homeassistant_service(const std::string &service_name)
Call a Home Assistant service from ESPHome.
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 register_user_service(UserServiceDescriptor *descriptor)
Definition: api_server.h:85
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 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.
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void subscribe_home_assistant_state(std::string entity_id, optional< std::string > attribute, std::function< void(std::string)> f)
Definition: api_server.cpp:315
std::vector< HomeassistantServiceMap > data
Definition: api_pb2.h:792
void register_service(void(T::*callback)(), const std::string &name)
Register a custom native API service that will show up in Home Assistant.
APIServer * global_api_server
Definition: api_server.cpp:305