Skip to content

Commit

Permalink
[HA Discovery] Auto Off timer (#1541)
Browse files Browse the repository at this point in the history
* [HA Discovery] Auto Off timer

Auto deactivate discovery after 30min from the last activation, goal is to avoid unwanted entities created
  • Loading branch information
1technophile authored Mar 16, 2023
1 parent 9219ff9 commit 3f92e14
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
5 changes: 4 additions & 1 deletion docs/integrate/home_assistant.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ From Home Assistant site
On OpenMQTTGateway the Home Assistant discovery is enabled by default on all binaries and platformio configurations except for UNO. With Arduino IDE please read the [advanced configuration section](../upload/advanced-configuration#auto-discovery) of the documentation. Here are a few tips for activating discovery on Home Assistant, but for detailed configuration please refer to the Home Assistant website.

Enable discovery on your MQTT integration in HASS.
Enable discovery on your MQTT integration in HASS (activated per default).

![](../img/OpenMQTTGateway-Configuration-Home-Assistant-Discovery-Integration.png)

Expand All @@ -36,6 +36,9 @@ OMG will use the auto discovery functionality of home assistant to create gatewa
::: info
The Bluetooth and the RTL_433 gateway will create automatically devices and entities, the RF gateway will create DeviceTrigger.
The OpenMQTTGateway will also be available as a device to monitor its parameters and control it. The sensors (DHT for example) and actuators (relays) are attached to the gateway.

30 minutes after its activation the auto discovery will be automaticaly deactivated, you can reactivate it from the gateway controls.
Some devices may require a button push or motion/contact event to trigger a message and generate the auto discovery.
:::

## RTL_433 auto discovery specificity
Expand Down
4 changes: 4 additions & 0 deletions main/config_mqttDiscovery.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ void announceDeviceTrigger(bool use_gateway_info,
# define discovery_republish_on_reconnect false
#endif

#ifndef DiscoveryAutoOffTimer
# define DiscoveryAutoOffTimer 1800000 // Timer (ms) that trigger auto discovery to off after activation, the goal is to avoid the discovery of entities that are not expected
#endif

#ifndef GATEWAY_MANUFACTURER
# define GATEWAY_MANUFACTURER "OMG_community"
#endif
Expand Down
6 changes: 6 additions & 0 deletions main/main.ino
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ int failure_number_ntwk = 0; // number of failure connecting to network
int failure_number_mqtt = 0; // number of failure connecting to MQTT
#ifdef ZmqttDiscovery
bool disc = true; // Auto discovery with Home Assistant convention
unsigned long lastDiscovery = 0; // Time of the last discovery to trigger automaticaly to off after DiscoveryAutoOffTimer
#endif
unsigned long timer_led_measures = 0;
static void* eClient = nullptr;
Expand Down Expand Up @@ -1537,6 +1538,9 @@ void loop() {
bool justReconnected = !connected;

#ifdef ZmqttDiscovery
// Deactivate autodiscovery after DiscoveryAutoOffTimer
if (disc && (now > lastDiscovery + DiscoveryAutoOffTimer))
disc = false;
// at first connection we publish the discovery payloads
// or, when we have just re-connected (only when discovery_republish_on_reconnect is enabled)
bool publishDiscovery = disc && (!connectedOnce || (discovery_republish_on_reconnect && justReconnected));
Expand Down Expand Up @@ -2363,6 +2367,8 @@ void MQTTtoSYS(char* topicOri, JsonObject& SYSdata) { // json object decoding
#ifdef ZmqttDiscovery
if (SYSdata.containsKey("discovery")) {
if (SYSdata["discovery"].is<bool>()) {
if (SYSdata["discovery"] == true && disc == false)
lastDiscovery = millis();
disc = SYSdata["discovery"];
stateMeasures();
if (disc)
Expand Down

0 comments on commit 3f92e14

Please sign in to comment.