diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 1c74b2d355..58828c131d 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -408,7 +408,7 @@ async def homeassistant_service_to_code( cg.add_define("USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON") await automation.build_automation( var.get_success_trigger_with_response(), - [(cg.JsonObject, "response"), *args], + [(cg.JsonObjectConst, "response"), *args], on_success, ) diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index 4f7a3f93d8..5d038e5ddd 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -114,7 +114,7 @@ class APIServer : public Component, public Controller { #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES // Action response handling - using ActionResponseCallback = std::function)>; + using ActionResponseCallback = std::function; 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); #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON diff --git a/esphome/components/api/homeassistant_service.h b/esphome/components/api/homeassistant_service.h index 28d5b7f456..730024f7b7 100644 --- a/esphome/components/api/homeassistant_service.h +++ b/esphome/components/api/homeassistant_service.h @@ -62,7 +62,6 @@ class ActionResponse { if (data == nullptr || data_len == 0) return; this->json_document_ = json::parse_json(data, data_len); - this->json_ = this->json_document_.as(); } #endif @@ -70,8 +69,8 @@ class ActionResponse { const std::string &get_error_message() const { return this->error_message_; } #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON - // Get data as parsed JSON object - JsonObject get_json() { return this->json_; } + // Get data as parsed JSON object (const version returns read-only view) + JsonObjectConst get_json() const { return this->json_document_.as(); } #endif protected: @@ -79,12 +78,11 @@ class ActionResponse { std::string error_message_; #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON JsonDocument json_document_; - JsonObject json_; #endif }; // Callback type for action responses -template using ActionResponseCallback = std::function, Ts...)>; +template using ActionResponseCallback = std::function; #endif template class HomeAssistantServiceCallAction : public Action { @@ -116,7 +114,9 @@ template class HomeAssistantServiceCallAction : public Actionflags_.wants_response = true; } #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON - Trigger *get_success_trigger_with_response() const { return this->success_trigger_with_response_; } + Trigger *get_success_trigger_with_response() const { + return this->success_trigger_with_response_; + } #endif Trigger *get_success_trigger() const { return this->success_trigger_; } Trigger *get_error_trigger() const { return this->error_trigger_; } @@ -164,25 +164,24 @@ template class HomeAssistantServiceCallAction : public Actionparent_->register_action_response_callback( - call_id, [this, captured_args](std::shared_ptr response) { - std::apply( - [this, &response](auto &&...args) { - if (response->is_success()) { + this->parent_->register_action_response_callback(call_id, [this, captured_args](const ActionResponse &response) { + std::apply( + [this, &response](auto &&...args) { + if (response.is_success()) { #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON - if (this->flags_.wants_response) { - this->success_trigger_with_response_->trigger(response->get_json(), args...); - } else + if (this->flags_.wants_response) { + this->success_trigger_with_response_->trigger(response.get_json(), args...); + } else #endif - { - this->success_trigger_->trigger(args...); - } - } else { - this->error_trigger_->trigger(response->get_error_message(), args...); - } - }, - captured_args); - }); + { + this->success_trigger_->trigger(args...); + } + } else { + this->error_trigger_->trigger(response.get_error_message(), args...); + } + }, + captured_args); + }); } #endif @@ -198,7 +197,7 @@ template class HomeAssistantServiceCallAction : public Action response_template_{""}; - Trigger *success_trigger_with_response_ = new Trigger(); + Trigger *success_trigger_with_response_ = new Trigger(); #endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON Trigger *success_trigger_ = new Trigger(); Trigger *error_trigger_ = new Trigger();