ESPHome 2025.5.0
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
10namespace esphome {
11namespace api {
12
19
20template<typename T> T get_execute_arg_value(const ExecuteServiceArgument &arg);
21
23
24template<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
65template<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
std::vector< ExecuteServiceArgument > args
Definition api_pb2.h:1020
std::vector< ListEntitiesServicesArgument > args
Definition api_pb2.h:984
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)
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint32_t fnv1_hash(const std::string &str)
Calculate a FNV-1 hash of str.
Definition helpers.cpp:186
uint16_t x
Definition tt21100.cpp:5