Use ${name} for device_name sensor

Solves #1907
This commit is contained in:
Edward Firmo
2024-03-15 22:33:06 +01:00
parent 55ed3775d1
commit 2e4b7f2a87

View File

@@ -52,19 +52,7 @@ esphome:
then: then:
- lambda: |- - lambda: |-
if (isnan(blueprint_status->raw_state)) blueprint_status->publish_state(0); if (isnan(blueprint_status->raw_state)) blueprint_status->publish_state(0);
std::string s = "${device_name}"; device_name->publish_state("${name}");
std::string result;
bool last_was_underscore = false;
for (char& c : s) {
if (isalnum(c)) {
result += tolower(c); // Add alphanumeric characters as lowercase
last_was_underscore = false;
} else if (!last_was_underscore) { // Replace non-alphanumeric with '_' but avoid consecutive '_'
result += '_';
last_was_underscore = true;
}
}
device_name->publish_state(result.c_str());
notification_label->publish_state(""); notification_label->publish_state("");
notification_text->publish_state(""); notification_text->publish_state("");
notification_unread->turn_off(); notification_unread->turn_off();
@@ -847,7 +835,7 @@ display:
- id: disp1 - id: disp1
platform: nextion platform: nextion
uart_id: tf_uart uart_id: tf_uart
start_up_page: 8 # start_up_page: 8 # Boot page
on_setup: on_setup:
lambda: |- lambda: |-
nextion_init->publish_state(true); nextion_init->publish_state(true);
@@ -1619,6 +1607,21 @@ text_sensor:
entity_category: diagnostic entity_category: diagnostic
internal: false internal: false
disabled_by_default: false disabled_by_default: false
lambda: return {"${name}"};
filters:
- lambda: |-
std::string result;
bool last_was_underscore = false;
for (char& c : x) {
if (isalnum(c)) {
result += tolower(c); // Add alphanumeric characters as lowercase
last_was_underscore = false;
} else if (!last_was_underscore) { // Replace non-alphanumeric with '_' but avoid consecutive '_'
result += '_';
last_was_underscore = true;
}
}
return result;
##### Entity Id of the entity displayed on the detailed pages ##### Entity Id of the entity displayed on the detailed pages
- id: detailed_entity - id: detailed_entity
@@ -1945,21 +1948,19 @@ script:
id: current_page id: current_page
state: boot state: boot
then: then:
- lambda: ESP_LOGV("script.global_settings", "Boot page is visible");
- wait_until: - wait_until:
condition: condition:
- not: - not:
- text_sensor.state: # Is boot page visible? - text_sensor.state: # Is boot page visible?
id: current_page id: current_page
state: 'boot' state: boot
timeout: 2s timeout: 2s
- if: - if:
condition: condition:
- text_sensor.state: # Avoid this being called twice by multiple boot triggers - text_sensor.state: # Avoid this being called twice by multiple boot triggers
id: current_page id: current_page
state: 'boot' state: boot
then: then:
- lambda: ESP_LOGV("script.global_settings", "Boot page still visible");
- if: - if:
condition: condition:
switch.is_on: notification_sound switch.is_on: notification_sound
@@ -1971,8 +1972,6 @@ script:
goto_page->execute(wakeup_page_name->state.c_str()); goto_page->execute(wakeup_page_name->state.c_str());
timer_reset_all->execute(wakeup_page_name->state.c_str()); timer_reset_all->execute(wakeup_page_name->state.c_str());
- lambda: ESP_LOGV("script.global_settings", "Finished");
- id: goto_page - id: goto_page
mode: restart mode: restart
parameters: parameters:
@@ -2049,7 +2048,7 @@ script:
mode: restart mode: restart
then: then:
- lambda: |- - lambda: |-
ESP_LOGV("script.page_blank", "Construct blank page"); ESP_LOGW("script.page_blank", "Construct blank page");
disp1->set_component_text_printf("esp_version", "ESP: ${version}"); // ESPHome version disp1->set_component_text_printf("esp_version", "ESP: ${version}"); // ESPHome version
#ifdef ARDUINO #ifdef ARDUINO
disp1->set_component_text_printf("framework", "Arduino"); disp1->set_component_text_printf("framework", "Arduino");
@@ -2066,7 +2065,6 @@ script:
set_brightness->execute(100); set_brightness->execute(100);
disp1->set_component_text_printf("boot.esph_version", "${version}"); // ESPHome version disp1->set_component_text_printf("boot.esph_version", "${version}"); // ESPHome version
if (current_page->state == "boot") { if (current_page->state == "boot") {
ESP_LOGW("DEBUG", "Page boot is visible");
#ifdef ARDUINO #ifdef ARDUINO
disp1->set_component_text("framework", "Arduino"); disp1->set_component_text("framework", "Arduino");
#elif defined(USE_ESP_IDF) #elif defined(USE_ESP_IDF)
@@ -2148,7 +2146,9 @@ script:
state: boot state: boot
timeout: 10s timeout: 10s
- lambda: |- - lambda: |-
if (current_page->state == "boot") goto_page->execute(wakeup_page_name->state.c_str()); if (current_page->state == "boot") {
goto_page->execute(wakeup_page_name->state.c_str());
}
- id: page_buttonpage - id: page_buttonpage
mode: restart mode: restart
@@ -2189,7 +2189,9 @@ script:
// Report new page to logs // Report new page to logs
ESP_LOGD("script.page_changed", "New page: %s", page.c_str()); ESP_LOGD("script.page_changed", "New page: %s", page.c_str());
// Go to boot page if not initiated // Go to boot page if not initiated
if (blueprint_status->state <= 99) goto_page->execute("boot"); if (not nextion_init->state) {
goto_page->execute("boot");
}
// Reset globals // Reset globals
if (page != "alarm" && if (page != "alarm" &&
page != "climate" && page != "climate" &&
@@ -2775,7 +2777,6 @@ script:
page: string page: string
then: then:
- lambda: |- - lambda: |-
ESP_LOGV("script.timer_reset_all", "Reset timers");
timer_page->execute(page.c_str(), int(timeout_page->state)); timer_page->execute(page.c_str(), int(timeout_page->state));
timer_dim->execute(page.c_str(), int(timeout_dim->state)); timer_dim->execute(page.c_str(), int(timeout_dim->state));
timer_sleep->execute(page.c_str(), int(timeout_sleep->state)); timer_sleep->execute(page.c_str(), int(timeout_sleep->state));
@@ -2785,8 +2786,6 @@ script:
page: string page: string
timeout: uint timeout: uint
then: then:
- lambda: |-
ESP_LOGV("script.timer_page", "Reset timer: %is", timeout);
- if: - if:
condition: condition:
- lambda: |- - lambda: |-
@@ -2817,7 +2816,6 @@ script:
timeout: uint timeout: uint
then: then:
- lambda: |- - lambda: |-
ESP_LOGV("script.timer_dim", "Reset timer: %is", timeout);
if (current_brightness->state <= display_dim_brightness->state if (current_brightness->state <= display_dim_brightness->state
and page != "screensaver" and page != "screensaver"
and page != "boot" and page != "boot"
@@ -2843,8 +2841,6 @@ script:
page: string page: string
timeout: uint timeout: uint
then: then:
- lambda: |-
ESP_LOGV("script.timer_sleep", "Reset timer: %is", timeout);
- if: - if:
condition: condition:
- lambda: return (timeout >= 1 and current_page->state != "screensaver" and current_page->state != "boot"); - lambda: return (timeout >= 1 and current_page->state != "screensaver" and current_page->state != "boot");