Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HA Discovery] Auto Off timer #1541

Merged
merged 2 commits into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -2355,6 +2359,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