Make LWT state message user configurable

Make LWT state message user configurable (#9395)
This commit is contained in:
Theo Arends 2020-09-27 12:51:50 +02:00
parent da6d057cbb
commit 1da31e5f15
5 changed files with 19 additions and 16 deletions

View File

@ -724,7 +724,7 @@ const char S_RSLT_POWER[] PROGMEM = D_RSLT_POWER;
const char S_RSLT_RESULT[] PROGMEM = D_RSLT_RESULT;
const char S_RSLT_WARNING[] PROGMEM = D_RSLT_WARNING;
const char S_LWT[] PROGMEM = D_LWT;
const char S_OFFLINE[] PROGMEM = D_OFFLINE;
const char S_LWT_OFFLINE[] PROGMEM = MQTT_LWT_OFFLINE;
// support.ino
static const char kMonthNames[] = D_MONTH3LIST;

View File

@ -369,8 +369,11 @@
//#define USE_ARDUINO_OTA // Add optional support for Arduino OTA (+13k code)
// -- MQTT ----------------------------------------
#define MQTT_TELE_RETAIN 0 // Tele messages may send retain flag (0 = off, 1 = on)
#define MQTT_CLEAN_SESSION 1 // Mqtt clean session connection (0 = No clean session, 1 = Clean session (default))
#define MQTT_LWT_OFFLINE "Offline" // MQTT LWT offline topic message
#define MQTT_LWT_ONLINE "Online" // MQTT LWT online topic message
#define MQTT_TELE_RETAIN 0 // Tele messages may send retain flag (0 = off, 1 = on)
#define MQTT_CLEAN_SESSION 1 // Mqtt clean session connection (0 = No clean session, 1 = Clean session (default))
// -- MQTT - Domoticz -----------------------------
#define USE_DOMOTICZ // Enable Domoticz (+6k code, +0.3k mem)

View File

@ -494,7 +494,7 @@ void MqttConnected(void)
Mqtt.connect_count++;
GetTopic_P(stopic, TELE, mqtt_topic, S_LWT);
Response_P(PSTR(D_ONLINE));
Response_P(PSTR(MQTT_LWT_ONLINE));
MqttPublish(stopic, true);
if (!Settings.flag4.only_json_message) { // SetOption90 - Disable non-json MQTT response
@ -581,7 +581,7 @@ void MqttReconnect(void)
#if defined(USE_MQTT_TLS) && defined(USE_MQTT_AWS_IOT)
// don't enable MQTT for AWS IoT if Private Key or Certificate are not set
if (Mqtt.mqtt_tls) {
if (0 == strlen(SettingsText(SET_MQTT_PWD))) { // we anticipate that an empty password does not make sense with TLS. This avoids failed connections
if (0 == strlen(SettingsText(SET_MQTT_PWD))) { // we anticipate that an empty password does not make sense with TLS. This avoids failed connections
Mqtt.allowed = false;
}
}
@ -612,7 +612,7 @@ void MqttReconnect(void)
}
GetTopic_P(stopic, TELE, mqtt_topic, S_LWT);
Response_P(S_OFFLINE);
Response_P(S_LWT_OFFLINE);
if (MqttClient.connected()) { MqttClient.disconnect(); }
#ifdef USE_MQTT_TLS
@ -895,8 +895,8 @@ void CmndFullTopic(void)
char stemp1[TOPSZ];
strlcpy(stemp1, (SC_DEFAULT == Shortcut()) ? MQTT_FULLTOPIC : XdrvMailbox.data, sizeof(stemp1));
if (strcmp(stemp1, SettingsText(SET_MQTT_FULLTOPIC))) {
Response_P((Settings.flag.mqtt_offline) ? S_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format
MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic
Response_P((Settings.flag.mqtt_offline) ? S_LWT_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format
MqttPublishPrefixTopic_P(TELE, S_LWT, true); // Offline or remove previous retained topic
SettingsUpdateText(SET_MQTT_FULLTOPIC, stemp1);
restart_flag = 2;
}
@ -999,8 +999,8 @@ void CmndTopic(void)
char stemp1[TOPSZ];
strlcpy(stemp1, (SC_DEFAULT == Shortcut()) ? MQTT_TOPIC : XdrvMailbox.data, sizeof(stemp1));
if (strcmp(stemp1, SettingsText(SET_MQTT_TOPIC))) {
Response_P((Settings.flag.mqtt_offline) ? S_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format
MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic
Response_P((Settings.flag.mqtt_offline) ? S_LWT_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format
MqttPublishPrefixTopic_P(TELE, S_LWT, true); // Offline or remove previous retained topic
SettingsUpdateText(SET_MQTT_TOPIC, stemp1);
restart_flag = 2;
}
@ -1342,7 +1342,7 @@ void MqttSaveSettings(void)
strlcpy(stemp2, (!strlen(tmp)) ? MQTT_FULLTOPIC : tmp, sizeof(stemp2));
MakeValidMqtt(1, stemp2);
if ((strcmp(stemp, SettingsText(SET_MQTT_TOPIC))) || (strcmp(stemp2, SettingsText(SET_MQTT_FULLTOPIC)))) {
Response_P((Settings.flag.mqtt_offline) ? S_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format
Response_P((Settings.flag.mqtt_offline) ? S_LWT_OFFLINE : ""); // SetOption10 - Control MQTT LWT message format
MqttPublishPrefixTopic_P(TELE, S_LWT, true); // Offline or remove previous retained topic
}
SettingsUpdateText(SET_MQTT_TOPIC, stemp);

View File

@ -59,8 +59,8 @@ const char HASS_DISCOVER_SENSOR[] PROGMEM =
const char HASS_DISCOVER_SENSOR_LWT[] PROGMEM =
",\"avty_t\":\"%s\"," // tele/dualr2/LWT
"\"pl_avail\":\"" D_ONLINE "\"," // Online
"\"pl_not_avail\":\"" D_OFFLINE "\""; // Offline
"\"pl_avail\":\"" MQTT_LWT_ONLINE "\"," // Online
"\"pl_not_avail\":\"" MQTT_LWT_OFFLINE "\""; // Offline
const char HASS_DISCOVER_RELAY[] PROGMEM =
",\"cmd_t\":\"%s\"," // cmnd/dualr2/POWER2
@ -184,8 +184,8 @@ const char HASS_DISCOVER_DEVICE[] PROGMEM = // Basic par
"\"hn\":\"%s\"," // Host Name
"\"mac\":\"%s\"," // Full MAC as Device id
"\"md\":\"%s\"," // Module Name
"\"ofln\":\"" D_OFFLINE "\"," // Payload Offline
"\"onln\":\"" D_ONLINE "\"," // Payload Online
"\"ofln\":\"" MQTT_LWT_OFFLINE "\"," // Payload Offline
"\"onln\":\"" MQTT_LWT_ONLINE "\"," // Payload Online
"\"state\":[\"%s\",\"%s\",\"%s\",\"%s\"]," // State text for "OFF","ON","TOGGLE","HOLD"
"\"sw\":\"%s\"," // Software Version
"\"t\":\"%s\"," // Topic

View File

@ -130,7 +130,7 @@ void DeepSleepPrepare(void)
Response_P(PSTR("{\"" D_PRFX_DEEPSLEEP "\":{\"" D_JSON_TIME "\":\"%s\",\"Epoch\":%d}}"), (char*)dt.c_str(), RtcSettings.nextwakeup);
MqttPublishPrefixTopic_P(RESULT_OR_STAT, PSTR(D_CMND_STATUS));
// Response_P(S_OFFLINE);
// Response_P(S_LWT_OFFLINE);
// MqttPublishPrefixTopic_P(TELE, PSTR(D_LWT), true); // Offline or remove previous retained topic
}