ESPHome 2025.9.0-dev
Loading...
Searching...
No Matches
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#ifdef USE_API_SERVICES
11namespace esphome::api {
12
14 public:
16
17 virtual bool execute_service(const ExecuteServiceRequest &req) = 0;
18
19 bool is_internal() { return false; }
20};
21
22template<typename T> T get_execute_arg_value(const ExecuteServiceArgument &arg);
23
25
26template<typename... Ts> class UserServiceBase : public UserServiceDescriptor {
27 public:
28 UserServiceBase(std::string name, const std::array<std::string, sizeof...(Ts)> &arg_names)
29 : name_(std::move(name)), arg_names_(arg_names) {
30 this->key_ = fnv1_hash(this->name_);
31 }
32
35 msg.set_name(StringRef(this->name_));
36 msg.key = this->key_;
37 std::array<enums::ServiceArgType, sizeof...(Ts)> arg_types = {to_service_arg_type<Ts>()...};
38 for (int i = 0; i < sizeof...(Ts); i++) {
39 msg.args.emplace_back();
40 auto &arg = msg.args.back();
41 arg.type = arg_types[i];
42 arg.set_name(StringRef(this->arg_names_[i]));
43 }
44 return msg;
45 }
46
47 bool execute_service(const ExecuteServiceRequest &req) override {
48 if (req.key != this->key_)
49 return false;
50 if (req.args.size() != this->arg_names_.size())
51 return false;
52 this->execute_(req.args, typename gens<sizeof...(Ts)>::type());
53 return true;
54 }
55
56 protected:
57 virtual void execute(Ts... x) = 0;
58 template<int... S> void execute_(std::vector<ExecuteServiceArgument> args, seq<S...> type) {
59 this->execute((get_execute_arg_value<Ts>(args[S]))...);
60 }
61
62 std::string name_;
63 uint32_t key_{0};
64 std::array<std::string, sizeof...(Ts)> arg_names_;
65};
66
67template<typename... Ts> class UserServiceTrigger : public UserServiceBase<Ts...>, public Trigger<Ts...> {
68 public:
69 UserServiceTrigger(const std::string &name, const std::array<std::string, sizeof...(Ts)> &arg_names)
70 : UserServiceBase<Ts...>(name, arg_names) {}
71
72 protected:
73 void execute(Ts... x) override { this->trigger(x...); } // NOLINT
74};
75
76} // namespace esphome::api
77#endif // USE_API_SERVICES
StringRef is a reference to a string owned by something else.
Definition string_ref.h:22
std::vector< ExecuteServiceArgument > args
Definition api_pb2.h:1252
std::vector< ListEntitiesServicesArgument > args
Definition api_pb2.h:1215
void set_name(const StringRef &ref)
Definition api_pb2.h:1213
virtual void execute(Ts... x)=0
void execute_(std::vector< ExecuteServiceArgument > args, seq< S... > type)
UserServiceBase(std::string name, const std::array< std::string, sizeof...(Ts)> &arg_names)
std::array< std::string, sizeof...(Ts)> arg_names_
ListEntitiesServicesResponse encode_list_service_response() override
bool execute_service(const ExecuteServiceRequest &req) override
virtual ListEntitiesServicesResponse encode_list_service_response()=0
virtual bool execute_service(const ExecuteServiceRequest &req)=0
UserServiceTrigger(const std::string &name, const std::array< std::string, sizeof...(Ts)> &arg_names)
void execute(Ts... x) override
uint8_t type
enums::ServiceArgType to_service_arg_type()
T get_execute_arg_value(const ExecuteServiceArgument &arg)
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:134
uint16_t x
Definition tt21100.cpp:5