Skip to content

Commit e1aef48

Browse files
authored
[Breaking] Deactivate Home Assistant presence per default (#915)
And enable to activate it by MQTT
1 parent adc5f15 commit e1aef48

File tree

5 files changed

+41
-18
lines changed

5 files changed

+41
-18
lines changed

docs/integrate/home_assistant.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ sensor:
168168
169169
### MQTT Room Presence
170170
171+
The publication into presence topic needs to be activated [here is the command](../use/ble.md)
172+
171173
```yaml
172174
sensor:
173175
- platform: mqtt_room

docs/use/ble.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,13 @@ If you want to change this characteristic:
133133
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
134134
:::
135135

136+
## Setting if the gateway publish into Home Assistant Home presence topic
137+
138+
If you want to publish to Home Assistant presence topic, you can activate this function by the HASS interface (this command is auto discovered), [here is a yaml example](../integrate/home_assistant.md#mqtt-room-presence).
139+
Or by an MQTT command.
140+
141+
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"hasspresence":true}'`
142+
136143
## Setting the minimum RSSI accepted to publish device data
137144

138145
If you want to change the minimum RSSI value accepted for a device to be published, you can change it by MQTT. For example if you want to set -80

main/ZgatewayBT.ino

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) {
9898
pub((char*)mactopic.c_str(), data);
9999
}
100100
if (haPresenceEnabled && data.containsKey("distance")) {
101-
data.remove("servicedatauuid");
102-
data.remove("servicedata");
101+
if (data.containsKey("servicedatauuid"))
102+
data.remove("servicedatauuid");
103+
if (data.containsKey("servicedata"))
104+
data.remove("servicedata");
103105
String topic = String(Base_Topic) + "home_presence/" + String(gateway_name);
104106
pub_custom_topic((char*)topic.c_str(), data, false);
105107
}
@@ -168,6 +170,7 @@ void emptyBTQueue() {
168170
JsonBundle& bundle = jsonBTBufferQueue[next % BTQueueSize];
169171
pubBTMainCore(*bundle.object, bundle.haPresence);
170172
atomic_store_explicit(&jsonBTBufferQueueNext, (next + 1) % (2 * BTQueueSize), ::memory_order_seq_cst); // use namespace std -> ambiguous error...
173+
vTaskDelay(1);
171174
}
172175
}
173176

@@ -570,11 +573,9 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
570573
BLEdata.set("rssi", (int)advertisedDevice->getRSSI());
571574
if (advertisedDevice->haveTXPower())
572575
BLEdata.set("txpower", (int8_t)advertisedDevice->getTXPower());
573-
# ifdef subjectHomePresence
574-
if (advertisedDevice->haveRSSI() && !publishOnlySensors) {
575-
haRoomPresence(BLEdata); // this device has an rssi and we don't want only sensors so in consequence we can use it for home assistant room presence component
576+
if (advertisedDevice->haveRSSI() && !publishOnlySensors && hassPresence) {
577+
hass_presence(BLEdata); // this device has an rssi and we don't want only sensors so in consequence we can use it for home assistant room presence component
576578
}
577-
# endif
578579
if (advertisedDevice->haveServiceData()) {
579580
int serviceDataCount = advertisedDevice->getServiceDataCount();
580581
Log.trace(F("Get services data number: %d" CR), serviceDataCount);
@@ -966,10 +967,8 @@ bool BTtoMQTT() {
966967
return false; //if we have at least one white mac and this mac is not white we go out
967968

968969
BLEdata.set("rssi", (int)rssi);
969-
# ifdef subjectHomePresence
970-
if (!publishOnlySensors)
971-
haRoomPresence(BLEdata); // this device has an rssi and we don't want only sensors so in consequence we can use it for home assistant room presence component
972-
# endif
970+
if (!publishOnlySensors && hassPresence)
971+
hass_presence(BLEdata); // this device has an rssi and we don't want only sensors so in consequence we can use it for home assistant room presence component
973972
Log.trace(F("Service data: %s" CR), restData.c_str());
974973
BLEdata.set("servicedata", restData.c_str());
975974
PublishDeviceData(BLEdata);
@@ -1513,8 +1512,7 @@ JsonObject& process_inode_em(JsonObject& BLEdata) {
15131512
return BLEdata;
15141513
}
15151514

1516-
# ifdef subjectHomePresence
1517-
void haRoomPresence(JsonObject& HomePresence) {
1515+
void hass_presence(JsonObject& HomePresence) {
15181516
int BLErssi = HomePresence["rssi"];
15191517
Log.trace(F("BLErssi %d" CR), BLErssi);
15201518
int txPower = HomePresence["txpower"] | 0;
@@ -1531,7 +1529,6 @@ void haRoomPresence(JsonObject& HomePresence) {
15311529
HomePresence["distance"] = distance;
15321530
Log.trace(F("Ble distance %D" CR), distance);
15331531
}
1534-
# endif
15351532

15361533
void BTforceScan() {
15371534
if (!ProcessLock) {
@@ -1604,6 +1601,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
16041601
bleConnect = (bool)BTdata["bleconnect"];
16051602
Log.notice(F("New value bleConnect: %T" CR), bleConnect);
16061603
}
1604+
if (BTdata.containsKey("lowpowermode")) {
1605+
changelowpowermode((int)BTdata["lowpowermode"]);
1606+
}
16071607
# endif
16081608
// MinRSSI set
16091609
if (BTdata.containsKey("minrssi")) {
@@ -1613,11 +1613,14 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
16131613
minRssi = abs((int)BTdata["minrssi"]);
16141614
Log.notice(F("New minrssi: %d" CR), minRssi);
16151615
}
1616-
# ifdef ESP32
1617-
if (BTdata.containsKey("lowpowermode")) {
1618-
changelowpowermode((int)BTdata["lowpowermode"]);
1616+
// Home Assistant presence message
1617+
if (BTdata.containsKey("hasspresence")) {
1618+
// storing Min RSSI for further use if needed
1619+
Log.trace(F("Previous hasspresence: %T" CR), hassPresence);
1620+
// set Min RSSI if present if not setting default value
1621+
hassPresence = (bool)BTdata["hasspresence"];
1622+
Log.notice(F("New hasspresence: %T" CR), hassPresence);
16191623
}
1620-
# endif
16211624
}
16221625
}
16231626
#endif

main/ZmqttDiscovery.ino

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,14 @@ void pubMqttDiscovery() {
700700
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
701701
"", "", "", "", true // device name, device manufacturer, device model, device mac, retain
702702
);
703+
createDiscovery("switch", //set Type
704+
"", "BT: Publish HASS presence", (char*)getUniqueId("hasspresence", "").c_str(), //set state_topic,name,uniqueId
705+
"", "", "", //set availability_topic,device_class,value_template,
706+
"{\"hasspresence\":true}", "{\"hasspresence\":false}", "", //set,payload_on,payload_off,unit_of_meas,
707+
0, //set off_delay
708+
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
709+
"", "", "", "", true // device name, device manufacturer, device model, device mac, retain
710+
);
703711
# ifdef ESP32
704712
createDiscovery("switch", //set Type
705713
"", "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId

main/config_BT.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ bool bleConnect = AttemptBLECOnnect;
7171
#ifndef PublishOnlySensors
7272
# define PublishOnlySensors false //false if we publish all BLE devices discovered or true only the identified sensors (like temperature sensors)
7373
#endif
74+
#ifndef HassPresence
75+
# define HassPresence false //false if we publish into Home Assistant presence topic
76+
#endif
7477

7578
#ifndef BTQueueSize
7679
# define BTQueueSize 4 // lockless queue size for multi core cases (ESP32 currently)
@@ -92,6 +95,7 @@ unsigned int BLEinterval = TimeBtwRead; //time between 2 scans
9295
unsigned int BLEscanBeforeConnect = ScanBeforeConnect; //Number of BLE scans between connection cycles
9396
unsigned long scanCount = 0;
9497
bool publishOnlySensors = PublishOnlySensors;
98+
bool hassPresence = HassPresence;
9599

96100
#ifndef pubKnownBLEServiceData
97101
# define pubKnownBLEServiceData false // define true if you want to publish service data belonging to recognised sensors
@@ -110,7 +114,6 @@ bool publishOnlySensors = PublishOnlySensors;
110114
#endif
111115

112116
/*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/
113-
// if not commented Home presence integration with HOME ASSISTANT is activated
114117
#define subjectHomePresence "home_presence/" // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name)
115118

116119
enum ble_sensor_model {

0 commit comments

Comments
 (0)