[Breaking] Deactivate Home Assistant presence per default (#915)
And enable to activate it by MQTT
This commit is contained in:
1 parent
adc5f157cd
commit
e1aef48296
5 files changed
+41
-18
No files matched your search
@@ -168,6 +168,8 @@ sensor:
|
|||||||
|
|
||||||
### MQTT Room Presence
|
### MQTT Room Presence
|
||||||
|
|
||||||
|
The publication into presence topic needs to be activated [here is the command](../use/ble.md)
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
sensor:
|
sensor:
|
||||||
- platform: mqtt_room
|
- platform: mqtt_room
|
||||||
|
|||||||
@@ -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.
|
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
|
## 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
|
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
|
||||||
|
|||||||
+20
-17
@@ -98,8 +98,10 @@ void pubBTMainCore(JsonObject& data, bool haPresenceEnabled = true) {
|
|||||||
pub((char*)mactopic.c_str(), data);
|
pub((char*)mactopic.c_str(), data);
|
||||||
}
|
}
|
||||||
if (haPresenceEnabled && data.containsKey("distance")) {
|
if (haPresenceEnabled && data.containsKey("distance")) {
|
||||||
data.remove("servicedatauuid");
|
if (data.containsKey("servicedatauuid"))
|
||||||
data.remove("servicedata");
|
data.remove("servicedatauuid");
|
||||||
|
if (data.containsKey("servicedata"))
|
||||||
|
data.remove("servicedata");
|
||||||
String topic = String(Base_Topic) + "home_presence/" + String(gateway_name);
|
String topic = String(Base_Topic) + "home_presence/" + String(gateway_name);
|
||||||
pub_custom_topic((char*)topic.c_str(), data, false);
|
pub_custom_topic((char*)topic.c_str(), data, false);
|
||||||
}
|
}
|
||||||
@@ -168,6 +170,7 @@ void emptyBTQueue() {
|
|||||||
JsonBundle& bundle = jsonBTBufferQueue[next % BTQueueSize];
|
JsonBundle& bundle = jsonBTBufferQueue[next % BTQueueSize];
|
||||||
pubBTMainCore(*bundle.object, bundle.haPresence);
|
pubBTMainCore(*bundle.object, bundle.haPresence);
|
||||||
atomic_store_explicit(&jsonBTBufferQueueNext, (next + 1) % (2 * BTQueueSize), ::memory_order_seq_cst); // use namespace std -> ambiguous error...
|
atomic_store_explicit(&jsonBTBufferQueueNext, (next + 1) % (2 * BTQueueSize), ::memory_order_seq_cst); // use namespace std -> ambiguous error...
|
||||||
|
vTaskDelay(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -570,11 +573,9 @@ class MyAdvertisedDeviceCallbacks : public BLEAdvertisedDeviceCallbacks {
|
|||||||
BLEdata.set("rssi", (int)advertisedDevice->getRSSI());
|
BLEdata.set("rssi", (int)advertisedDevice->getRSSI());
|
||||||
if (advertisedDevice->haveTXPower())
|
if (advertisedDevice->haveTXPower())
|
||||||
BLEdata.set("txpower", (int8_t)advertisedDevice->getTXPower());
|
BLEdata.set("txpower", (int8_t)advertisedDevice->getTXPower());
|
||||||
# ifdef subjectHomePresence
|
if (advertisedDevice->haveRSSI() && !publishOnlySensors && hassPresence) {
|
||||||
if (advertisedDevice->haveRSSI() && !publishOnlySensors) {
|
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
|
||||||
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 (advertisedDevice->haveServiceData()) {
|
if (advertisedDevice->haveServiceData()) {
|
||||||
int serviceDataCount = advertisedDevice->getServiceDataCount();
|
int serviceDataCount = advertisedDevice->getServiceDataCount();
|
||||||
Log.trace(F("Get services data number: %d" CR), serviceDataCount);
|
Log.trace(F("Get services data number: %d" CR), serviceDataCount);
|
||||||
@@ -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
|
return false; //if we have at least one white mac and this mac is not white we go out
|
||||||
|
|
||||||
BLEdata.set("rssi", (int)rssi);
|
BLEdata.set("rssi", (int)rssi);
|
||||||
# ifdef subjectHomePresence
|
if (!publishOnlySensors && hassPresence)
|
||||||
if (!publishOnlySensors)
|
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
|
||||||
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
|
|
||||||
Log.trace(F("Service data: %s" CR), restData.c_str());
|
Log.trace(F("Service data: %s" CR), restData.c_str());
|
||||||
BLEdata.set("servicedata", restData.c_str());
|
BLEdata.set("servicedata", restData.c_str());
|
||||||
PublishDeviceData(BLEdata);
|
PublishDeviceData(BLEdata);
|
||||||
@@ -1513,8 +1512,7 @@ JsonObject& process_inode_em(JsonObject& BLEdata) {
|
|||||||
return BLEdata;
|
return BLEdata;
|
||||||
}
|
}
|
||||||
|
|
||||||
# ifdef subjectHomePresence
|
void hass_presence(JsonObject& HomePresence) {
|
||||||
void haRoomPresence(JsonObject& HomePresence) {
|
|
||||||
int BLErssi = HomePresence["rssi"];
|
int BLErssi = HomePresence["rssi"];
|
||||||
Log.trace(F("BLErssi %d" CR), BLErssi);
|
Log.trace(F("BLErssi %d" CR), BLErssi);
|
||||||
int txPower = HomePresence["txpower"] | 0;
|
int txPower = HomePresence["txpower"] | 0;
|
||||||
@@ -1531,7 +1529,6 @@ void haRoomPresence(JsonObject& HomePresence) {
|
|||||||
HomePresence["distance"] = distance;
|
HomePresence["distance"] = distance;
|
||||||
Log.trace(F("Ble distance %D" CR), distance);
|
Log.trace(F("Ble distance %D" CR), distance);
|
||||||
}
|
}
|
||||||
# endif
|
|
||||||
|
|
||||||
void BTforceScan() {
|
void BTforceScan() {
|
||||||
if (!ProcessLock) {
|
if (!ProcessLock) {
|
||||||
@@ -1604,6 +1601,9 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
|
|||||||
bleConnect = (bool)BTdata["bleconnect"];
|
bleConnect = (bool)BTdata["bleconnect"];
|
||||||
Log.notice(F("New value bleConnect: %T" CR), bleConnect);
|
Log.notice(F("New value bleConnect: %T" CR), bleConnect);
|
||||||
}
|
}
|
||||||
|
if (BTdata.containsKey("lowpowermode")) {
|
||||||
|
changelowpowermode((int)BTdata["lowpowermode"]);
|
||||||
|
}
|
||||||
# endif
|
# endif
|
||||||
// MinRSSI set
|
// MinRSSI set
|
||||||
if (BTdata.containsKey("minrssi")) {
|
if (BTdata.containsKey("minrssi")) {
|
||||||
@@ -1613,11 +1613,14 @@ void MQTTtoBT(char* topicOri, JsonObject& BTdata) { // json object decoding
|
|||||||
minRssi = abs((int)BTdata["minrssi"]);
|
minRssi = abs((int)BTdata["minrssi"]);
|
||||||
Log.notice(F("New minrssi: %d" CR), minRssi);
|
Log.notice(F("New minrssi: %d" CR), minRssi);
|
||||||
}
|
}
|
||||||
# ifdef ESP32
|
// Home Assistant presence message
|
||||||
if (BTdata.containsKey("lowpowermode")) {
|
if (BTdata.containsKey("hasspresence")) {
|
||||||
changelowpowermode((int)BTdata["lowpowermode"]);
|
// 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
|
#endif
|
||||||
@@ -700,6 +700,14 @@ void pubMqttDiscovery() {
|
|||||||
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset, //set,payload_avalaible,payload_not avalaible ,is a gateway entity, command topic
|
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
|
"", "", "", "", 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
|
# ifdef ESP32
|
||||||
createDiscovery("switch", //set Type
|
createDiscovery("switch", //set Type
|
||||||
"", "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId
|
"", "SYS: Low Power Mode command", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId
|
||||||
|
|||||||
+4
-1
@@ -71,6 +71,9 @@ bool bleConnect = AttemptBLECOnnect;
|
|||||||
#ifndef PublishOnlySensors
|
#ifndef PublishOnlySensors
|
||||||
# define PublishOnlySensors false //false if we publish all BLE devices discovered or true only the identified sensors (like temperature sensors)
|
# define PublishOnlySensors false //false if we publish all BLE devices discovered or true only the identified sensors (like temperature sensors)
|
||||||
#endif
|
#endif
|
||||||
|
#ifndef HassPresence
|
||||||
|
# define HassPresence false //false if we publish into Home Assistant presence topic
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef BTQueueSize
|
#ifndef BTQueueSize
|
||||||
# define BTQueueSize 4 // lockless queue size for multi core cases (ESP32 currently)
|
# define BTQueueSize 4 // lockless queue size for multi core cases (ESP32 currently)
|
||||||
@@ -92,6 +95,7 @@ unsigned int BLEinterval = TimeBtwRead; //time between 2 scans
|
|||||||
unsigned int BLEscanBeforeConnect = ScanBeforeConnect; //Number of BLE scans between connection cycles
|
unsigned int BLEscanBeforeConnect = ScanBeforeConnect; //Number of BLE scans between connection cycles
|
||||||
unsigned long scanCount = 0;
|
unsigned long scanCount = 0;
|
||||||
bool publishOnlySensors = PublishOnlySensors;
|
bool publishOnlySensors = PublishOnlySensors;
|
||||||
|
bool hassPresence = HassPresence;
|
||||||
|
|
||||||
#ifndef pubKnownBLEServiceData
|
#ifndef pubKnownBLEServiceData
|
||||||
# define pubKnownBLEServiceData false // define true if you want to publish service data belonging to recognised sensors
|
# define pubKnownBLEServiceData false // define true if you want to publish service data belonging to recognised sensors
|
||||||
@@ -110,7 +114,6 @@ bool publishOnlySensors = PublishOnlySensors;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
/*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/
|
/*-------------------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)
|
#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 {
|
enum ble_sensor_model {
|
||||||
|
|||||||
Reference in new issue
Block a user