mirror of
https://github.com/esphome/esphome.git
synced 2025-11-20 16:55:49 +00:00
cleanup
This commit is contained in:
@@ -1536,12 +1536,12 @@ bool APIConnection::send_device_info_response(const DeviceInfoRequest &msg) {
|
||||
void APIConnection::on_home_assistant_state_response(const HomeAssistantStateResponse &msg) {
|
||||
for (auto &it : this->parent_->get_state_subs()) {
|
||||
// Compare entity_id and attribute with message fields
|
||||
bool entity_match = (strcmp(it.entity_id_, msg.entity_id.c_str()) == 0);
|
||||
bool attribute_match = (it.attribute_ != nullptr && strcmp(it.attribute_, msg.attribute.c_str()) == 0) ||
|
||||
(it.attribute_ == nullptr && msg.attribute.empty());
|
||||
bool entity_match = (strcmp(it.entity_id, msg.entity_id.c_str()) == 0);
|
||||
bool attribute_match = (it.attribute != nullptr && strcmp(it.attribute, msg.attribute.c_str()) == 0) ||
|
||||
(it.attribute == nullptr && msg.attribute.empty());
|
||||
|
||||
if (entity_match && attribute_match) {
|
||||
it.callback_(msg.state);
|
||||
it.callback(msg.state);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1878,12 +1878,12 @@ void APIConnection::process_state_subscriptions_() {
|
||||
|
||||
const auto &it = subs[this->state_subs_at_];
|
||||
SubscribeHomeAssistantStateResponse resp;
|
||||
resp.set_entity_id(StringRef(it.entity_id_));
|
||||
resp.set_entity_id(StringRef(it.entity_id));
|
||||
|
||||
// Avoid string copy by using the const char* pointer if it exists
|
||||
resp.set_attribute(it.attribute_ != nullptr ? StringRef(it.attribute_) : StringRef(""));
|
||||
resp.set_attribute(it.attribute != nullptr ? StringRef(it.attribute) : StringRef(""));
|
||||
|
||||
resp.once = it.once_;
|
||||
resp.once = it.once;
|
||||
if (this->send_message(resp, SubscribeHomeAssistantStateResponse::MESSAGE_TYPE)) {
|
||||
this->state_subs_at_++;
|
||||
}
|
||||
|
||||
@@ -435,8 +435,8 @@ void APIServer::handle_action_response(uint32_t call_id, bool success, const std
|
||||
void APIServer::add_state_subscription_(const char *entity_id, const char *attribute,
|
||||
std::function<void(std::string)> f, bool once) {
|
||||
this->state_subs_.push_back(HomeAssistantStateSubscription{
|
||||
.entity_id_ = entity_id, .attribute_ = attribute, .callback_ = std::move(f), .once_ = once,
|
||||
// entity_id_dynamic_storage_ and attribute_dynamic_storage_ remain nullptr (no heap allocation)
|
||||
.entity_id = entity_id, .attribute = attribute, .callback = std::move(f), .once = once,
|
||||
// entity_id_dynamic_storage and attribute_dynamic_storage remain nullptr (no heap allocation)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -445,18 +445,18 @@ void APIServer::add_state_subscription_(std::string entity_id, optional<std::str
|
||||
std::function<void(std::string)> f, bool once) {
|
||||
HomeAssistantStateSubscription sub;
|
||||
// Allocate heap storage for the strings
|
||||
sub.entity_id_dynamic_storage_ = std::make_unique<std::string>(std::move(entity_id));
|
||||
sub.entity_id_ = sub.entity_id_dynamic_storage_->c_str();
|
||||
sub.entity_id_dynamic_storage = std::make_unique<std::string>(std::move(entity_id));
|
||||
sub.entity_id = sub.entity_id_dynamic_storage->c_str();
|
||||
|
||||
if (attribute.has_value()) {
|
||||
sub.attribute_dynamic_storage_ = std::make_unique<std::string>(std::move(attribute.value()));
|
||||
sub.attribute_ = sub.attribute_dynamic_storage_->c_str();
|
||||
sub.attribute_dynamic_storage = std::make_unique<std::string>(std::move(attribute.value()));
|
||||
sub.attribute = sub.attribute_dynamic_storage->c_str();
|
||||
} else {
|
||||
sub.attribute_ = nullptr;
|
||||
sub.attribute = nullptr;
|
||||
}
|
||||
|
||||
sub.callback_ = std::move(f);
|
||||
sub.once_ = once;
|
||||
sub.callback = std::move(f);
|
||||
sub.once = once;
|
||||
this->state_subs_.push_back(std::move(sub));
|
||||
}
|
||||
|
||||
|
||||
@@ -154,15 +154,15 @@ class APIServer : public Component, public Controller {
|
||||
|
||||
#ifdef USE_API_HOMEASSISTANT_STATES
|
||||
struct HomeAssistantStateSubscription {
|
||||
const char *entity_id_; // Pointer to flash (internal) or heap (external)
|
||||
const char *attribute_; // Pointer to flash or nullptr (nullptr means no attribute)
|
||||
std::function<void(std::string)> callback_;
|
||||
bool once_;
|
||||
const char *entity_id; // Pointer to flash (internal) or heap (external)
|
||||
const char *attribute; // Pointer to flash or nullptr (nullptr means no attribute)
|
||||
std::function<void(std::string)> callback;
|
||||
bool once;
|
||||
|
||||
// Dynamic storage for external components using std::string API (custom_api_device.h)
|
||||
// These are only allocated when using the std::string overload (nullptr for const char* overload)
|
||||
std::unique_ptr<std::string> entity_id_dynamic_storage_;
|
||||
std::unique_ptr<std::string> attribute_dynamic_storage_;
|
||||
std::unique_ptr<std::string> entity_id_dynamic_storage;
|
||||
std::unique_ptr<std::string> attribute_dynamic_storage;
|
||||
};
|
||||
|
||||
// New const char* overload (for internal components - zero allocation)
|
||||
|
||||
Reference in New Issue
Block a user