diff --git a/esphome/core/application.h b/esphome/core/application.h index 3c293e6c8f..e9c8638f60 100644 --- a/esphome/core/application.h +++ b/esphome/core/application.h @@ -37,8 +37,12 @@ namespace esphome { class Application { public: - void pre_setup(const std::string &name, const char *compilation_time) { - this->name_ = name; + void pre_setup(const std::string &name, const char *compilation_time, bool name_add_mac_suffix) { + if (name_add_mac_suffix) { + this->name_ = name + "_" + get_mac_address().substr(6); + } else { + this->name_ = name; + } this->compilation_time_ = compilation_time; global_preferences.begin(); } diff --git a/esphome/core_config.py b/esphome/core_config.py index c0498a8fa9..55d219d86b 100644 --- a/esphome/core_config.py +++ b/esphome/core_config.py @@ -45,6 +45,8 @@ LoopTrigger = cg.esphome_ns.class_( VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+(?:[ab]\d+)?$") +CONF_NAME_ADD_MAC_SUFFIX = "name_add_mac_suffix" + def validate_board(value): if CORE.is_esp8266: @@ -173,6 +175,7 @@ CONFIG_SCHEMA = cv.Schema( ), cv.Optional(CONF_INCLUDES, default=[]): cv.ensure_list(valid_include), cv.Optional(CONF_LIBRARIES, default=[]): cv.ensure_list(cv.string_strict), + cv.Optional(CONF_NAME_ADD_MAC_SUFFIX, default=False): cv.boolean, cv.Optional("esphome_core_version"): cv.invalid( "The esphome_core_version option has been " "removed in 1.13 - the esphome core source " @@ -289,7 +292,11 @@ def _add_automations(config): def to_code(config): cg.add_global(cg.global_ns.namespace("esphome").using) cg.add( - cg.App.pre_setup(config[CONF_NAME], cg.RawExpression('__DATE__ ", " __TIME__')) + cg.App.pre_setup( + config[CONF_NAME], + cg.RawExpression('__DATE__ ", " __TIME__'), + config[CONF_NAME_ADD_MAC_SUFFIX], + ) ) CORE.add_job(_add_automations, config) diff --git a/tests/test1.yaml b/tests/test1.yaml index 316f31e68a..b50d5eeef7 100644 --- a/tests/test1.yaml +++ b/tests/test1.yaml @@ -6,6 +6,7 @@ substitutions: esphome: name: test1 + name_add_mac_suffix: true platform: ESP32 board: nodemcu-32s on_boot: