mirror of
https://github.com/esphome/esphome.git
synced 2026-02-18 15:35:59 -07:00
cleanup
This commit is contained in:
@@ -431,72 +431,61 @@ void APIServer::handle_action_response(uint32_t call_id, bool success, const std
|
||||
#endif // USE_API_HOMEASSISTANT_SERVICES
|
||||
|
||||
#ifdef USE_API_HOMEASSISTANT_STATES
|
||||
// New const char* overload (for internal components - zero allocation)
|
||||
void APIServer::subscribe_home_assistant_state(const char *entity_id, const char *attribute,
|
||||
std::function<void(std::string)> f) {
|
||||
// Helper to add subscription (reduces duplication)
|
||||
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_ = false,
|
||||
.once_ = once,
|
||||
.has_attribute_ = (attribute != nullptr),
|
||||
// entity_id_copy_ and attribute_copy_ remain nullptr (no heap allocation)
|
||||
});
|
||||
}
|
||||
|
||||
// Helper to add subscription with heap-allocated strings (reduces duplication)
|
||||
void APIServer::add_state_subscription_(std::string entity_id, optional<std::string> attribute,
|
||||
std::function<void(std::string)> f, bool once) {
|
||||
HomeAssistantStateSubscription sub;
|
||||
// Allocate heap storage for the strings
|
||||
sub.entity_id_copy_ = std::make_unique<std::string>(std::move(entity_id));
|
||||
sub.entity_id_ = sub.entity_id_copy_->c_str();
|
||||
|
||||
if (attribute.has_value()) {
|
||||
sub.attribute_copy_ = std::make_unique<std::string>(std::move(attribute.value()));
|
||||
sub.attribute_ = sub.attribute_copy_->c_str();
|
||||
sub.has_attribute_ = true;
|
||||
} else {
|
||||
sub.attribute_ = nullptr;
|
||||
sub.has_attribute_ = false;
|
||||
}
|
||||
|
||||
sub.callback_ = std::move(f);
|
||||
sub.once_ = once;
|
||||
this->state_subs_.push_back(std::move(sub));
|
||||
}
|
||||
|
||||
// New const char* overload (for internal components - zero allocation)
|
||||
void APIServer::subscribe_home_assistant_state(const char *entity_id, const char *attribute,
|
||||
std::function<void(std::string)> f) {
|
||||
this->add_state_subscription_(entity_id, attribute, std::move(f), false);
|
||||
}
|
||||
|
||||
void APIServer::get_home_assistant_state(const char *entity_id, const char *attribute,
|
||||
std::function<void(std::string)> f) {
|
||||
this->state_subs_.push_back(HomeAssistantStateSubscription{
|
||||
.entity_id_ = entity_id,
|
||||
.attribute_ = attribute,
|
||||
.callback_ = std::move(f),
|
||||
.once_ = true,
|
||||
.has_attribute_ = (attribute != nullptr),
|
||||
// entity_id_copy_ and attribute_copy_ remain nullptr (no heap allocation)
|
||||
});
|
||||
this->add_state_subscription_(entity_id, attribute, std::move(f), true);
|
||||
}
|
||||
|
||||
// Existing std::string overload (for custom_api_device.h - heap allocation)
|
||||
void APIServer::subscribe_home_assistant_state(std::string entity_id, optional<std::string> attribute,
|
||||
std::function<void(std::string)> f) {
|
||||
HomeAssistantStateSubscription sub;
|
||||
// Allocate heap storage for the strings
|
||||
sub.entity_id_copy_ = std::make_unique<std::string>(std::move(entity_id));
|
||||
sub.entity_id_ = sub.entity_id_copy_->c_str();
|
||||
|
||||
if (attribute.has_value()) {
|
||||
sub.attribute_copy_ = std::make_unique<std::string>(std::move(attribute.value()));
|
||||
sub.attribute_ = sub.attribute_copy_->c_str();
|
||||
sub.has_attribute_ = true;
|
||||
} else {
|
||||
sub.attribute_ = nullptr;
|
||||
sub.has_attribute_ = false;
|
||||
}
|
||||
|
||||
sub.callback_ = std::move(f);
|
||||
sub.once_ = false;
|
||||
this->state_subs_.push_back(std::move(sub));
|
||||
this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), false);
|
||||
}
|
||||
|
||||
void APIServer::get_home_assistant_state(std::string entity_id, optional<std::string> attribute,
|
||||
std::function<void(std::string)> f) {
|
||||
HomeAssistantStateSubscription sub;
|
||||
// Allocate heap storage for the strings
|
||||
sub.entity_id_copy_ = std::make_unique<std::string>(std::move(entity_id));
|
||||
sub.entity_id_ = sub.entity_id_copy_->c_str();
|
||||
|
||||
if (attribute.has_value()) {
|
||||
sub.attribute_copy_ = std::make_unique<std::string>(std::move(attribute.value()));
|
||||
sub.attribute_ = sub.attribute_copy_->c_str();
|
||||
sub.has_attribute_ = true;
|
||||
} else {
|
||||
sub.attribute_ = nullptr;
|
||||
sub.has_attribute_ = false;
|
||||
}
|
||||
|
||||
sub.callback_ = std::move(f);
|
||||
sub.once_ = true;
|
||||
this->state_subs_.push_back(std::move(sub));
|
||||
this->add_state_subscription_(std::move(entity_id), std::move(attribute), std::move(f), true);
|
||||
}
|
||||
|
||||
const std::vector<APIServer::HomeAssistantStateSubscription> &APIServer::get_state_subs() const {
|
||||
|
||||
@@ -197,6 +197,13 @@ class APIServer : public Component, public Controller {
|
||||
bool update_noise_psk_(const SavedNoisePsk &new_psk, const LogString *save_log_msg, const LogString *fail_log_msg,
|
||||
const psk_t &active_psk, bool make_active);
|
||||
#endif // USE_API_NOISE
|
||||
#ifdef USE_API_HOMEASSISTANT_STATES
|
||||
// Helper methods to reduce code duplication
|
||||
void add_state_subscription_(const char *entity_id, const char *attribute, std::function<void(std::string)> f,
|
||||
bool once);
|
||||
void add_state_subscription_(std::string entity_id, optional<std::string> attribute,
|
||||
std::function<void(std::string)> f, bool once);
|
||||
#endif // USE_API_HOMEASSISTANT_STATES
|
||||
// Pointers and pointer-like types first (4 bytes each)
|
||||
std::unique_ptr<socket::Socket> socket_ = nullptr;
|
||||
#ifdef USE_API_CLIENT_CONNECTED_TRIGGER
|
||||
|
||||
Reference in New Issue
Block a user