diff --git a/docs/use/displays.md b/docs/use/displays.md index 572a56523d..4b37aa8d31 100644 --- a/docs/use/displays.md +++ b/docs/use/displays.md @@ -54,6 +54,15 @@ As the display Metric setting is being defined in the WebUI part of OpenMQTTGate `mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoWebUI/config -m {"displayMetric":false}` +### Display name as advetised Bluetooth name or `model_id` +There is a build property of ForceDeviceName which forces devices when they are added in Home Assistant auto-discovery to be created with their Bluetooth advertised name isntead of their `model_id`. The default naming is `model_id` with `{"displayDeviceName":true}`. If you have enabled auto-discovery then a restart is required. + +This can also be adjusted in the WebUI by switching the Configure WebUI Device naming between `Model ID` (false) or `Device name` (true) + +This can also be changed with the runtime command. + +`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoWebUI/config -m {"displayDeviceName":true}` + ### Rotating the display by 180 degrees This can be set with the compiler directive `-DDISPLAY_FLIP=false`. diff --git a/docs/use/webui.md b/docs/use/webui.md index b3004a91af..c43a590b90 100644 --- a/docs/use/webui.md +++ b/docs/use/webui.md @@ -28,7 +28,7 @@ Ability to change the mqtt settings, if the change is unsuccessful it will rever ## WebUI -Ability to change the display of sensor to Metric or Imperial, and disable the WebUI Authentication +Ability to change the display of sensor to Metric or Imperial, display the device name as the Bluetooth advertised device name or the model id and disable the WebUI Authentication ## Bluetooth Low Energy - BLE diff --git a/main/TheengsCommon.h b/main/TheengsCommon.h index f52cea64cb..31160a21bc 100644 --- a/main/TheengsCommon.h +++ b/main/TheengsCommon.h @@ -62,6 +62,7 @@ extern bool ready_to_sleep; extern char mqtt_topic[]; extern char gateway_name[]; extern unsigned long lastDiscovery; // Time of the last discovery to trigger automaticaly to off after DiscoveryAutoOffTimer +extern bool displayDeviceName; #if BLEDecryptor extern char ble_aes[]; diff --git a/main/User_config.h b/main/User_config.h index e11e3a2e98..1b4c3c9ccd 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -604,6 +604,11 @@ extern ss_cnt_parameters cnt_parameters_array[]; # define LOG_LEVEL LOG_LEVEL_NOTICE #endif +/*-------------------DEFINE DISPLAY NAME-------------------*/ +#ifndef DISPLAY_DEVICE_NAME +# define DISPLAY_DEVICE_NAME false // Set to true to force the device name to be from the name of the device and not the model +#endif + /*-------------------ESP Wifi band and tx power ---------------------*/ //Certain sensors are sensitive to Wifi which can cause interference with their normal operation //For example it can cause false triggers on a PIR HC-SR501 diff --git a/main/config_WebContent.h b/main/config_WebContent.h index eccf82f740..1cfc8cd2b1 100644 --- a/main/config_WebContent.h +++ b/main/config_WebContent.h @@ -116,7 +116,7 @@ const char config_gateway_body[] = body_header "
OpenMQTTGateway Logging

Log Level


" body_footer_config_menu; -const char config_webui_body[] = body_header "
Configure WebUI

Display Metric

Secure WebUI


" body_footer_config_menu; +const char config_webui_body[] = body_header "
Configure WebUI

Display Metric

Device naming

Secure WebUI


" body_footer_config_menu; const char config_rf_body[] = body_header "
" diff --git a/main/gatewayBT.cpp b/main/gatewayBT.cpp index 8f676c4b36..efd9fd0523 100644 --- a/main/gatewayBT.cpp +++ b/main/gatewayBT.cpp @@ -870,7 +870,7 @@ void setupBTTasksAndBLE() { # if defined(USE_ESP_IDF) || defined(USE_BLUFI) 14500, # else - 9500, /* Stack size in bytes */ + 10500, /* Stack size in bytes */ # endif NULL, /* Task input parameter */ 2, /* Priority of the task (set higher than core task) */ @@ -955,11 +955,11 @@ void launchBTDiscovery(bool overrideDiscovery) { Log.trace(F("properties: %s" CR), properties.c_str()); std::string brand = decoder.getTheengAttribute(p->sensorModel_id, "brand"); std::string model = decoder.getTheengAttribute(p->sensorModel_id, "model"); -# if ForceDeviceName - if (p->name[0] != '\0') { - model = p->name; + if (displayDeviceName || ForceDeviceName) { + if (p->name[0] != '\0') { + model = p->name; + } } -# endif std::string model_id = decoder.getTheengAttribute(p->sensorModel_id, "model_id"); // Check for tracker status @@ -1014,7 +1014,12 @@ void launchBTDiscovery(bool overrideDiscovery) { Log.trace(F("Key: %s"), prop.key().c_str()); Log.trace(F("Unit: %s"), prop.value()["unit"].as()); Log.trace(F("Name: %s"), prop.value()["name"].as()); - String entity_name = String(model_id.c_str()) + "-" + String(prop.key().c_str()); + String entity_name = ""; + if (displayDeviceName || ForceDeviceName) { + entity_name = String(model.c_str()) + "-" + String(prop.key().c_str()); + } else { + entity_name = String(model_id.c_str()) + "-" + String(prop.key().c_str()); + } String unique_id = macWOdots + "-" + String(prop.key().c_str()); String value_template = "{{ value_json." + String(prop.key().c_str()) + " | is_defined }}"; if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::SBS1 && strcmp(prop.key().c_str(), "state") == 0) { @@ -1436,8 +1441,8 @@ void process_bledata(JsonObject& BLEdata) { Log.notice(F("Active and continuous scanning required, parameters adapted" CR)); stateBTMeasures(false); } - } else if (BLEdata.containsKey("cont") && BTConfig.BLEinterval != MinTimeBtwScan) { - if (BLEdata["cont"]) { + } else if (BLEdata.containsKey("acts") && BTConfig.BLEinterval != MinTimeBtwScan) { + if (BLEdata["acts"]) { BTConfig.BLEinterval = MinTimeBtwScan; if ((BLEdata["type"].as()).compare("CTMO") == 0) { BTConfig.scanDuration = MinScanDuration; diff --git a/main/main.cpp b/main/main.cpp index 8f75b30bac..17090cb60d 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -292,6 +292,7 @@ bool failSafeMode = false; bool ProcessLock = true; // Process lock when we want to use a critical function like OTA for example bool mqttSetupPending = true; static int cnt_index = CNT_DEFAULT_INDEX; +bool displayDeviceName = DISPLAY_DEVICE_NAME; #ifdef ESP32 # include diff --git a/main/mqttDiscovery.cpp b/main/mqttDiscovery.cpp index 9362a05ecb..f31e08c64c 100644 --- a/main/mqttDiscovery.cpp +++ b/main/mqttDiscovery.cpp @@ -598,7 +598,11 @@ void createDiscovery(const char* sensor_type, // generate unique device name by adding the second half of the device_id only if device_name and device_id are different and we don't want to use the BLE name if (device_name[0]) { - if (strcmp(device_id, device_name) != 0 && device_id[0] && !ForceDeviceName) { + #if defined(ZwebUI) && defined(ESP32) // displayDeviceName only applies when running with the WebUI and ESP32 + if (strcmp(device_id, device_name) != 0 && device_id[0] && !displayDeviceName) { + #elif !ForceDeviceName // Support ForceDeviceName for esp8266's + if (strcmp(device_id, device_name) != 0 && device_id[0] && !ForceDeviceName) { + #endif device["name"] = device_name + String("-") + String(device_id + 6); } else { device["name"] = device_name; diff --git a/main/webUI.cpp b/main/webUI.cpp index f6be12f40f..4fb11178f6 100644 --- a/main/webUI.cpp +++ b/main/webUI.cpp @@ -504,8 +504,9 @@ void handleCN() { * @brief /WU - Configuration Page * T: handleWU: uri: /wu, args: 3, method: 1 * T: handleWU Arg: 0, dm=on - displayMetric - * T: handleWU Arg: 1, sw=on - webUISecure - * T: handleWU Arg: 2, save= + * T: handleWU Arg: 1, dn=1 - displayDeviceName + * T: handleWU Arg: 2, sw=on - webUISecure + * T: handleWU Arg: 3, save= */ void handleWU() { WEBUI_TRACE_LOG(F("handleWU: uri: %s, args: %d, method: %d" CR), server.uri(), server.args(), server.method()); @@ -521,6 +522,11 @@ void handleWU() { } displayMetric = server.hasArg("dm"); + if (server.hasArg("dn") && server.arg("dn").toInt() != displayDeviceName) { + displayDeviceName = server.arg("dn").toInt(); + update = true; + } + if (webUISecure != server.hasArg("sw")) { update = true; } @@ -541,7 +547,7 @@ void handleWU() { response += String(script); response += String(style); int logLevel = Log.getLevel(); - snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_webui_body, jsonChar, gateway_name, (displayMetric ? "checked" : ""), (webUISecure ? "checked" : "")); + snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, config_webui_body, jsonChar, gateway_name, (displayMetric ? "checked" : ""), (!displayDeviceName ? "selected" : ""), (displayDeviceName ? "selected" : ""), (webUISecure ? "checked" : "")); response += String(buffer); snprintf(buffer, WEB_TEMPLATE_BUFFER_MAX_SIZE, footer, OMG_VERSION); response += String(buffer); @@ -1863,6 +1869,7 @@ String stateWebUIStatus() { StaticJsonDocument WebUIdataBuffer; JsonObject WebUIdata = WebUIdataBuffer.to(); WebUIdata["displayMetric"] = (bool)displayMetric; + WebUIdata["displayDeviceName"] = (bool)displayDeviceName; WebUIdata["webUISecure"] = (bool)webUISecure; WebUIdata["displayQueue"] = uxQueueMessagesWaiting(webUIQueue); @@ -1879,6 +1886,7 @@ bool WebUIConfig_save() { StaticJsonDocument jsonBuffer; JsonObject jo = jsonBuffer.to(); jo["displayMetric"] = (bool)displayMetric; + jo["displayDeviceName"] = (bool)displayDeviceName; jo["webUISecure"] = (bool)webUISecure; // Save config into NVS (non-volatile storage) String conf = ""; @@ -1912,6 +1920,7 @@ bool WebUIConfig_load() { } JsonObject jo = jsonBuffer.as(); displayMetric = jo["displayMetric"].as(); + displayDeviceName = jo["displayDeviceName"].as(); webUISecure = jo["webUISecure"].as(); return true; } else {