5#ifdef USE_API_HOMEASSISTANT_SERVICES
10#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
21 template<
typename T>
static std::string value_to_string(T &&
val) {
return to_string(std::forward<T>(
val)); }
24 static std::string value_to_string(
char *
val) {
25 return val ? std::string(
val) : std::string();
27 static std::string value_to_string(
const char *
val) {
return std::string(
val); }
28 static std::string value_to_string(
const std::string &
val) {
return val; }
29 static std::string value_to_string(std::string &&
val) {
return std::move(
val); }
34 template<
typename F, enable_if_t<!is_invocable<F, X...>
::value,
int> = 0>
37 template<
typename F, enable_if_t<is_invocable<F, X...>
::value,
int> = 0>
39 :
TemplatableValue<std::string, X...>([f](X...
x) -> std::string { return value_to_string(f(
x...)); }) {}
52#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
57 : success_(success), error_message_(std::move(error_message)) {}
59#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
60 ActionResponse(
bool success, std::string error_message,
const uint8_t *data,
size_t data_len)
61 : success_(success), error_message_(std::move(error_message)) {
62 if (data ==
nullptr || data_len == 0)
64 this->json_document_ = json::parse_json(data, data_len);
71#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
73 JsonObjectConst
get_json()
const {
return this->json_document_.as<JsonObjectConst>(); }
79#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
91 this->flags_.is_event = is_event;
94 template<
typename T>
void set_service(T service) { this->service_ = service; }
99 template<
typename T>
void add_data(std::string key, T value) { this->data_.emplace_back(std::move(key), value); }
101 this->data_template_.emplace_back(std::move(key), value);
104 this->variables_.emplace_back(std::move(key), value);
107#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
109 this->response_template_ = response_template;
110 this->flags_.has_response_template =
true;
116#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
118 return this->success_trigger_with_response_;
127 std::string service_value = this->service_.value(
x...);
129 resp.
is_event = this->flags_.is_event;
130 for (
auto &it : this->data_) {
131 resp.
data.emplace_back();
132 auto &kv = resp.
data.back();
134 kv.value = it.value.value(
x...);
136 for (
auto &it : this->data_template_) {
140 kv.value = it.value.value(
x...);
142 for (
auto &it : this->variables_) {
146 kv.value = it.value.value(
x...);
149#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
150 if (this->flags_.wants_status) {
152 static uint32_t call_id_counter = 1;
153 uint32_t call_id = call_id_counter++;
155#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
156 if (this->flags_.wants_response) {
159 if (this->flags_.has_response_template) {
160 std::string response_template_value = this->response_template_.value(
x...);
166 auto captured_args = std::make_tuple(
x...);
167 this->parent_->register_action_response_callback(call_id, [
this, captured_args](
const ActionResponse &response) {
169 [
this, &response](
auto &&...args) {
171#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
172 if (this->flags_.wants_response) {
173 this->success_trigger_with_response_->trigger(response.
get_json(), args...);
177 this->success_trigger_->trigger(args...);
188 this->parent_->send_homeassistant_action(resp);
197#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
198#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
200 Trigger<JsonObjectConst, Ts...> *success_trigger_with_response_ =
new Trigger<JsonObjectConst, Ts...>();
StringRef is a reference to a string owned by something else.
std::string value(X... x)
ActionResponse(bool success, std::string error_message, const uint8_t *data, size_t data_len)
JsonDocument json_document_
JsonObjectConst get_json() const
const std::string & get_error_message() const
ActionResponse(bool success, std::string error_message="")
std::string error_message_
void set_service(T service)
HomeAssistantServiceCallAction(APIServer *parent, bool is_event)
std::vector< TemplatableKeyValuePair< Ts... > > variables_
Trigger< JsonObjectConst, Ts... > * get_success_trigger_with_response() const
void play(Ts... x) override
void add_variable(std::string key, T value)
std::vector< TemplatableKeyValuePair< Ts... > > data_template_
void add_data(std::string key, T value)
void add_data_template(std::string key, T value)
Trigger< Ts... > * get_success_trigger() const
void set_response_template(T response_template)
std::vector< TemplatableKeyValuePair< Ts... > > data_
void set_wants_response()
Trigger< std::string, Ts... > * get_error_trigger() const
std::vector< HomeassistantServiceMap > data_template
std::vector< HomeassistantServiceMap > data
std::vector< HomeassistantServiceMap > variables
std::string response_template
void set_service(const StringRef &ref)
TemplatableKeyValuePair(std::string key, T value)
TemplatableStringValue< Ts... > value
TemplatableStringValue(F value)
TemplatableStringValue(F f)
std::function< void(const ActionResponse &, Ts...)> ActionResponseCallback
uint8_t has_response_template