1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-14 15:53:48 +01:00

remove std::map, only 1 or 2 callbacks in flight ever

This commit is contained in:
J. Nick Koston
2025-10-07 09:47:52 -05:00
committed by Jesse Hills
parent 1f557b46b3
commit cd4c4eab35
2 changed files with 22 additions and 14 deletions

View File

@@ -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<ActionResponse>(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<ActionResponse>(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

View File

@@ -199,7 +199,11 @@ class APIServer : public Component, public Controller {
std::vector<UserServiceDescriptor *> user_services_;
#endif
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
std::map<uint32_t, ActionResponseCallback> action_response_callbacks_;
struct PendingActionResponse {
uint32_t call_id;
ActionResponseCallback callback;
};
std::vector<PendingActionResponse> action_response_callbacks_;
#endif
// Group smaller types together