1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-13 15:23:49 +01:00

as const object

This commit is contained in:
J. Nick Koston
2025-10-07 08:58:14 -05:00
committed by Jesse Hills
parent cd4c4eab35
commit cbd30ce37a
3 changed files with 25 additions and 26 deletions

View File

@@ -408,7 +408,7 @@ async def homeassistant_service_to_code(
cg.add_define("USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON") cg.add_define("USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON")
await automation.build_automation( await automation.build_automation(
var.get_success_trigger_with_response(), var.get_success_trigger_with_response(),
[(cg.JsonObject, "response"), *args], [(cg.JsonObjectConst, "response"), *args],
on_success, on_success,
) )

View File

@@ -114,7 +114,7 @@ class APIServer : public Component, public Controller {
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
// Action response handling // Action response handling
using ActionResponseCallback = std::function<void(std::shared_ptr<class ActionResponse>)>; using ActionResponseCallback = std::function<void(const class ActionResponse &)>;
void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback); void register_action_response_callback(uint32_t call_id, ActionResponseCallback callback);
void handle_action_response(uint32_t call_id, bool success, const std::string &error_message); void handle_action_response(uint32_t call_id, bool success, const std::string &error_message);
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON

View File

@@ -62,7 +62,6 @@ class ActionResponse {
if (data == nullptr || data_len == 0) if (data == nullptr || data_len == 0)
return; return;
this->json_document_ = json::parse_json(data, data_len); this->json_document_ = json::parse_json(data, data_len);
this->json_ = this->json_document_.as<JsonObject>();
} }
#endif #endif
@@ -70,8 +69,8 @@ class ActionResponse {
const std::string &get_error_message() const { return this->error_message_; } const std::string &get_error_message() const { return this->error_message_; }
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
// Get data as parsed JSON object // Get data as parsed JSON object (const version returns read-only view)
JsonObject get_json() { return this->json_; } JsonObjectConst get_json() const { return this->json_document_.as<JsonObjectConst>(); }
#endif #endif
protected: protected:
@@ -79,12 +78,11 @@ class ActionResponse {
std::string error_message_; std::string error_message_;
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
JsonDocument json_document_; JsonDocument json_document_;
JsonObject json_;
#endif #endif
}; };
// Callback type for action responses // Callback type for action responses
template<typename... Ts> using ActionResponseCallback = std::function<void(std::shared_ptr<ActionResponse>, Ts...)>; template<typename... Ts> using ActionResponseCallback = std::function<void(const ActionResponse &, Ts...)>;
#endif #endif
template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts...> { template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts...> {
@@ -116,7 +114,9 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
void set_wants_response() { this->flags_.wants_response = true; } void set_wants_response() { this->flags_.wants_response = true; }
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
Trigger<JsonObject, Ts...> *get_success_trigger_with_response() const { return this->success_trigger_with_response_; } Trigger<JsonObjectConst, Ts...> *get_success_trigger_with_response() const {
return this->success_trigger_with_response_;
}
#endif #endif
Trigger<Ts...> *get_success_trigger() const { return this->success_trigger_; } Trigger<Ts...> *get_success_trigger() const { return this->success_trigger_; }
Trigger<std::string, Ts...> *get_error_trigger() const { return this->error_trigger_; } Trigger<std::string, Ts...> *get_error_trigger() const { return this->error_trigger_; }
@@ -164,25 +164,24 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
#endif #endif
auto captured_args = std::make_tuple(x...); auto captured_args = std::make_tuple(x...);
this->parent_->register_action_response_callback( this->parent_->register_action_response_callback(call_id, [this, captured_args](const ActionResponse &response) {
call_id, [this, captured_args](std::shared_ptr<ActionResponse> response) { std::apply(
std::apply( [this, &response](auto &&...args) {
[this, &response](auto &&...args) { if (response.is_success()) {
if (response->is_success()) {
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
if (this->flags_.wants_response) { if (this->flags_.wants_response) {
this->success_trigger_with_response_->trigger(response->get_json(), args...); this->success_trigger_with_response_->trigger(response.get_json(), args...);
} else } else
#endif #endif
{ {
this->success_trigger_->trigger(args...); this->success_trigger_->trigger(args...);
} }
} else { } else {
this->error_trigger_->trigger(response->get_error_message(), args...); this->error_trigger_->trigger(response.get_error_message(), args...);
} }
}, },
captured_args); captured_args);
}); });
} }
#endif #endif
@@ -198,7 +197,7 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
TemplatableStringValue<Ts...> response_template_{""}; TemplatableStringValue<Ts...> response_template_{""};
Trigger<JsonObject, Ts...> *success_trigger_with_response_ = new Trigger<JsonObject, Ts...>(); Trigger<JsonObjectConst, Ts...> *success_trigger_with_response_ = new Trigger<JsonObjectConst, Ts...>();
#endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON #endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON
Trigger<Ts...> *success_trigger_ = new Trigger<Ts...>(); Trigger<Ts...> *success_trigger_ = new Trigger<Ts...>();
Trigger<std::string, Ts...> *error_trigger_ = new Trigger<std::string, Ts...>(); Trigger<std::string, Ts...> *error_trigger_ = new Trigger<std::string, Ts...>();