[BT] BM2 Tracker addition and Discovery adjustment (#1765)

* BM2 as Tracker additions

* BM2 discovery update
This commit is contained in:
DigiH authored and GitHub committed 2023-10-12 15:29:20 -05:00
1 parent 22b503762c
commit 45bd18b03c
3 files changed
+30 -14

No files matched your search

+1 -1
View File
@@ -46,7 +46,7 @@ Once the data has been transmitted to the MQTT broker, it can be easily integrat
Examples of compatible sensors among [our list](https://decoder.theengs.io/devices/devices_by_brand.html: Mi Flora, Mi jia, LYWDS02, LYWSD03MMC, ClearGrass, Mi scale, iBBQ, TPMS Examples of compatible sensors among [our list](https://decoder.theengs.io/devices/devices_by_brand.html: Mi Flora, Mi jia, LYWDS02, LYWSD03MMC, ClearGrass, Mi scale, iBBQ, TPMS
## Receiving signals from BLE tracker devices for Presence detection ## Receiving signals from BLE tracker devices for Presence detection
The gateway can detect the BLE trackers from Tile, NUT, TAGIT, ITAG, MiBand, Amazfit and RuuviTag and create automatically a device tracker entity following the Home Assistant discovery convention (if the auto discovery is activated). The gateway can detect BLE trackers from Tile, Nut, TagIt and iTag, as well as other devices with additional properties decoding like Mi Band, Amazfit, RuuviTag and others indicated as Presence Trackers in the [compatible BLE devices list](https://decoder.theengs.io/devices/devices.html), and automatically creates a device tracker entity following the Home Assistant discovery convention (if auto discovery is activated).
To do this activate the "BT: Publish HASS presence" switch in your controller or send the following MQTT command to your broker: To do this activate the "BT: Publish HASS presence" switch in your controller or send the following MQTT command to your broker:
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"hasspresence":true}'` `mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"hasspresence":true}'`
+29 -12
View File
@@ -504,6 +504,7 @@ void updateDevicesStatus() {
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILE || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILE ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILEN || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TILEN ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::ITAG || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::ITAG ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::BM2 ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV1 || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV1 ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV2)) { // We apply the offline status only for tracking device, can be extended further to all the devices p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::RUUVITAG_RAWV2)) { // We apply the offline status only for tracking device, can be extended further to all the devices
if ((p->lastUpdate != 0) && (p->lastUpdate < (now - BTConfig.presenceAwayTimer) && (now > BTConfig.presenceAwayTimer)) && if ((p->lastUpdate != 0) && (p->lastUpdate < (now - BTConfig.presenceAwayTimer) && (now > BTConfig.presenceAwayTimer)) &&
@@ -799,7 +800,7 @@ void BLEconnect() {
DT24_connect BLEclient(addr); DT24_connect BLEclient(addr);
BLEclient.processActions(BLEactions); BLEclient.processActions(BLEactions);
BLEclient.publishData(); BLEclient.publishData();
} else if (p->sensorModel_id == BLEconectable::id::BM2) { } else if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::BM2) {
BM2_connect BLEclient(addr); BM2_connect BLEclient(addr);
BLEclient.processActions(BLEactions); BLEclient.processActions(BLEactions);
BLEclient.publishData(); BLEclient.publishData();
@@ -831,7 +832,7 @@ void BLEconnect() {
if (p->sensorModel_id != BLEconectable::id::DT24_BLE && if (p->sensorModel_id != BLEconectable::id::DT24_BLE &&
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC &&
p->sensorModel_id != BLEconectable::id::LYWSD03MMC && p->sensorModel_id != BLEconectable::id::LYWSD03MMC &&
p->sensorModel_id != BLEconectable::id::BM2 && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::BM2 &&
p->sensorModel_id != BLEconectable::id::MHO_C401 && p->sensorModel_id != BLEconectable::id::MHO_C401 &&
p->sensorModel_id != BLEconectable::id::XMWSDJ04MMC) { p->sensorModel_id != BLEconectable::id::XMWSDJ04MMC) {
// if irregulary connected to and connection failed clear the connect flag. // if irregulary connected to and connection failed clear the connect flag.
@@ -1048,10 +1049,7 @@ void launchBTDiscovery(bool overrideDiscovery) {
if (overrideDiscovery || !isDiscovered(p)) { if (overrideDiscovery || !isDiscovered(p)) {
String macWOdots = String(p->macAdr); String macWOdots = String(p->macAdr);
macWOdots.replace(":", ""); macWOdots.replace(":", "");
if (!BTConfig.extDecoderEnable && // Do not decode if an external decoder is configured if (p->sensorModel_id >= 0) {
p->sensorModel_id > TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL &&
p->sensorModel_id < TheengsDecoder::BLE_ID_NUM::BLE_ID_MAX &&
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::BM2) { // Exception on HHCCJCY01HHCC and BM2 as these ones are discoverable and connectable
Log.trace(F("Looking for Model_id: %d" CR), p->sensorModel_id); Log.trace(F("Looking for Model_id: %d" CR), p->sensorModel_id);
std::string properties = decoder.getTheengProperties(p->sensorModel_id); std::string properties = decoder.getTheengProperties(p->sensorModel_id);
Log.trace(F("properties: %s" CR), properties.c_str()); Log.trace(F("properties: %s" CR), properties.c_str());
@@ -1059,6 +1057,10 @@ void launchBTDiscovery(bool overrideDiscovery) {
std::string model = decoder.getTheengAttribute(p->sensorModel_id, "model"); std::string model = decoder.getTheengAttribute(p->sensorModel_id, "model");
std::string model_id = decoder.getTheengAttribute(p->sensorModel_id, "model_id"); std::string model_id = decoder.getTheengAttribute(p->sensorModel_id, "model_id");
String discovery_topic = String(subjectBTtoMQTT) + "/" + macWOdots; String discovery_topic = String(subjectBTtoMQTT) + "/" + macWOdots;
if (!BTConfig.extDecoderEnable && // Do not decode if an external decoder is configured
p->sensorModel_id > TheengsDecoder::BLE_ID_NUM::UNKNOWN_MODEL &&
p->sensorModel_id < TheengsDecoder::BLE_ID_NUM::BLE_ID_MAX &&
p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC && p->sensorModel_id != TheengsDecoder::BLE_ID_NUM::BM2) { // Exception on HHCCJCY01HHCC and BM2 as these ones are discoverable and connectable
if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::NUT || if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::NUT ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::MIBAND || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::MIBAND ||
p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TAGIT || p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::TAGIT ||
@@ -1145,8 +1147,18 @@ void launchBTDiscovery(bool overrideDiscovery) {
if (p->sensorModel_id == BLEconectable::id::DT24_BLE) { if (p->sensorModel_id == BLEconectable::id::DT24_BLE) {
DT24Discovery(macWOdots.c_str(), "DT24-BLE"); DT24Discovery(macWOdots.c_str(), "DT24-BLE");
} }
if (p->sensorModel_id == BLEconectable::id::BM2) { if (p->sensorModel_id == TheengsDecoder::BLE_ID_NUM::BM2) {
// Sensor discovery
BM2Discovery(macWOdots.c_str(), "BM2"); BM2Discovery(macWOdots.c_str(), "BM2");
// Device tracker discovery
String tracker_id = macWOdots + "-tracker";
createDiscovery("device_tracker",
discovery_topic.c_str(), "BM2-tracker", tracker_id.c_str(),
will_Topic, "occupancy", "{% if value_json.get('rssi') -%}home{%- else -%}not_home{%- endif %}",
"", "", "",
0, "", "", false, "",
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
stateClassNone);
} }
if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC) { if (p->sensorModel_id == BLEconectable::id::LYWSD03MMC) {
LYWSD03MMCDiscovery(macWOdots.c_str(), "LYWSD03MMC"); LYWSD03MMCDiscovery(macWOdots.c_str(), "LYWSD03MMC");
@@ -1164,6 +1176,7 @@ void launchBTDiscovery(bool overrideDiscovery) {
Log.trace(F("Device UNKNOWN_MODEL %s" CR), p->macAdr); Log.trace(F("Device UNKNOWN_MODEL %s" CR), p->macAdr);
} }
} }
}
p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice p->isDisc = true; // we don't need the semaphore and all the search magic via createOrUpdateDevice
} else { } else {
Log.trace(F("Device already discovered or that doesn't require discovery %s" CR), p->macAdr); Log.trace(F("Device already discovered or that doesn't require discovery %s" CR), p->macAdr);
@@ -1216,8 +1229,14 @@ void process_bledata(JsonObject& BLEdata) {
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 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
if (model_id >= 0) { // Broadcaster devices if (model_id >= 0) { // Broadcaster devices
Log.trace(F("Decoder found device: %s" CR), BLEdata["model_id"].as<const char*>()); Log.trace(F("Decoder found device: %s" CR), BLEdata["model_id"].as<const char*>());
if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC || model_id == TheengsDecoder::BLE_ID_NUM::BM2) { if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC || model_id == TheengsDecoder::BLE_ID_NUM::BM2) { // Device that broadcast and can be connected
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type); // Device that broadcast and can be connected if (BLEdata.containsKey("name")) {
std::string name = BLEdata["name"];
if (name.compare("Battery Monitor") == 0 || name.compare("Li Battery Monitor") == 0 || name.compare("ZX-1689") == 0)
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type);
}
if (model_id == TheengsDecoder::BLE_ID_NUM::HHCCJCY01HHCC)
createOrUpdateDevice(mac, device_flags_connect, model_id, mac_type);
} else { } else {
createOrUpdateDevice(mac, device_flags_init, model_id, mac_type); createOrUpdateDevice(mac, device_flags_init, model_id, mac_type);
if (BTConfig.adaptiveScan == true && (BTConfig.BLEinterval != MinTimeBtwScan || BTConfig.intervalActiveScan != MinTimeBtwScan)) { if (BTConfig.adaptiveScan == true && (BTConfig.BLEinterval != MinTimeBtwScan || BTConfig.intervalActiveScan != MinTimeBtwScan)) {
@@ -1242,14 +1261,12 @@ void process_bledata(JsonObject& BLEdata) {
} }
} }
} else { } else {
if (BLEdata.containsKey("name")) { // Connectable devices if (BLEdata.containsKey("name")) { // Connectable only devices
std::string name = BLEdata["name"]; std::string name = BLEdata["name"];
if (name.compare("LYWSD03MMC") == 0) if (name.compare("LYWSD03MMC") == 0)
model_id = BLEconectable::id::LYWSD03MMC; model_id = BLEconectable::id::LYWSD03MMC;
else if (name.compare("DT24-BLE") == 0) else if (name.compare("DT24-BLE") == 0)
model_id = BLEconectable::id::DT24_BLE; model_id = BLEconectable::id::DT24_BLE;
else if (name.compare("Battery Monitor") == 0 || name.compare("Li Battery Monitor") == 0 || name.compare("ZX-1689") == 0)
model_id = BLEconectable::id::BM2;
else if (name.compare("MHO-C401") == 0) else if (name.compare("MHO-C401") == 0)
model_id = BLEconectable::id::MHO_C401; model_id = BLEconectable::id::MHO_C401;
else if (name.compare("XMWSDJ04MMC") == 0) else if (name.compare("XMWSDJ04MMC") == 0)
-1
View File
@@ -231,7 +231,6 @@ public:
LYWSD03MMC, LYWSD03MMC,
MHO_C401, MHO_C401,
DT24_BLE, DT24_BLE,
BM2,
XMWSDJ04MMC, XMWSDJ04MMC,
MAX, MAX,
}; };