Skip to content

Commit

Permalink
[Breaking] Deactivate Home Assistant presence per default (#915)
Browse files Browse the repository at this point in the history
And enable to activate it by MQTT
  • Loading branch information
1technophile authored Apr 5, 2021
1 parent adc5f15 commit e1aef48
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
2 changes: 2 additions & 0 deletions docs/integrate/home_assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ sensor:
### MQTT Room Presence
The publication into presence topic needs to be activated [here is the command](../use/ble.md)
```yaml
sensor:
- platform: mqtt_room
Expand Down
7 changes: 7 additions & 0 deletions docs/use/ble.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ If you want to change this characteristic:
With Home Assistant, this command is directly avalaible through MQTT auto discovery as a switch into the HASS OpenMQTTGateway device entities list.
:::

## Setting if the gateway publish into Home Assistant Home presence topic

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).
Or by an MQTT command.

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoBT/config -m '{"hasspresence":true}'`

## Setting the minimum RSSI accepted to publish device data

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
Expand Down
37 changes: 20 additions & 17 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) {
pub((char*)mactopic.c_str(), data);
}
if (haPresenceEnabled && data.containsKey("distance")) {
data.remove("servicedatauuid");
data.remove("servicedata");
if (data.containsKey("servicedatauuid"))
data.remove("servicedatauuid");
if (data.containsKey("servicedata"))
data.remove("servicedata");

This comment has been minimized.

Copy link
@csiki2

csiki2 Apr 6, 2021

Contributor

Why was this needed?
As far as I can see data.remove contains the check whether the key exist or not.
For me seems to be misleading and unnecessary.

This comment has been minimized.

Copy link
@1technophile

1technophile Apr 9, 2021

Author Owner

I have seen that there is a check into the library code indeed.
Nevertheless I decided to let it in the omg code, asking myself of the level of robustness we should have for this kind of operation. And I choose the most robust approach, but maybe this is a wrong approach.

String topic = String(Base_Topic) + "home_presence/" + String(gateway_name);
pub_custom_topic((char*)topic.c_str(), data, false);
}
Expand Down Expand Up @@ -168,6 +170,7 @@ void emptyBTQueue() {
JsonBundle& bundle = jsonBTBufferQueue[next % BTQueueSize];
pubBTMainCore(*bundle.object, bundle.haPresence);
atomic_store_explicit(&jsonBTBufferQueueNext, (next + 1) % (2 * BTQueueSize), ::memory_order_seq_cst); // use namespace std -> ambiguous error...
vTaskDelay(1);
}
}

Expand Down Expand Up @@ -570,11 +573,9 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
BLEdata.set("rssi", (int)advertisedDevice->getRSSI());
if (advertisedDevice->haveTXPower())
BLEdata.set("txpower", (int8_t)advertisedDevice->getTXPower());
# ifdef subjectHomePresence
if (advertisedDevice->haveRSSI() && !publishOnlySensors) {
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
if (advertisedDevice->haveRSSI() && !publishOnlySensors && hassPresence) {
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
}
# endif
if (advertisedDevice->haveServiceData()) {
int serviceDataCount = advertisedDevice->getServiceDataCount();
Log.trace(F("Get services data number: %d" CR), serviceDataCount);
Expand Down Expand Up @@ -966,10 +967,8 @@ bool BTtoMQTT() {
return false; //if we have at least one white mac and this mac is not white we go out

BLEdata.set("rssi", (int)rssi);
# ifdef subjectHomePresence
if (!publishOnlySensors)
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
# endif
if (!publishOnlySensors && hassPresence)
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
Log.trace(F("Service data: %s" CR), restData.c_str());
BLEdata.set("servicedata", restData.c_str());
PublishDeviceData(BLEdata);
Expand Down Expand Up @@ -1513,8 +1512,7 @@ JsonObject& process_inode_em(JsonObject& BLEdata) {
return BLEdata;
}

# ifdef subjectHomePresence
void haRoomPresence(JsonObject& HomePresence) {
void hass_presence(JsonObject& HomePresence) {
int BLErssi = HomePresence["rssi"];
Log.trace(F("BLErssi %d" CR), BLErssi);
int txPower = HomePresence["txpower"] | 0;
Expand All @@ -1531,7 +1529,6 @@ void haRoomPresence(JsonObject& HomePresence) {
HomePresence["distance"] = distance;
Log.trace(F("Ble distance %D" CR), distance);
}
# endif

void BTforceScan() {
if (!ProcessLock) {
Expand Down Expand Up @@ -1604,6 +1601,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
bleConnect = (bool)BTdata["bleconnect"];
Log.notice(F("New value bleConnect: %T" CR), bleConnect);
}
if (BTdata.containsKey("lowpowermode")) {
changelowpowermode((int)BTdata["lowpowermode"]);
}
# endif
// MinRSSI set
if (BTdata.containsKey("minrssi")) {
Expand All @@ -1613,11 +1613,14 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
minRssi = abs((int)BTdata["minrssi"]);
Log.notice(F("New minrssi: %d" CR), minRssi);
}
# ifdef ESP32
if (BTdata.containsKey("lowpowermode")) {
changelowpowermode((int)BTdata["lowpowermode"]);
// Home Assistant presence message
if (BTdata.containsKey("hasspresence")) {
// storing Min RSSI for further use if needed
Log.trace(F("Previous hasspresence: %T" CR), hassPresence);
// set Min RSSI if present if not setting default value
hassPresence = (bool)BTdata["hasspresence"];
Log.notice(F("New hasspresence: %T" CR), hassPresence);
}
# endif
}
}
#endif
8 changes: 8 additions & 0 deletions main/ZmqttDiscovery.ino
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,14 @@ void pubMqttDiscovery() {
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", true // device name, device manufacturer, device model, device mac, retain
);
createDiscovery("switch", //set Type
"", "BT: Publish HASS presence", (char*)getUniqueId("hasspresence", "").c_str(), //set state_topic,name,uniqueId
"", "", "", //set availability_topic,device_class,value_template,
"{\"hasspresence\":true}", "{\"hasspresence\":false}", "", //set,payload_on,payload_off,unit_of_meas,
0, //set off_delay
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
"", "", "", "", true // device name, device manufacturer, device model, device mac, retain
);
# ifdef ESP32
createDiscovery("switch", //set Type
"", "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId
Expand Down
5 changes: 4 additions & 1 deletion main/config_BT.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ bool bleConnect = AttemptBLECOnnect;
#ifndef PublishOnlySensors
# define PublishOnlySensors false //false if we publish all BLE devices discovered or true only the identified sensors (like temperature sensors)
#endif
#ifndef HassPresence
# define HassPresence false //false if we publish into Home Assistant presence topic
#endif

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

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

/*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/
// if not commented Home presence integration with HOME ASSISTANT is activated
#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)

enum ble_sensor_model {
Expand Down

0 comments on commit e1aef48

Please sign in to comment.