change temperaturesensor id to underscore notation

This commit is contained in:
MichaelDvP
2024-07-04 13:42:23 +02:00
parent 147804b4b8
commit cee2d5ca31
4 changed files with 15 additions and 9 deletions

View File

@@ -389,7 +389,13 @@ export const entityItemValidation = (entity: EntityItem[], entityItem: EntityIte
export const temperatureSensorItemValidation = () => export const temperatureSensorItemValidation = () =>
new Schema({ 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[]) => ({ export const isGPIOUniqueValidator = (sensors: AnalogSensor[]) => ({

View File

@@ -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); Mqtt::add_ha_sections_to_doc("temperature", stat_t, config, !is_ha_device_created, val_cond);
char topic[Mqtt::MQTT_TOPIC_MAX_SIZE]; char topic[Mqtt::MQTT_TOPIC_MAX_SIZE];
// use '_' as HA doesn't like '-' in the topic name snprintf(topic, sizeof(topic), "sensor/%s/%s_%s/config", Mqtt::basename().c_str(), F_(temperaturesensor), sensor.id().c_str());
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());
sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>()); sensor.ha_registered = Mqtt::queue_ha(topic, config.as<JsonObject>());
} }
@@ -580,7 +576,7 @@ TemperatureSensor::Sensor::Sensor(const uint8_t addr[])
char id_s[20]; char id_s[20];
snprintf(id_s, snprintf(id_s,
sizeof(id_s), sizeof(id_s),
"%02X-%04X-%04X-%04X", "%02X_%04X_%04X_%04X",
(unsigned int)(internal_id_ >> 48) & 0xFF, (unsigned int)(internal_id_ >> 48) & 0xFF,
(unsigned int)(internal_id_ >> 32) & 0xFFFF, (unsigned int)(internal_id_ >> 32) & 0xFFFF,
(unsigned int)(internal_id_ >> 16) & 0xFFFF, (unsigned int)(internal_id_ >> 16) & 0xFFFF,

View File

@@ -793,7 +793,7 @@ void Test::run_test(uuid::console::Shell & shell, const std::string & cmd, const
EMSESP::webAPIService.webAPIService(&request); EMSESP::webAPIService.webAPIService(&request);
request.url("/api/temperaturesensor/info"); request.url("/api/temperaturesensor/info");
EMSESP::webAPIService.webAPIService(&request); EMSESP::webAPIService.webAPIService(&request);
request.url("/api/temperaturesensor/01-0203-0405-0607"); request.url("/api/temperaturesensor/01_0203_0405_0607");
EMSESP::webAPIService.webAPIService(&request); EMSESP::webAPIService.webAPIService(&request);
ok = true; 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"); shell.invoke_command("call system publish");
// rename // 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("show values");
shell.invoke_command("call system publish"); shell.invoke_command("call system publish");
ok = true; ok = true;

View File

@@ -99,6 +99,10 @@ StateUpdateResult WebCustomization::update(JsonObject root, WebCustomization & c
sensor.id = sensorJson["id"].as<std::string>(); sensor.id = sensorJson["id"].as<std::string>();
sensor.name = sensorJson["name"].as<std::string>(); sensor.name = sensorJson["name"].as<std::string>();
sensor.offset = sensorJson["offset"]; 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 customizations.sensorCustomizations.push_back(sensor); // add to list
} }
} }