5#ifdef USE_API_HOMEASSISTANT_SERVICES
10#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
23 static_assert(std::is_constructible_v<
TemplatableValue<std::string, X...>,
const char *>,
24 "Base class must have const char* constructor for STATIC_STRING optimization");
28 template<
typename T>
static std::string value_to_string(T &&
val) {
return to_string(std::forward<T>(
val)); }
31 static std::string value_to_string(
char *
val) {
32 return val ? std::string(
val) : std::string();
34 static std::string value_to_string(
const char *
val) {
return std::string(
val); }
35 static std::string value_to_string(
const std::string &
val) {
return val; }
36 static std::string value_to_string(std::string &&
val) {
return std::move(
val); }
41 template<
typename F, enable_if_t<!is_invocable<F, X...>
::value,
int> = 0>
44 template<
typename F, enable_if_t<is_invocable<F, X...>
::value,
int> = 0>
46 :
TemplatableValue<std::string, X...>([f](X...
x) -> std::string { return value_to_string(f(
x...)); }) {}
59 const char *key{
nullptr};
63#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
70 ActionResponse(
bool success,
const std::string &error_message) : success_(success), error_message_(error_message) {}
72#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
73 ActionResponse(
bool success,
const std::string &error_message,
const uint8_t *data,
size_t data_len)
74 : success_(success), error_message_(error_message) {
75 if (data ==
nullptr || data_len == 0)
77 this->json_document_ = json::parse_json(data, data_len);
85#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
87 JsonObjectConst
get_json()
const {
return this->json_document_.as<JsonObjectConst>(); }
93#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
105 this->flags_.is_event = is_event;
108 template<
typename T>
void set_service(T service) { this->service_ = service; }
112 void init_data(
size_t count) { this->data_.init(count); }
119 template<
typename V>
void add_data(
const char *key, V &&value) {
120 this->add_kv_(this->data_, key, std::forward<V>(value));
123 this->add_kv_(this->data_template_, key, std::forward<V>(value));
126 this->add_kv_(this->variables_, key, std::forward<V>(value));
129#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
131 this->response_template_ = response_template;
132 this->flags_.has_response_template =
true;
138#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
140 return this->success_trigger_with_response_;
147 void play(
const Ts &...
x)
override {
149 std::string service_value = this->service_.value(
x...);
151 resp.
is_event = this->flags_.is_event;
152 this->populate_service_map(resp.
data, this->data_,
x...);
153 this->populate_service_map(resp.
data_template, this->data_template_,
x...);
154 this->populate_service_map(resp.
variables, this->variables_,
x...);
156#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
157 if (this->flags_.wants_status) {
159 static uint32_t call_id_counter = 1;
160 uint32_t call_id = call_id_counter++;
162#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
163 if (this->flags_.wants_response) {
166 if (this->flags_.has_response_template) {
167 std::string response_template_value = this->response_template_.value(
x...);
173 auto captured_args = std::make_tuple(
x...);
174 this->parent_->register_action_response_callback(call_id, [
this, captured_args](
const ActionResponse &response) {
176 [
this, &response](
auto &&...args) {
178#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
179 if (this->flags_.wants_response) {
180 this->success_trigger_with_response_->trigger(response.
get_json(), args...);
184 this->success_trigger_->trigger(args...);
195 this->parent_->send_homeassistant_action(resp);
202 auto &kv = vec.emplace_back();
204 kv.value = std::forward<V>(value);
207 template<
typename VectorType,
typename SourceType>
209 dest.init(source.size());
210 for (
auto &it : source) {
211 auto &kv = dest.emplace_back();
213 kv.value = it.value.value(
x...);
222#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
223#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
225 Trigger<JsonObjectConst, Ts...> *success_trigger_with_response_ =
new Trigger<JsonObjectConst, Ts...>();
Fixed-capacity vector - allocates once at runtime, never reallocates This avoids std::vector template...
StringRef is a reference to a string owned by something else.
std::string value(X... x)
ActionResponse(bool success, const std::string &error_message)
JsonDocument json_document_
ActionResponse(bool success, const std::string &error_message, const uint8_t *data, size_t data_len)
const StringRef & get_error_message() const
JsonObjectConst get_json() const
void set_service(T service)
HomeAssistantServiceCallAction(APIServer *parent, bool is_event)
void add_data(const char *key, V &&value)
void play(const Ts &...x) override
void add_variable(const char *key, V &&value)
Trigger< JsonObjectConst, Ts... > * get_success_trigger_with_response() const
FixedVector< TemplatableKeyValuePair< Ts... > > data_
void add_kv_(FixedVector< TemplatableKeyValuePair< Ts... > > &vec, const char *key, V &&value)
Trigger< Ts... > * get_success_trigger() const
FixedVector< TemplatableKeyValuePair< Ts... > > variables_
void set_response_template(T response_template)
void init_data(size_t count)
FixedVector< TemplatableKeyValuePair< Ts... > > data_template_
void add_data_template(const char *key, V &&value)
static void populate_service_map(VectorType &dest, SourceType &source, Ts... x)
void init_data_template(size_t count)
void init_variables(size_t count)
void set_wants_response()
Trigger< std::string, Ts... > * get_error_trigger() const
FixedVector< HomeassistantServiceMap > variables
FixedVector< HomeassistantServiceMap > data
std::string response_template
FixedVector< HomeassistantServiceMap > data_template
void set_service(const StringRef &ref)
TemplatableStringValue< Ts... > value
TemplatableKeyValuePair(const char *key, T value)
TemplatableKeyValuePair()=default
TemplatableStringValue(F value)
TemplatableStringValue(F f)
std::function< void(const ActionResponse &, Ts...)> ActionResponseCallback
uint8_t has_response_template