Skip to content

Commit e576b26

Browse files
authored
[BT] Fix HA discovery for motion and door sensor (#1798)
* [BT] Fix HA discovery for motion sensor * Add status binary sensor and text sensor
1 parent 5a36034 commit e576b26

File tree

4 files changed

+32
-2
lines changed

4 files changed

+32
-2
lines changed

docs/use/ble.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ Note that you can find apps to simulate beacons and do some tests like [Beacon s
6868

6969
iOS version >=10 devices advertise without an extra app MAC address, nevertheless this address [changes randomly](https://github.com/1technophile/OpenMQTTGateway/issues/71) and cannot be used for presence detection. You must install an app to advertise a fixed MAC address.
7070

71+
::: info
72+
The `presenceawaytimer` is also used to reset the state of the PIR/motion sensors to `off` when using HA MQTT discovery convention. If the Sensor does not detect a motion, its state will be automatically set to `off` after the `presenceawaytimer`.
73+
:::
74+
7175
## Receiving signals from BLE devices with accelerometers for movement detection
7276
The gateway is designed to detect BLE trackers from BlueCharm and automatically create a binary sensor entity in accordance with the Home Assistant discovery convention, provided that auto discovery is enabled.
7377

main/ZgatewayBT.ino

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,6 +1030,30 @@ void launchBTDiscovery(bool overrideDiscovery) {
10301030
0, "", "", false, "",
10311031
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
10321032
stateClassMeasurement, nullptr, nullptr, "[\"lb\",\"kg\",\"jin\"]");
1033+
} else if (strcmp(prop.key().c_str(), "pres") == 0 || strcmp(prop.key().c_str(), "movement") == 0) {
1034+
createDiscovery("binary_sensor",
1035+
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),
1036+
will_Topic, "motion", value_template.c_str(), // Idealy we should have the decoder give the name here as "motion" but for now it gives "presence"
1037+
"True", "False", "",
1038+
BTConfig.presenceAwayTimer, "", "", false, "",
1039+
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
1040+
stateClassNone);
1041+
} else if (strcmp(prop.value()["unit"], "string") == 0 && strcmp(prop.key().c_str(), "mac") != 0) {
1042+
createDiscovery("sensor",
1043+
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),
1044+
will_Topic, prop.value()["name"], value_template.c_str(),
1045+
"", "", "",
1046+
0, "", "", false, "",
1047+
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
1048+
stateClassNone);
1049+
} else if (strcmp(prop.value()["unit"], "status") == 0) {
1050+
createDiscovery("binary_sensor",
1051+
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),
1052+
will_Topic, prop.value()["name"], value_template.c_str(),
1053+
"True", "False", "",
1054+
0, "", "", false, "",
1055+
model.c_str(), brand.c_str(), model_id.c_str(), macWOdots.c_str(), false,
1056+
stateClassNone);
10331057
} else if (strcmp(prop.key().c_str(), "device") != 0 && strcmp(prop.key().c_str(), "mac") != 0) { // Exception on device and mac as these ones are not sensors
10341058
createDiscovery("sensor",
10351059
discovery_topic.c_str(), entity_name.c_str(), unique_id.c_str(),

main/config_BT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ struct BTConfig_s {
163163
bool pubAdvData; // Publish advertisement data
164164
bool pubBeaconUuidForTopic; // Use iBeacon UUID as topic, instead of sender (random) MAC address
165165
bool ignoreWBlist; // Disable Whitelist & Blacklist
166-
unsigned long presenceAwayTimer; //Timer that trigger a tracker state as offline if not seen
166+
unsigned long presenceAwayTimer; //Timer that trigger a tracker/PIR state as offline/off if not seen
167167
unsigned long movingTimer; //Timer that trigger a moving sensor state as offline if not seen
168168
};
169169

main/config_mqttDiscovery.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ void announceDeviceTrigger(bool use_gateway_info,
184184
const char* availableHASSClasses[] = {"battery",
185185
"carbon_monoxide",
186186
"carbon_dioxide",
187+
"door",
188+
"enum",
187189
"pm10",
188190
"pm25",
189191
"humidity",
@@ -197,7 +199,7 @@ const char* availableHASSClasses[] = {"battery",
197199
"energy",
198200
"power_factor",
199201
"voltage",
200-
"enum"};
202+
"window"};
201203

202204
// From https://github.com/home-assistant/core/blob/d7ac4bd65379e11461c7ce0893d3533d8d8b8cbf/homeassistant/const.py#L379
203205
// List of units available in Home Assistant

0 commit comments

Comments
 (0)