Skip to content

Commit

Permalink
Remove device_name sensor
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardtfn committed Dec 5, 2024
1 parent 668655a commit 1dcfa04
Show file tree
Hide file tree
Showing 28 changed files with 114 additions and 101 deletions.
4 changes: 2 additions & 2 deletions ReleaseNotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ between ESPHome and Nextion. Visit the updated [API documentation](docs/api.md)
| `component_text`, `icon`, etc. | [`component`](docs/api.md#component-action-component) | Set display components |
<!-- markdownlint-enable MD033 -->

2. **Sensor `blueprint_status` removed**
Boot info is now transferred via events.
2. **Sensors `blueprint_status` and `device_name` removed**
Device's and boot info is now transferred via events.

3. **Notification unread switch replaced**
The "**Notification unread**" switch is now a binary sensor.
Expand Down
74 changes: 74 additions & 0 deletions components/nspanel_ha_blueprint/core_hw_wifi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// core_hw_wifi.h

#pragma once

#ifdef NSPANEL_HA_BLUEPRINT_CORE_HW_WIFI

#ifndef CORE_HW_WIFI_H
#define CORE_HW_WIFI_H

#include <algorithm>
#include <cctype>
#include <string>
#include "esphome/core/log.h"

namespace nspanel_ha_blueprint {

// Method to slugify a raw name
std::string slugify(const std::string &raw_name) {
std::string result;
bool last_was_underscore = false;

for (const char& c : raw_name) {
if (isalnum(c)) {
result += tolower(c); // Add alphanumeric characters as lowercase
last_was_underscore = false;
} else if (!last_was_underscore) { // Replace non-alphanumeric with '_', avoiding consecutive '_'
result += '_';
last_was_underscore = true;
}
}

// Trim leading and trailing underscores
if (!result.empty() && result.front() == '_') result.erase(0, 1);
if (!result.empty() && result.back() == '_') result.pop_back();

return result;
}

/**
* Generate a device name based on a raw name, optionally appending a MAC address suffix.
*
* This function constructs a device name by taking the provided `raw_name` and appending
* the last three bytes of the Wi-Fi MAC address as a suffix if available. If the MAC
* address cannot be fetched or if Wi-Fi is not used, the function returns the raw name
* unchanged.
*
* @param raw_name The base name for the device, provided as input.
* @return A slugified string representing the generated device name.
*/
std::string generate_device_name(const std::string &raw_name) {
std::string full_name = raw_name; // Start with the raw name

#ifdef ESP_MAC_WIFI_STA
uint8_t mac[6];
if (esp_read_mac(mac, ESP_MAC_WIFI_STA) == ESP_OK) {
// Add MAC suffix if it can be read
char hex_suffix[7];
snprintf(hex_suffix, sizeof(hex_suffix), "%02X%02X%02X", mac[3], mac[4], mac[5]);
full_name += "-" + std::string(hex_suffix);
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARNING
} else {
esphome::ESP_LOGW("generate_device_name", "Failed to read MAC address! Suffix not added.");
#endif // ESPHOME_LOG_LEVEL
}
#endif // ESP_MAC_WIFI_STA

return slugify(full_name); // Slugify the name
}

} // namespace nspanel_ha_blueprint

#endif // CORE_HW_WIFI_H

#endif // NSPANEL_HA_BLUEPRINT_CORE_HW_WIFI
4 changes: 2 additions & 2 deletions esphome/nspanel_esphome_core_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ script:
- homeassistant.event:
event: esphome.nspanel_ha_blueprint
data:
device_name: !lambda return device_name->state.c_str();
device_name: !lambda return id(device_name).c_str();
esphome_version: ${version}
type: button_click
page: !lambda return page_names[page_id];
Expand All @@ -136,7 +136,7 @@ script:
- homeassistant.event:
event: esphome.nspanel_ha_blueprint
data:
device_name: !lambda return device_name->state.c_str();
device_name: !lambda return id(device_name).c_str();
esphome_version: ${version}
type: action_call
action: !lambda return action.c_str();
Expand Down
2 changes: 1 addition & 1 deletion esphome/nspanel_esphome_core_boot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ script:
std::map<std::string, std::string> event_data;
// Adding static data to the event
event_data["device_name"] = device_name->state.c_str();
event_data["device_name"] = id(device_name).c_str();
event_data["esphome_version"] = "${version}";
event_data["type"] = "boot";
event_data["step"] = step_name.c_str();
Expand Down
6 changes: 3 additions & 3 deletions esphome/nspanel_esphome_core_hw_display.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ script:
if (event == "short_click" or event == "long_click") {
// Button click event
ha_event.fire_homeassistant_event("esphome.nspanel_ha_blueprint", {
{"device_name", device_name->state.c_str()},
{"device_name", id(device_name).c_str()},
{"esphome_version", "${version}"},
{"type", "button_click"},
{"page", page.c_str()},
Expand All @@ -653,7 +653,7 @@ script:
} else {
// Generic event
ha_event.fire_homeassistant_event("esphome.nspanel_ha_blueprint", {
{"device_name", device_name->state.c_str()},
{"device_name", id(device_name).c_str()},
{"esphome_version", "${version}"},
{"type", "button_click"},
{"page", page.c_str()},
Expand Down Expand Up @@ -715,7 +715,7 @@ script:
// Inform Home Assistant about the page change
esphome::api::CustomAPIDevice ha_event;
ha_event.fire_homeassistant_event("esphome.nspanel_ha_blueprint", {
{"device_name", device_name->state.c_str()},
{"device_name", id(device_name).c_str()},
{"esphome_version", "${version}"},
{"type", "page_changed"},
{"page", page_names[new_page_id]},
Expand Down
62 changes: 13 additions & 49 deletions esphome/nspanel_esphome_core_hw_wifi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,24 @@ esphome:
build_flags:
- -D NSPANEL_HA_BLUEPRINT_CORE_HW_WIFI
on_boot:
- priority: 600.4 # This is where most sensors are set up.
- priority: 600.9 # This is where most sensors are set up.
then:
- text_sensor.template.publish:
id: device_name
state: ${name}
- lambda: |-
id(device_name) = generate_device_name("${name}");
on_shutdown:
- priority: 0.1
- priority: 0.9
then: # Update Wi-Fi icon
- lambda: |-
set_component_text->execute(${PAGE_ID_HOME}, "wifi_icon", "${MDI_ICON_RESTART}");
set_component_font_color->execute(${PAGE_ID_HOME}, "wifi_icon", 63488);
globals:
- id: device_name
type: std::string
restore_value: false
initial_value: '"${name}"'

script:
- id: !extend boot_package_register
then:
Expand All @@ -64,8 +69,10 @@ script:
if (rssi > -70) ESP_LOGI("${project_tag}", "Wi-Fi: %s (%.0f dBm)", rssi_status, rssi);
else if (rssi > -80) ESP_LOGW("${project_tag}", "Wi-Fi: %s (%.0f dBm)", rssi_status, rssi);
else ESP_LOGE("${project_tag}", "Wi-Fi: %s (%.0f dBm)", rssi_status, rssi);
} else
} else {
ESP_LOGE("${project_tag}", "Wi-Fi: DISCONNECTED");
}
ESP_LOGI("${project_tag}", "Device name: %s", id(device_name).c_str());
- id: !extend page_boot
then:
Expand Down Expand Up @@ -172,49 +179,6 @@ sensor:
icon: mdi:wifi
entity_category: diagnostic

text_sensor:
##### Device name - Used by bluepring to find action's names #####
- id: device_name
name: Device Name
platform: template
icon: mdi:identifier
entity_category: diagnostic
internal: false
disabled_by_default: false
lambda: |-
return {"${name}"};
filters:
- lambda: |-
#ifdef ESP_MAC_WIFI_STA
std::string suffix = "00ERROR"; // Default suffix in case of an error
uint8_t mac[6] = {0,0,0,0,0,0};
if (esp_read_mac(mac, ESP_MAC_WIFI_STA) == ESP_OK) {
suffix.clear(); // Clear the default error suffix
for (int i = 3; i < 6; ++i) { // Use last 3 bytes of MAC
char hex[3];
snprintf(hex, sizeof(hex), "%02X", mac[i]);
suffix += hex;
}
}
// Proceed with suffix (either MAC-based or default error indicator)
const std::string raw_name = (x + "-" + suffix);
#else
const std::string raw_name = x;
#endif
std::string result;
bool last_was_underscore = false;
for (const char& c : raw_name) {
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;
##### WIFI SETUP #####
wifi:
id: wifi_component
Expand Down
2 changes: 1 addition & 1 deletion esphome/nspanel_esphome_core_versioning.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ script:
- homeassistant.event:
event: esphome.nspanel_ha_blueprint
data:
device_name: !lambda return device_name->state.c_str();
device_name: !lambda return id(device_name).c_str();
esphome_version: ${version}
type: version
tft: !lambda return id(version_tft).c_str();
Expand Down
2 changes: 1 addition & 1 deletion esphome/nspanel_esphome_standard_page_light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ script:
if (rgb_color.size() == 3) {
esphome::api::CustomAPIDevice ha_event;
ha_event.fire_homeassistant_event("esphome.nspanel_ha_blueprint", {
{"device_name", device_name->state.c_str()},
{"device_name", id(device_name).c_str()},
{"esphome_version", "${version}"},
{"type", "action_call"},
{"action", "light.turn_on"},
Expand Down
4 changes: 2 additions & 2 deletions esphome/nspanel_esphome_standard_page_notification.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ text_sensor:
- homeassistant.event:
event: esphome.nspanel_ha_blueprint
data:
device_name: !lambda return device_name->state.c_str();
device_name: !lambda return id(device_name).c_str();
esphome_version: ${version}
type: notification_changed
component: notification_label
Expand All @@ -193,7 +193,7 @@ text_sensor:
- homeassistant.event:
event: esphome.nspanel_ha_blueprint
data:
device_name: !lambda return device_name->state.c_str();
device_name: !lambda return id(device_name).c_str();
esphome_version: ${version}
type: notification_changed
component: notification_text
Expand Down
2 changes: 1 addition & 1 deletion hmi/dev/nspanel_CJK_eu_code/boot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variable (string) version
Attributes
ID : 7
Scope : global
Text : 4.4.0.dev8
Text : 4.4.0.dev9
Max. Text Size: 16

Variable (int32) log_len
Expand Down
2 changes: 1 addition & 1 deletion hmi/dev/nspanel_CJK_us_code/boot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variable (string) version
Attributes
ID : 7
Scope : global
Text : 4.4.0.dev8
Text : 4.4.0.dev9
Max. Text Size: 16

Variable (int32) log_len
Expand Down
2 changes: 1 addition & 1 deletion hmi/dev/nspanel_CJK_us_land_code/boot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variable (string) version
Attributes
ID : 7
Scope : global
Text : 4.4.0.dev8
Text : 4.4.0.dev9
Max. Text Size: 16

Variable (int32) log_len
Expand Down
2 changes: 1 addition & 1 deletion hmi/dev/nspanel_eu_code/boot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variable (string) version
Attributes
ID : 7
Scope : global
Text : 4.4.0.dev8
Text : 4.4.0.dev9
Max. Text Size: 16

Variable (int32) log_len
Expand Down
2 changes: 1 addition & 1 deletion hmi/dev/nspanel_us_code/boot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variable (string) version
Attributes
ID : 7
Scope : global
Text : 4.4.0.dev8
Text : 4.4.0.dev9
Max. Text Size: 16

Variable (int32) log_len
Expand Down
2 changes: 1 addition & 1 deletion hmi/dev/nspanel_us_land_code/boot.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Variable (string) version
Attributes
ID : 7
Scope : global
Text : 4.4.0.dev8
Text : 4.4.0.dev9
Max. Text Size: 16

Variable (int32) log_len
Expand Down
Binary file modified hmi/nspanel_CJK_eu.HMI
Binary file not shown.
Binary file modified hmi/nspanel_CJK_eu.tft
Binary file not shown.
Binary file modified hmi/nspanel_CJK_us.HMI
Binary file not shown.
Binary file modified hmi/nspanel_CJK_us.tft
Binary file not shown.
Binary file modified hmi/nspanel_CJK_us_land.HMI
Binary file not shown.
Binary file modified hmi/nspanel_CJK_us_land.tft
Binary file not shown.
Binary file modified hmi/nspanel_eu.HMI
Binary file not shown.
Binary file modified hmi/nspanel_eu.tft
Binary file not shown.
Binary file modified hmi/nspanel_us.HMI
Binary file not shown.
Binary file modified hmi/nspanel_us.tft
Binary file not shown.
Binary file modified hmi/nspanel_us_land.HMI
Binary file not shown.
Binary file modified hmi/nspanel_us_land.tft
Binary file not shown.
Loading

0 comments on commit 1dcfa04

Please sign in to comment.