Skip to content

Commit

Permalink
Added config option for HA autodiscovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
blazoncek committed Jan 22, 2022
1 parent fe6b1c1 commit 26ae6d3
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion usermods/multi_relay/usermod_multi_relay.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class MultiRelay : public Usermod {
// status of initialisation
bool initDone = false;

bool HAautodiscovery = false;

uint16_t periodicBroadcastSec = 60;
unsigned long lastBroadcast = 0;

Expand All @@ -57,6 +59,7 @@ class MultiRelay : public Usermod {
static const char _external[];
static const char _button[];
static const char _broadcast[];
static const char _HAautodiscovery[];

void publishMqtt(int relay) {
//Check if MQTT Connected, otherwise it will crash the 8266
Expand Down Expand Up @@ -259,7 +262,7 @@ class MultiRelay : public Usermod {
strcpy(subuf, mqttDeviceTopic);
strcat_P(subuf, PSTR("/relay/#"));
mqtt->subscribe(subuf, 0);
publishHomeAssistantAutodiscovery();
if (HAautodiscovery) publishHomeAssistantAutodiscovery();
for (uint8_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
if (_relay[i].pin<0) continue;
publishMqtt(i); //publish current state
Expand Down Expand Up @@ -547,6 +550,7 @@ class MultiRelay : public Usermod {
relay[FPSTR(_external)] = _relay[i].external;
relay[FPSTR(_button)] = _relay[i].button;
}
top[FPSTR(_HAautodiscovery)] = HAautodiscovery;
DEBUG_PRINTLN(F("MultiRelay config saved."));
}

Expand All @@ -569,6 +573,7 @@ class MultiRelay : public Usermod {
enabled = top[FPSTR(_enabled)] | enabled;
periodicBroadcastSec = top[FPSTR(_broadcast)] | periodicBroadcastSec;
periodicBroadcastSec = min(900,max(0,(int)periodicBroadcastSec));
HAautodiscovery = top[FPSTR(_HAautodiscovery)] | HAautodiscovery;

for (uint8_t i=0; i<MULTI_RELAY_MAX_RELAYS; i++) {
String parName = FPSTR(_relay_str); parName += '-'; parName += i;
Expand Down Expand Up @@ -636,3 +641,4 @@ const char MultiRelay::_activeHigh[] PROGMEM = "active-high";
const char MultiRelay::_external[] PROGMEM = "external";
const char MultiRelay::_button[] PROGMEM = "button";
const char MultiRelay::_broadcast[] PROGMEM = "broadcast-sec";
const char MultiRelay::_HAautodiscovery[] PROGMEM = "HA-autodiscovery";

0 comments on commit 26ae6d3

Please sign in to comment.