[core] Deprecate get_object_id() and migrate remaining usages to get_object_id_to()

This commit is contained in:
J. Nick Koston
2025-12-22 15:13:59 -10:00
parent b4c92dd8cb
commit 7944fe6993
3 changed files with 21 additions and 5 deletions

View File

@@ -1,4 +1,5 @@
#include "pid_climate.h"
#include "esphome/core/entity_base.h"
#include "esphome/core/log.h"
namespace esphome {
@@ -162,14 +163,16 @@ void PIDClimate::start_autotune(std::unique_ptr<PIDAutotuner> &&autotune) {
float min_value = this->supports_cool_() ? -1.0f : 0.0f;
float max_value = this->supports_heat_() ? 1.0f : 0.0f;
this->autotuner_->config(min_value, max_value);
this->autotuner_->set_autotuner_id(this->get_object_id());
char object_id_buf[OBJECT_ID_MAX_LEN];
StringRef object_id = this->get_object_id_to(object_id_buf);
this->autotuner_->set_autotuner_id(std::string(object_id.c_str()));
ESP_LOGI(TAG,
"%s: Autotune has started. This can take a long time depending on the "
"responsiveness of your system. Your system "
"output will be altered to deliberately oscillate above and below the setpoint multiple times. "
"Until your sensor provides a reading, the autotuner may display \'nan\'",
this->get_object_id().c_str());
object_id.c_str());
this->set_interval("autotune-progress", 10000, [this]() {
if (this->autotuner_ != nullptr && !this->autotuner_->is_finished())
@@ -177,8 +180,7 @@ void PIDClimate::start_autotune(std::unique_ptr<PIDAutotuner> &&autotune) {
});
if (mode != climate::CLIMATE_MODE_HEAT_COOL) {
ESP_LOGW(TAG, "%s: !!! For PID autotuner you need to set AUTO (also called heat/cool) mode!",
this->get_object_id().c_str());
ESP_LOGW(TAG, "%s: !!! For PID autotuner you need to set AUTO (also called heat/cool) mode!", object_id.c_str());
}
}

View File

@@ -112,7 +112,12 @@ void PrometheusHandler::handleRequest(AsyncWebServerRequest *req) {
std::string PrometheusHandler::relabel_id_(EntityBase *obj) {
auto item = relabel_map_id_.find(obj);
return item == relabel_map_id_.end() ? obj->get_object_id() : item->second;
if (item != relabel_map_id_.end()) {
return item->second;
}
char object_id_buf[OBJECT_ID_MAX_LEN];
StringRef object_id = obj->get_object_id_to(object_id_buf);
return std::string(object_id.c_str());
}
std::string PrometheusHandler::relabel_name_(EntityBase *obj) {

View File

@@ -33,6 +33,15 @@ class EntityBase {
bool has_own_name() const { return this->flags_.has_own_name; }
// Get the sanitized name of this Entity as an ID.
// Deprecated: object_id mangles names and all object_id methods are planned for removal.
// See https://github.com/esphome/backlog/issues/76
// Now is the time to stop using object_id entirely. If you still need it temporarily,
// use get_object_id_to() which will remain available longer but will also eventually be removed.
ESPDEPRECATED("object_id mangles names and all object_id methods are planned for removal "
"(see https://github.com/esphome/backlog/issues/76). "
"Now is the time to stop using object_id. If still needed, use get_object_id_to() "
"which will remain available longer. get_object_id() will be removed in 2026.7.0",
"2025.12.0")
std::string get_object_id() const;
void set_object_id(const char *object_id);