mirror of
https://github.com/esphome/esphome.git
synced 2025-09-08 06:12:20 +01:00
[api] Avoid object_id string allocations for all entity info messages (#10260)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/string_ref.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
@@ -50,13 +51,18 @@ std::string EntityBase::get_object_id() const {
|
||||
if (!this->flags_.has_own_name && App.is_name_add_mac_suffix_enabled()) {
|
||||
// `App.get_friendly_name()` is dynamic.
|
||||
return str_sanitize(str_snake_case(App.get_friendly_name()));
|
||||
} else {
|
||||
// `App.get_friendly_name()` is constant.
|
||||
if (this->object_id_c_str_ == nullptr) {
|
||||
return "";
|
||||
}
|
||||
return this->object_id_c_str_;
|
||||
}
|
||||
// `App.get_friendly_name()` is constant.
|
||||
return this->object_id_c_str_ == nullptr ? "" : this->object_id_c_str_;
|
||||
}
|
||||
StringRef EntityBase::get_object_id_ref_for_api_() const {
|
||||
static constexpr auto EMPTY_STRING = StringRef::from_lit("");
|
||||
// Return empty for dynamic case (MAC suffix)
|
||||
if (!this->flags_.has_own_name && App.is_name_add_mac_suffix_enabled()) {
|
||||
return EMPTY_STRING;
|
||||
}
|
||||
// For static case, return the string or empty if null
|
||||
return this->object_id_c_str_ == nullptr ? EMPTY_STRING : StringRef(this->object_id_c_str_);
|
||||
}
|
||||
void EntityBase::set_object_id(const char *object_id) {
|
||||
this->object_id_c_str_ = object_id;
|
||||
|
Reference in New Issue
Block a user