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...)); }) {}
56#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
61 : success_(success), error_message_(std::move(error_message)) {}
63#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
64 ActionResponse(
bool success, std::string error_message,
const uint8_t *data,
size_t data_len)
65 : success_(success), error_message_(std::move(error_message)) {
66 if (data ==
nullptr || data_len == 0)
68 this->json_document_ = json::parse_json(data, data_len);
75#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
77 JsonObjectConst
get_json()
const {
return this->json_document_.as<JsonObjectConst>(); }
83#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
95 this->flags_.is_event = is_event;
98 template<
typename T>
void set_service(T service) { this->service_ = service; }
102 void init_data(
size_t count) { this->data_.init(count); }
108 template<
typename K,
typename V>
void add_data(K &&key, V &&value) {
109 this->add_kv_(this->data_, std::forward<K>(key), std::forward<V>(value));
112 this->add_kv_(this->data_template_, std::forward<K>(key), std::forward<V>(value));
114 template<
typename K,
typename V>
void add_variable(K &&key, V &&value) {
115 this->add_kv_(this->variables_, std::forward<K>(key), std::forward<V>(value));
118#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
120 this->response_template_ = response_template;
121 this->flags_.has_response_template =
true;
127#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
129 return this->success_trigger_with_response_;
136 void play(
const Ts &...
x)
override {
138 std::string service_value = this->service_.value(
x...);
140 resp.
is_event = this->flags_.is_event;
141 this->populate_service_map(resp.
data, this->data_,
x...);
142 this->populate_service_map(resp.
data_template, this->data_template_,
x...);
143 this->populate_service_map(resp.
variables, this->variables_,
x...);
145#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
146 if (this->flags_.wants_status) {
148 static uint32_t call_id_counter = 1;
149 uint32_t call_id = call_id_counter++;
151#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
152 if (this->flags_.wants_response) {
155 if (this->flags_.has_response_template) {
156 std::string response_template_value = this->response_template_.value(
x...);
162 auto captured_args = std::make_tuple(
x...);
163 this->parent_->register_action_response_callback(call_id, [
this, captured_args](
const ActionResponse &response) {
165 [
this, &response](
auto &&...args) {
167#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
168 if (this->flags_.wants_response) {
169 this->success_trigger_with_response_->trigger(response.
get_json(), args...);
173 this->success_trigger_->trigger(args...);
184 this->parent_->send_homeassistant_action(resp);
190 auto &kv = vec.emplace_back();
191 kv.key = std::forward<K>(key);
192 kv.value = std::forward<V>(value);
195 template<
typename VectorType,
typename SourceType>
197 dest.init(source.size());
198 for (
auto &it : source) {
199 auto &kv = dest.emplace_back();
201 kv.value = it.value.value(
x...);
210#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
211#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
213 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, 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)
void add_variable(K &&key, V &&value)
void play(const Ts &...x) override
void add_kv_(FixedVector< TemplatableKeyValuePair< Ts... > > &vec, K &&key, V &&value)
Trigger< JsonObjectConst, Ts... > * get_success_trigger_with_response() const
FixedVector< TemplatableKeyValuePair< Ts... > > data_
void add_data_template(K &&key, V &&value)
Trigger< Ts... > * get_success_trigger() const
void add_data(K &&key, V &&value)
FixedVector< TemplatableKeyValuePair< Ts... > > variables_
void set_response_template(T response_template)
void init_data(size_t count)
FixedVector< TemplatableKeyValuePair< Ts... > > data_template_
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)
TemplatableKeyValuePair(std::string key, T value)
TemplatableStringValue< Ts... > value
TemplatableKeyValuePair()=default
TemplatableStringValue(F value)
TemplatableStringValue(F f)
std::function< void(const ActionResponse &, Ts...)> ActionResponseCallback
uint8_t has_response_template