Add option to suffix name with mac address (#1615)

* Add option to suffix name with mac address

* Rename and add to test file
This commit is contained in:
Jesse Hills 2021-03-18 09:22:48 +13:00 committed by GitHub
parent 7708b81ef5
commit faf577a9dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 15 additions and 3 deletions

View File

@ -37,8 +37,12 @@ namespace esphome {
class Application { class Application {
public: public:
void pre_setup(const std::string &name, const char *compilation_time) { void pre_setup(const std::string &name, const char *compilation_time, bool name_add_mac_suffix) {
this->name_ = name; if (name_add_mac_suffix) {
this->name_ = name + "_" + get_mac_address().substr(6);
} else {
this->name_ = name;
}
this->compilation_time_ = compilation_time; this->compilation_time_ = compilation_time;
global_preferences.begin(); global_preferences.begin();
} }

View File

@ -45,6 +45,8 @@ LoopTrigger = cg.esphome_ns.class_(
VERSION_REGEX = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+(?:[ab]\d+)?$") 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): def validate_board(value):
if CORE.is_esp8266: 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_INCLUDES, default=[]): cv.ensure_list(valid_include),
cv.Optional(CONF_LIBRARIES, default=[]): cv.ensure_list(cv.string_strict), 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( cv.Optional("esphome_core_version"): cv.invalid(
"The esphome_core_version option has been " "The esphome_core_version option has been "
"removed in 1.13 - the esphome core source " "removed in 1.13 - the esphome core source "
@ -289,7 +292,11 @@ def _add_automations(config):
def to_code(config): def to_code(config):
cg.add_global(cg.global_ns.namespace("esphome").using) cg.add_global(cg.global_ns.namespace("esphome").using)
cg.add( 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) CORE.add_job(_add_automations, config)

View File

@ -6,6 +6,7 @@ substitutions:
esphome: esphome:
name: test1 name: test1
name_add_mac_suffix: true
platform: ESP32 platform: ESP32
board: nodemcu-32s board: nodemcu-32s
on_boot: on_boot: