Skip to content

Commit d4d4331

Browse files
committed
Ability to set device name without suffix
- Simply Add -DForceDeviceNameNoSuffix
1 parent 401be5f commit d4d4331

3 files changed

Lines changed: 76 additions & 16 deletions

File tree

main/config_mqttDiscovery.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,16 @@ extern char discovery_prefix[];
135135
# define GATEWAY_MANUFACTURER "OMG_community"
136136
#endif
137137

138+
// ForceDeviceName: Set to true to force the device name to be from the name of the device and not the model
139+
// When enabled, device names will use BLE device name (may still include MAC suffix for uniqueness if device_id differs)
138140
#ifndef ForceDeviceName
139-
# define ForceDeviceName false // Set to true to force the device name to be from the name of the device and not the model
141+
# define ForceDeviceName false
142+
#endif
143+
144+
// ForceDeviceNameNoSuffix: Set to true to force the device name to be from the name of the device without appending MAC suffix
145+
// This overrides ForceDeviceName behavior and ensures device name is used as-is without any MAC suffix
146+
#ifndef ForceDeviceNameNoSuffix
147+
# define ForceDeviceNameNoSuffix false
140148
#endif
141149

142150
/*-------------- Auto discovery macros-----------------*/

main/gatewayBT.cpp

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,9 @@ void DT24Discovery(const char* mac, const char* sensorModel_id) {
507507
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
508508
};
509509

510-
createDiscoveryFromList(mac, DT24sensor, DT24parametersCount, "DT24", "ATorch", sensorModel_id);
510+
// Use sensorModel_id (device name) as device_name if provided, otherwise use model name
511+
const char* device_name = (sensorModel_id && sensorModel_id[0] != '\0') ? sensorModel_id : "DT24-BLE";
512+
createDiscoveryFromList(mac, DT24sensor, DT24parametersCount, device_name, "ATorch", "DT24-BLE");
511513
}
512514

513515
void BM2Discovery(const char* mac, const char* sensorModel_id) {
@@ -519,21 +521,27 @@ void BM2Discovery(const char* mac, const char* sensorModel_id) {
519521
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
520522
};
521523

522-
createDiscoveryFromList(mac, BM2sensor, BM2parametersCount, "BM2", "Generic", sensorModel_id);
524+
// Use sensorModel_id (device name) as device_name if provided, otherwise use model name
525+
const char* device_name = (sensorModel_id && sensorModel_id[0] != '\0') ? sensorModel_id : "BM2";
526+
createDiscoveryFromList(mac, BM2sensor, BM2parametersCount, device_name, "Generic", "BM2");
523527
}
524528

525529
void LYWSD03MMCDiscovery(const char* mac, const char* sensorModel) {
526-
# define LYWSD03MMCparametersCount 4
530+
# define LYWSD03MMCparametersCount 6
527531
THEENGS_LOG_TRACE(F("LYWSD03MMCDiscovery" CR));
528532
const char* LYWSD03MMCsensor[LYWSD03MMCparametersCount][9] = {
529533
{HASS_TYPE_SENSOR, "batt", mac, HASS_CLASS_BATTERY, jsonBatt, "", "", HASS_UNIT_PERCENT, stateClassMeasurement},
530534
{HASS_TYPE_SENSOR, "volt", mac, "", jsonVolt, "", "", HASS_UNIT_VOLT, stateClassMeasurement},
531535
{HASS_TYPE_SENSOR, "temp", mac, HASS_CLASS_TEMPERATURE, jsonTempc, "", "", HASS_UNIT_CELSIUS, stateClassMeasurement},
536+
{HASS_TYPE_SENSOR, "tempc", mac, HASS_CLASS_TEMPERATURE, jsonTempc, "", "", HASS_UNIT_CELSIUS, stateClassMeasurement},
537+
{HASS_TYPE_SENSOR, "tempf", mac, HASS_CLASS_TEMPERATURE, jsonTempf, "", "", HASS_UNIT_FAHRENHEIT, stateClassMeasurement},
532538
{HASS_TYPE_SENSOR, "hum", mac, HASS_CLASS_HUMIDITY, jsonHum, "", "", HASS_UNIT_PERCENT, stateClassMeasurement}
533539
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
534540
};
535541

536-
createDiscoveryFromList(mac, LYWSD03MMCsensor, LYWSD03MMCparametersCount, "LYWSD03MMC", "Xiaomi", sensorModel);
542+
// Use sensorModel (device name) as device_name if provided, otherwise use model name
543+
const char* device_name = (sensorModel && sensorModel[0] != '\0') ? sensorModel : "LYWSD03MMC";
544+
createDiscoveryFromList(mac, LYWSD03MMCsensor, LYWSD03MMCparametersCount, device_name, "Xiaomi", "LYWSD03MMC");
537545
}
538546

539547
void MHO_C401Discovery(const char* mac, const char* sensorModel) {
@@ -547,7 +555,9 @@ void MHO_C401Discovery(const char* mac, const char* sensorModel) {
547555
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
548556
};
549557

550-
createDiscoveryFromList(mac, MHO_C401sensor, MHO_C401parametersCount, "MHO_C401", "Xiaomi", sensorModel);
558+
// Use sensorModel (device name) as device_name if provided, otherwise use model name
559+
const char* device_name = (sensorModel && sensorModel[0] != '\0') ? sensorModel : "MHO_C401";
560+
createDiscoveryFromList(mac, MHO_C401sensor, MHO_C401parametersCount, device_name, "Xiaomi", "MHO-C401");
551561
}
552562

553563
void HHCCJCY01HHCCDiscovery(const char* mac, const char* sensorModel) {
@@ -562,7 +572,9 @@ void HHCCJCY01HHCCDiscovery(const char* mac, const char* sensorModel) {
562572
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
563573
};
564574

565-
createDiscoveryFromList(mac, HHCCJCY01HHCCsensor, HHCCJCY01HHCCparametersCount, "HHCCJCY01HHCC", "Xiaomi", sensorModel);
575+
// Use sensorModel (device name) as device_name if provided, otherwise use model name
576+
const char* device_name = (sensorModel && sensorModel[0] != '\0') ? sensorModel : "HHCCJCY01HHCC";
577+
createDiscoveryFromList(mac, HHCCJCY01HHCCsensor, HHCCJCY01HHCCparametersCount, device_name, "Xiaomi", "HHCCJCY01HHCC");
566578
}
567579

568580
void XMWSDJ04MMCDiscovery(const char* mac, const char* sensorModel) {
@@ -576,7 +588,9 @@ void XMWSDJ04MMCDiscovery(const char* mac, const char* sensorModel) {
576588
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
577589
};
578590

579-
createDiscoveryFromList(mac, XMWSDJ04MMCsensor, XMWSDJ04MMCparametersCount, "XMWSDJ04MMC", "Xiaomi", sensorModel);
591+
// Use sensorModel (device name) as device_name if provided, otherwise use model name
592+
const char* device_name = (sensorModel && sensorModel[0] != '\0') ? sensorModel : "XMWSDJ04MMC";
593+
createDiscoveryFromList(mac, XMWSDJ04MMCsensor, XMWSDJ04MMCparametersCount, device_name, "Xiaomi", "XMWSDJ04MMC");
580594
}
581595

582596
# else
@@ -1126,12 +1140,15 @@ void launchBTDiscovery(bool overrideDiscovery) {
11261140
p->sensorModel_id < BLEconectable::id::MAX) ||
11271141
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::BM2) {
11281142
// Discovery of sensors from which we retrieve data only by connect
1143+
// Use device name if available, otherwise use model name
1144+
const char* device_name_for_discovery = (p->name[0] != '\0') ? p->name : model.c_str();
1145+
11291146
if (p->sensorModel_id == BLEconectable::id::DT24_BLE) {
1130-
DT24Discovery(macWOdots.c_str(), "DT24-BLE");
1147+
DT24Discovery(macWOdots.c_str(), device_name_for_discovery);
11311148
}
11321149
if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::BM2) {
11331150
// Sensor discovery
1134-
BM2Discovery(macWOdots.c_str(), "BM2");
1151+
BM2Discovery(macWOdots.c_str(), device_name_for_discovery);
11351152
// Device tracker discovery
11361153
String tracker_id = macWOdots + "-tracker";
11371154
createDiscovery(HASS_TYPE_DEVICE_TRACKER,
@@ -1143,17 +1160,27 @@ void launchBTDiscovery(bool overrideDiscovery) {
11431160
stateClassNone);
11441161
}
11451162
if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC) {
1146-
LYWSD03MMCDiscovery(macWOdots.c_str(), "LYWSD03MMC");
1163+
LYWSD03MMCDiscovery(macWOdots.c_str(), device_name_for_discovery);
11471164
}
11481165
if (p->sensorModel_id == BLEconectable::id::MHO_C401) {
1149-
MHO_C401Discovery(macWOdots.c_str(), "MHO-C401");
1166+
MHO_C401Discovery(macWOdots.c_str(), device_name_for_discovery);
11501167
}
11511168
if (p->sensorModel_id == BLEconectable::id::XMWSDJ04MMC) {
1152-
XMWSDJ04MMCDiscovery(macWOdots.c_str(), "XMWSDJ04MMC");
1169+
XMWSDJ04MMCDiscovery(macWOdots.c_str(), device_name_for_discovery);
11531170
}
11541171
if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC) {
1155-
HHCCJCY01HHCCDiscovery(macWOdots.c_str(), "HHCCJCY01HHCC");
1172+
HHCCJCY01HHCCDiscovery(macWOdots.c_str(), device_name_for_discovery);
11561173
}
1174+
1175+
// Add RSSI sensor for connectable devices (RSSI is published in broadcast payload)
1176+
String rssi_id = macWOdots + "-rssi";
1177+
createDiscovery(HASS_TYPE_SENSOR,
1178+
discovery_topic.c_str(), "rssi", rssi_id.c_str(),
1179+
will_Topic, "signal_strength", jsonRSSI,
1180+
"", "", "dB",
1181+
0, "", "", false, "",
1182+
device_name_for_discovery, brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
1183+
stateClassMeasurement, nullptr, nullptr, nullptr, nullptr, true);
11571184
} else {
11581185
THEENGS_LOG_TRACE(F("Device UNKNOWN_MODEL %s" CR), p->macAdr);
11591186
}
@@ -1429,7 +1456,23 @@ void process_bledata(JsonObject& BLEdata) {
14291456
if ((BLEdata["type"].as<string>()).compare("RMAC") != 0 && model_id != TheengsDecoder::BLE_ID_NUM::IBEACON) { // Do not store in memory the random mac devices and iBeacons
14301457
if (model_id >= 0) { // Broadcaster devices
14311458
THEENGS_LOG_TRACE(F("Decoder found device: %s" CR), BLEdata["model_id"].as<const char*>());
1432-
if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC || model_id == TheengsDecoder::BLE_ID_NUM::BM2) { // Device that broadcast and can be connected
1459+
// Check if this is a connectable device by model_id string (for devices with custom names like PVVX firmware)
1460+
if (BLEdata.containsKey("model_id")) {
1461+
std::string model_id_str = BLEdata["model_id"].as<std::string>();
1462+
if (model_id_str.find("LYWSD03MMC") != std::string::npos) {
1463+
model_id = BLEconectable::id::LYWSD03MMC;
1464+
THEENGS_LOG_TRACE(F("Connectable device identified by model_id: LYWSD03MMC" CR));
1465+
} else if (model_id_str.find("MHO-C401") != std::string::npos || model_id_str.find("MHOC401") != std::string::npos) {
1466+
model_id = BLEconectable::id::MHO_C401;
1467+
THEENGS_LOG_TRACE(F("Connectable device identified by model_id: MHO-C401" CR));
1468+
} else if (model_id_str.find("XMWSDJ04MMC") != std::string::npos) {
1469+
model_id = BLEconectable::id::XMWSDJ04MMC;
1470+
THEENGS_LOG_TRACE(F("Connectable device identified by model_id: XMWSDJ04MMC" CR));
1471+
}
1472+
}
1473+
if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC || model_id == TheengsDecoder::BLE_ID_NUM::BM2 ||
1474+
model_id == BLEconectable::id::LYWSD03MMC || model_id == BLEconectable::id::MHO_C401 ||
1475+
model_id == BLEconectable::id::XMWSDJ04MMC) { // Device that broadcast and can be connected
14331476
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type, deviceName);
14341477
} else {
14351478
createOrUpdateDevice(mac, device_flags_init, model_id, mac_type, deviceName);

main/mqttDiscovery.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,11 +654,20 @@ void createDiscovery(const char* sensor_type,
654654

655655
// 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
656656
if (device_name[0]) {
657+
# ifdef ForceDeviceNameNoSuffix
658+
// ForceDeviceNameNoSuffix is enabled: always use the device name as-is without MAC suffix
659+
device["name"] = device_name;
660+
# else
661+
// Original ForceDeviceName behavior: use BLE device name instead of model
662+
// If ForceDeviceName is disabled, may add MAC suffix for uniqueness
657663
if (strcmp(device_id, device_name) != 0 && device_id[0] && !ForceDeviceName) {
664+
// Append MAC suffix (last 6 chars) if device_id and device_name differ and ForceDeviceName is disabled
658665
device["name"] = device_name + String("-") + String(device_id + 6);
659666
} else {
667+
// Use device name as-is (ForceDeviceName enabled, or device_id matches device_name, or no device_id)
660668
device["name"] = device_name;
661669
}
670+
# endif
662671
}
663672

664673
device["via_device"] = String(getMacAddress()); //mac address of the gateway so that the devices link to the gateway
@@ -1152,4 +1161,4 @@ void pubMqttDiscovery() {
11521161
}
11531162
#else
11541163
void pubMqttDiscovery() {}
1155-
#endif
1164+
#endif

0 commit comments

Comments
 (0)