mirror of
https://github.com/Threnklyn/esphome-dev.git
synced 2026-05-30 01:38:27 +02:00
Allow entity names to be set to None (#4607)
* Allow entity names to be set to None so they take on the device friendly_name automatically * Use empty
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -10,7 +11,13 @@ EntityBase::EntityBase(std::string name) : name_(std::move(name)) { this->calc_o
|
||||
// Entity Name
|
||||
const std::string &EntityBase::get_name() const { return this->name_; }
|
||||
void EntityBase::set_name(const std::string &name) {
|
||||
this->name_ = name;
|
||||
if (name.empty()) {
|
||||
this->name_ = App.get_friendly_name();
|
||||
this->has_own_name_ = false;
|
||||
} else {
|
||||
this->name_ = name;
|
||||
this->has_own_name_ = true;
|
||||
}
|
||||
this->calc_object_id_();
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,9 @@ class EntityBase {
|
||||
const std::string &get_name() const;
|
||||
void set_name(const std::string &name);
|
||||
|
||||
// Get whether this Entity has its own name or it should use the device friendly_name.
|
||||
bool has_own_name() const { return this->has_own_name_; }
|
||||
|
||||
// Get the sanitized name of this Entity as an ID. Caching it internally.
|
||||
const std::string &get_object_id();
|
||||
|
||||
@@ -52,6 +55,7 @@ class EntityBase {
|
||||
void calc_object_id_();
|
||||
|
||||
std::string name_;
|
||||
bool has_own_name_{false};
|
||||
std::string object_id_;
|
||||
const char *icon_c_str_{nullptr};
|
||||
uint32_t object_id_hash_;
|
||||
|
||||
Reference in New Issue
Block a user