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