[HA Discovery] Auto Off timer (#1541)

* [HA Discovery] Auto Off timer

Auto deactivate discovery after 30min from the last activation, goal is to avoid unwanted entities created
This commit is contained in:
Florian authored and GitHub committed 2023-03-16 10:43:18 -05:00
1 parent 9219ff928c
commit 3f92e14ba6
3 files changed
+14 -1

No files matched your search

+4
View File
@@ -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
+6
View File
@@ -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;
@@ -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));
@@ -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)