Make add mac global

Complements #2024
This commit is contained in:
Edward Firmo
2024-04-07 19:08:24 +02:00
parent 5fbae3128f
commit 2f697923cc

View File

@@ -1713,24 +1713,29 @@ text_sensor:
internal: false
disabled_by_default: false
lambda: |-
return {"${name}"};
filters:
- lambda: |-
#ifdef NSPANEL_HA_BLUEPRINT_PREBUILT
std::string suffix;
std::string suffix = "00ERROR"; // Default suffix in case of an error
uint8_t mac[6] = {0,0,0,0,0,0};
esp_read_mac(mac, ESP_MAC_WIFI_STA);
for (int i = 3; i < 6; ++i) {
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;
}
return {"${name}" + "-" + suffix};
}
// Proceed with suffix (either MAC-based or default error indicator)
const std::string raw_name = (x + "-" + suffix);
#else
return {"${name}"};
const std::string raw_name = x;
#endif
filters:
- lambda: |-
std::string result;
bool last_was_underscore = false;
for (char& c : x) {
for (char& c : raw_name) {
if (isalnum(c)) {
result += tolower(c); // Add alphanumeric characters as lowercase
last_was_underscore = false;