From cd4c4eab35ba4869d956834061557ec25c23633a Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 7 Oct 2025 09:47:52 -0500 Subject: [PATCH] remove std::map, only 1 or 2 callbacks in flight ever --- esphome/components/api/api_server.cpp | 30 +++++++++++++++------------ esphome/components/api/api_server.h | 6 +++++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 95617c75f1..778d9389ef 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -406,27 +406,31 @@ void APIServer::send_homeassistant_action(const HomeassistantActionRequest &call } #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES void APIServer::register_action_response_callback(uint32_t call_id, ActionResponseCallback callback) { - this->action_response_callbacks_[call_id] = std::move(callback); + this->action_response_callbacks_.push_back({call_id, std::move(callback)}); } void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message) { - auto it = this->action_response_callbacks_.find(call_id); - if (it != this->action_response_callbacks_.end()) { - auto callback = std::move(it->second); - this->action_response_callbacks_.erase(it); - auto response = std::make_shared(success, error_message); - callback(response); + for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) { + if (it->call_id == call_id) { + auto callback = std::move(it->callback); + this->action_response_callbacks_.erase(it); + ActionResponse response(success, error_message); + callback(response); + return; + } } } #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message, const uint8_t *response_data, size_t response_data_len) { - auto it = this->action_response_callbacks_.find(call_id); - if (it != this->action_response_callbacks_.end()) { - auto callback = std::move(it->second); - this->action_response_callbacks_.erase(it); - auto response = std::make_shared(success, error_message, response_data, response_data_len); - callback(response); + for (auto it = this->action_response_callbacks_.begin(); it != this->action_response_callbacks_.end(); ++it) { + if (it->call_id == call_id) { + auto callback = std::move(it->callback); + this->action_response_callbacks_.erase(it); + ActionResponse response(success, error_message, response_data, response_data_len); + callback(response); + return; + } } } #endif // USE_API_HOMEASSISTANT_ACTION_RESPONSES_JSON diff --git a/esphome/components/api/api_server.h b/esphome/components/api/api_server.h index cd6c51cad2..4f7a3f93d8 100644 --- a/esphome/components/api/api_server.h +++ b/esphome/components/api/api_server.h @@ -199,7 +199,11 @@ class APIServer : public Component, public Controller { std::vector user_services_; #endif #ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES - std::map action_response_callbacks_; + struct PendingActionResponse { + uint32_t call_id; + ActionResponseCallback callback; + }; + std::vector action_response_callbacks_; #endif // Group smaller types together