1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-01 15:41:52 +00:00

response suggestions

This commit is contained in:
J. Nick Koston
2025-10-02 15:22:21 +02:00
parent 211a8c872b
commit f2682d9df5
15 changed files with 61 additions and 28 deletions

View File

@@ -356,6 +356,7 @@ async def homeassistant_service_to_code(
cg.add(var.set_response_template(templ))
if on_response := config.get(CONF_ON_RESPONSE):
cg.add_define("USE_API_HOMEASSISTANT_ACTION_RESPONSES")
trigger = cg.new_Pvariable(
on_response[CONF_TRIGGER_ID],
template_arg,

View File

@@ -789,12 +789,12 @@ message HomeassistantActionResponse {
option (id) = 130;
option (source) = SOURCE_CLIENT;
option (no_delay) = true;
option (ifdef) = "USE_API_HOMEASSISTANT_SERVICES";
option (ifdef) = "USE_API_HOMEASSISTANT_ACTION_RESPONSES";
uint32 call_id = 1; // Matches the call_id from HomeassistantActionRequest
bool success = 2; // Whether the service call succeeded
string error_message = 3; // Error message if success = false
string response_data = 4; // Service response data
bytes response_data = 4 [(pointer_to_buffer) = true]; // Service response data
}
// ==================== IMPORT HOME ASSISTANT STATES ====================

View File

@@ -1550,9 +1550,10 @@ void APIConnection::execute_service(const ExecuteServiceRequest &msg) {
}
#endif
#ifdef USE_API_HOMEASSISTANT_SERVICES
#if defined(USE_API_HOMEASSISTANT_SERVICES) && defined(USE_API_HOMEASSISTANT_ACTION_RESPONSES)
void APIConnection::on_homeassistant_action_response(const HomeassistantActionResponse &msg) {
this->parent_->handle_action_response(msg.call_id, msg.success, msg.error_message, msg.response_data);
this->parent_->handle_action_response(msg.call_id, msg.success, msg.error_message,
reinterpret_cast<const char *>(msg.response_data), msg.response_data_len);
};
#endif
#ifdef USE_API_NOISE

View File

@@ -137,8 +137,10 @@ class APIConnection final : public APIServerConnection {
return;
this->send_message(call, HomeassistantActionRequest::MESSAGE_TYPE);
}
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
void on_homeassistant_action_response(const HomeassistantActionResponse &msg) override;
#endif
#endif
#ifdef USE_BLUETOOTH_PROXY
void subscribe_bluetooth_le_advertisements(const SubscribeBluetoothLEAdvertisementsRequest &msg) override;
void unsubscribe_bluetooth_le_advertisements(const UnsubscribeBluetoothLEAdvertisementsRequest &msg) override;

View File

@@ -896,6 +896,8 @@ void HomeassistantActionRequest::calculate_size(ProtoSize &size) const {
size.add_uint32(1, this->call_id);
size.add_length(1, this->response_template.size());
}
#endif
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
bool HomeassistantActionResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
switch (field_id) {
case 1:
@@ -914,9 +916,12 @@ bool HomeassistantActionResponse::decode_length(uint32_t field_id, ProtoLengthDe
case 3:
this->error_message = value.as_string();
break;
case 4:
this->response_data = value.as_string();
case 4: {
// Use raw data directly to avoid allocation
this->response_data = value.data();
this->response_data_len = value.size();
break;
}
default:
return false;
}

View File

@@ -1124,17 +1124,20 @@ class HomeassistantActionRequest final : public ProtoMessage {
protected:
};
#endif
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
class HomeassistantActionResponse final : public ProtoDecodableMessage {
public:
static constexpr uint8_t MESSAGE_TYPE = 130;
static constexpr uint8_t ESTIMATED_SIZE = 24;
static constexpr uint8_t ESTIMATED_SIZE = 34;
#ifdef HAS_PROTO_MESSAGE_DUMP
const char *message_name() const override { return "homeassistant_action_response"; }
#endif
uint32_t call_id{0};
bool success{false};
std::string error_message{};
std::string response_data{};
const uint8_t *response_data{nullptr};
uint16_t response_data_len{0};
#ifdef HAS_PROTO_MESSAGE_DUMP
void dump_to(std::string &out) const override;
#endif

View File

@@ -1125,12 +1125,16 @@ void HomeassistantActionRequest::dump_to(std::string &out) const {
dump_field(out, "call_id", this->call_id);
dump_field(out, "response_template", this->response_template);
}
#endif
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
void HomeassistantActionResponse::dump_to(std::string &out) const {
MessageDumpHelper helper(out, "HomeassistantActionResponse");
dump_field(out, "call_id", this->call_id);
dump_field(out, "success", this->success);
dump_field(out, "error_message", this->error_message);
dump_field(out, "response_data", this->response_data);
out.append(" response_data: ");
out.append(format_hex_pretty(this->response_data, this->response_data_len));
out.append("\n");
}
#endif
#ifdef USE_API_HOMEASSISTANT_STATES

View File

@@ -611,7 +611,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
break;
}
#endif
#ifdef USE_API_HOMEASSISTANT_SERVICES
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
case HomeassistantActionResponse::MESSAGE_TYPE: {
HomeassistantActionResponse msg;
msg.decode(msg_data, msg_size);

View File

@@ -66,7 +66,7 @@ class APIServerConnectionBase : public ProtoService {
virtual void on_subscribe_homeassistant_services_request(const SubscribeHomeassistantServicesRequest &value){};
#endif
#ifdef USE_API_HOMEASSISTANT_SERVICES
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
virtual void on_homeassistant_action_response(const HomeassistantActionResponse &value){};
#endif
#ifdef USE_API_HOMEASSISTANT_STATES

View File

@@ -404,17 +404,17 @@ 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);
}
void APIServer::handle_action_response(uint32_t call_id, bool success, const std::string &error_message,
const std::string &response_data) {
const char *response_data, size_t response_data_len) {
auto it = this->action_response_callbacks_.find(call_id);
if (it != this->action_response_callbacks_.end()) {
// Create the response object
auto response = std::make_shared<class ActionResponse>(success, error_message);
response->set_data(response_data);
auto response = std::make_shared<class ActionResponse>(success, error_message, response_data, response_data_len);
// Call the callback
it->second(response);
@@ -424,6 +424,7 @@ void APIServer::handle_action_response(uint32_t call_id, bool success, const std
}
}
#endif
#endif
#ifdef USE_API_HOMEASSISTANT_STATES
void APIServer::subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,

View File

@@ -111,12 +111,13 @@ class APIServer : public Component, public Controller {
#endif
#ifdef USE_API_HOMEASSISTANT_SERVICES
void send_homeassistant_action(const HomeassistantActionRequest &call);
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
// Action response handling
using ActionResponseCallback = std::function<void(std::shared_ptr<class ActionResponse>)>;
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,
const std::string &response_data);
const char *response_data, size_t response_data_len);
#endif
#endif
#ifdef USE_API_SERVICES
void register_user_service(UserServiceDescriptor *descriptor) { this->user_services_.push_back(descriptor); }
@@ -193,7 +194,7 @@ class APIServer : public Component, public Controller {
#ifdef USE_API_SERVICES
std::vector<UserServiceDescriptor *> user_services_;
#endif
#ifdef USE_API_HOMEASSISTANT_SERVICES
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
std::map<uint32_t, ActionResponseCallback> action_response_callbacks_;
#endif

View File

@@ -47,35 +47,36 @@ template<typename... Ts> class TemplatableKeyValuePair {
TemplatableStringValue<Ts...> value;
};
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
// Represents the response data from a Home Assistant action
class ActionResponse {
public:
ActionResponse(bool success, std::string error_message = "")
: success_(success), error_message_(std::move(error_message)) {}
ActionResponse(bool success, std::string error_message, const char *data, size_t data_len)
: success_(success), error_message_(std::move(error_message)), data_(data), data_len_(data_len) {}
bool is_success() const { return this->success_; }
const std::string &get_error_message() const { return this->error_message_; }
const std::string &get_data() const { return this->data_; }
const char *get_data() const { return this->data_; }
size_t get_data_len() const { return this->data_len_; }
// Get data as parsed JSON object
// Returns unbound JsonObject if data is empty or invalid JSON
JsonObject get_json() {
if (this->data_.empty())
if (this->data_len_ == 0)
return JsonObject(); // Return unbound JsonObject if no data
if (!this->parsed_json_) {
this->json_document_ = json::parse_json(this->data_);
this->json_document_ = json::parse_json(this->data_, this->data_len_);
this->json_ = this->json_document_.as<JsonObject>();
this->parsed_json_ = true;
}
return this->json_;
}
void set_data(const std::string &data) { this->data_ = data; }
protected:
bool success_;
std::string error_message_;
std::string data_;
const char *data_;
size_t data_len_;
JsonDocument json_document_;
JsonObject json_;
bool parsed_json_{false};
@@ -83,6 +84,7 @@ class ActionResponse {
// Callback type for action responses
template<typename... Ts> using ActionResponseCallback = std::function<void(std::shared_ptr<ActionResponse>, Ts...)>;
#endif
template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts...> {
public:
@@ -101,6 +103,7 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
this->variables_.emplace_back(std::move(key), value);
}
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
template<typename T> void set_response_template(T response_template) {
this->response_template_ = response_template;
this->has_response_template_ = true;
@@ -110,6 +113,7 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
this->wants_response_ = true;
this->response_callback_ = callback;
}
#endif
void play(Ts... x) override {
HomeassistantActionRequest resp;
@@ -135,6 +139,7 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
kv.value = it.value.value(x...);
}
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
if (this->wants_response_) {
// Generate a unique call ID for this service call
static uint32_t call_id_counter = 1;
@@ -152,6 +157,7 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
std::apply([this, &response](auto &&...args) { this->response_callback_(response, args...); }, captured_args);
});
}
#endif
this->parent_->send_homeassistant_action(resp);
}
@@ -163,12 +169,15 @@ template<typename... Ts> class HomeAssistantServiceCallAction : public Action<Ts
std::vector<TemplatableKeyValuePair<Ts...>> data_;
std::vector<TemplatableKeyValuePair<Ts...>> data_template_;
std::vector<TemplatableKeyValuePair<Ts...>> variables_;
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
TemplatableStringValue<Ts...> response_template_{""};
ActionResponseCallback<Ts...> response_callback_;
bool wants_response_{false};
bool has_response_template_{false};
#endif
};
#ifdef USE_API_HOMEASSISTANT_ACTION_RESPONSES
template<typename... Ts>
class HomeAssistantActionResponseTrigger : public Trigger<std::shared_ptr<ActionResponse>, Ts...> {
public:
@@ -177,6 +186,7 @@ class HomeAssistantActionResponseTrigger : public Trigger<std::shared_ptr<Action
[this](std::shared_ptr<ActionResponse> response, Ts... x) { this->trigger(response, x...); });
}
};
#endif
} // namespace esphome::api
#endif

View File

@@ -26,7 +26,7 @@ bool parse_json(const std::string &data, const json_parse_t &f) {
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
}
JsonDocument parse_json(const std::string &data) {
JsonDocument parse_json(const char *data, size_t len) {
// NOLINTBEGIN(clang-analyzer-cplusplus.NewDeleteLeaks) false positive with ArduinoJson
#ifdef USE_PSRAM
auto doc_allocator = SpiRamAllocator();
@@ -38,12 +38,12 @@ JsonDocument parse_json(const std::string &data) {
ESP_LOGE(TAG, "Could not allocate memory for JSON document!");
return JsonObject(); // return unbound object
}
DeserializationError err = deserializeJson(json_document, data);
DeserializationError err = deserializeJson(json_document, data, len);
if (err == DeserializationError::Ok) {
return json_document;
} else if (err == DeserializationError::NoMemory) {
ESP_LOGE(TAG, "Can not allocate more memory for deserialization. Consider making source string smaller");
ESP_LOGE(TAG, "Can not allocate more memory for deserialization. Consider making source buffer smaller");
return JsonObject(); // return unbound object
}
ESP_LOGE(TAG, "Parse error: %s", err.c_str());
@@ -51,6 +51,8 @@ JsonDocument parse_json(const std::string &data) {
// NOLINTEND(clang-analyzer-cplusplus.NewDeleteLeaks)
}
JsonDocument parse_json(const std::string &data) { return parse_json(data.c_str(), data.size()); }
std::string JsonBuilder::serialize() {
if (doc_.overflowed()) {
ESP_LOGE(TAG, "JSON document overflow");

View File

@@ -51,6 +51,8 @@ std::string build_json(const json_build_t &f);
bool parse_json(const std::string &data, const json_parse_t &f);
/// Parse a JSON string and return the root JsonDocument (or an unbound object on error)
JsonDocument parse_json(const std::string &data);
/// Parse JSON from a buffer and return the root JsonDocument (or an unbound object on error)
JsonDocument parse_json(const char *data, size_t len);
/// Builder class for creating JSON documents without lambdas
class JsonBuilder {

View File

@@ -111,6 +111,7 @@
#define USE_API_CLIENT_CONNECTED_TRIGGER
#define USE_API_CLIENT_DISCONNECTED_TRIGGER
#define USE_API_HOMEASSISTANT_SERVICES
#define USE_API_HOMEASSISTANT_ACTION_RESPONSES
#define USE_API_HOMEASSISTANT_STATES
#define USE_API_NOISE
#define USE_API_PLAINTEXT