ESPHome  2024.11.0
user_services.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <utility>
4 #include <vector>
5 
8 #include "api_pb2.h"
9 
10 namespace esphome {
11 namespace api {
12 
14  public:
16 
17  virtual bool execute_service(const ExecuteServiceRequest &req) = 0;
18 };
19 
20 template<typename T> T get_execute_arg_value(const ExecuteServiceArgument &arg);
21 
22 template<typename T> enums::ServiceArgType to_service_arg_type();
23 
24 template<typename... Ts> class UserServiceBase : public UserServiceDescriptor {
25  public:
26  UserServiceBase(std::string name, const std::array<std::string, sizeof...(Ts)> &arg_names)
27  : name_(std::move(name)), arg_names_(arg_names) {
28  this->key_ = fnv1_hash(this->name_);
29  }
30 
33  msg.name = this->name_;
34  msg.key = this->key_;
35  std::array<enums::ServiceArgType, sizeof...(Ts)> arg_types = {to_service_arg_type<Ts>()...};
36  for (int i = 0; i < sizeof...(Ts); i++) {
38  arg.type = arg_types[i];
39  arg.name = this->arg_names_[i];
40  msg.args.push_back(arg);
41  }
42  return msg;
43  }
44 
45  bool execute_service(const ExecuteServiceRequest &req) override {
46  if (req.key != this->key_)
47  return false;
48  if (req.args.size() != this->arg_names_.size())
49  return false;
50  this->execute_(req.args, typename gens<sizeof...(Ts)>::type());
51  return true;
52  }
53 
54  protected:
55  virtual void execute(Ts... x) = 0;
56  template<int... S> void execute_(std::vector<ExecuteServiceArgument> args, seq<S...> type) {
57  this->execute((get_execute_arg_value<Ts>(args[S]))...);
58  }
59 
60  std::string name_;
61  uint32_t key_{0};
62  std::array<std::string, sizeof...(Ts)> arg_names_;
63 };
64 
65 template<typename... Ts> class UserServiceTrigger : public UserServiceBase<Ts...>, public Trigger<Ts...> {
66  public:
67  UserServiceTrigger(const std::string &name, const std::array<std::string, sizeof...(Ts)> &arg_names)
68  : UserServiceBase<Ts...>(name, arg_names) {}
69 
70  protected:
71  void execute(Ts... x) override { this->trigger(x...); } // NOLINT
72 };
73 
74 } // namespace api
75 } // namespace esphome
const char * name
Definition: stm32flash.h:78
enums::ServiceArgType to_service_arg_type()
uint16_t x
Definition: tt21100.cpp:17
std::vector< ListEntitiesServicesArgument > args
Definition: api_pb2.h:903
UserServiceTrigger(const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names)
Definition: user_services.h:67
STL namespace.
void execute_(std::vector< ExecuteServiceArgument > args, seq< S... > type)
Definition: user_services.h:56
T get_execute_arg_value(const ExecuteServiceArgument &arg)
void execute(Ts... x) override
Definition: user_services.h:71
ListEntitiesServicesResponse encode_list_service_response() override
Definition: user_services.h:31
bool execute_service(const ExecuteServiceRequest &req) override
Definition: user_services.h:45
UserServiceBase(std::string name, const std::array< std::string, sizeof...(Ts)> &arg_names)
Definition: user_services.h:26
uint8_t type
std::vector< ExecuteServiceArgument > args
Definition: api_pb2.h:937
virtual ListEntitiesServicesResponse encode_list_service_response()=0
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition: helpers.cpp:183
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual bool execute_service(const ExecuteServiceRequest &req)=0