From cee2d5ca314866284965571285b2f06fe0c08cb7 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Thu, 4 Jul 2024 13:42:23 +0200 Subject: [PATCH] change temperaturesensor id to underscore notation --- interface/src/project/validators.ts | 8 +++++++- src/temperaturesensor.cpp | 8 ++------ src/test/test.cpp | 4 ++-- src/web/WebCustomizationService.cpp | 4 ++++ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/interface/src/project/validators.ts b/interface/src/project/validators.ts index 168685da6..348a46970 100644 --- a/interface/src/project/validators.ts +++ b/interface/src/project/validators.ts @@ -389,7 +389,13 @@ export const entityItemValidation = (entity: EntityItem[], entityItem: EntityIte export const temperatureSensorItemValidation = () => new Schema({ - n: [{ required: true, message: 'Name is required' }] + n: [ + { + type: 'string', + pattern: /^[a-zA-Z0-9_\\.]{0,15}$/, + message: "Must be <15 characters: alpha numeric, '_' or '.'" + } + ] }); export const isGPIOUniqueValidator = (sensors: AnalogSensor[]) => ({ diff --git a/src/temperaturesensor.cpp b/src/temperaturesensor.cpp index 3b0773e34..7766b465c 100644 --- a/src/temperaturesensor.cpp +++ b/src/temperaturesensor.cpp @@ -556,11 +556,7 @@ void TemperatureSensor::publish_values(const bool force) { Mqtt::add_ha_sections_to_doc("temperature", stat_t, config, !is_ha_device_created, val_cond); char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; - // use '_' as HA doesn't like '-' in the topic name - std::string sensorid = sensor.id(); - std::replace(sensorid.begin(), sensorid.end(), '-', '_'); - - snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensorid.c_str()); + snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id().c_str()); sensor.ha_registered = Mqtt::queue_ha(topic, config.as()); } @@ -580,7 +576,7 @@ TemperatureSensor::Sensor::Sensor(const uint8_t addr[]) char id_s[20]; snprintf(id_s, sizeof(id_s), - "%02X-%04X-%04X-%04X", + "%02X_%04X_%04X_%04X", (unsigned int)(internal_id_ >> 48) & 0xFF, (unsigned int)(internal_id_ >> 32) & 0xFFFF, (unsigned int)(internal_id_ >> 16) & 0xFFFF, diff --git a/src/test/test.cpp b/src/test/test.cpp index 9ff67b305..3fec354fd 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -793,7 +793,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const EMSESP::webAPIService.webAPIService(&request); request.url("/api/temperaturesensor/info"); EMSESP::webAPIService.webAPIService(&request); - request.url("/api/temperaturesensor/01-0203-0405-0607"); + request.url("/api/temperaturesensor/01_0203_0405_0607"); EMSESP::webAPIService.webAPIService(&request); ok = true; @@ -810,7 +810,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const shell.invoke_command("call system publish"); // rename - EMSESP::temperaturesensor_.update("01-0203-0405-0607", "testtemperature", 2); + EMSESP::temperaturesensor_.update("01_0203_0405_0607", "testtemperature", 2); shell.invoke_command("show values"); shell.invoke_command("call system publish"); ok = true; diff --git a/src/web/WebCustomizationService.cpp b/src/web/WebCustomizationService.cpp index 407acbbf4..afefbb992 100644 --- a/src/web/WebCustomizationService.cpp +++ b/src/web/WebCustomizationService.cpp @@ -99,6 +99,10 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c sensor.id = sensorJson["id"].as(); sensor.name = sensorJson["name"].as(); sensor.offset = sensorJson["offset"]; + if (sensor.id == sensor.name) { + sensor.name = ""; // no need to store id as name + } + std::replace(sensor.id.begin(), sensor.id.end(), '-', '_'); // change old ids to v3.7 style customizations.sensorCustomizations.push_back(sensor); // add to list } }