Fix subscribe/unsubscribe without reboot

This commit is contained in:
Federico Leoni 2020-08-15 15:23:37 -03:00
parent 6a40222db9
commit dab1ea9dd3
2 changed files with 14 additions and 13 deletions

View File

@ -370,7 +370,7 @@
// -- MQTT - Home Assistant Discovery ------------- // -- MQTT - Home Assistant Discovery -------------
#define USE_HOME_ASSISTANT // Enable Home Assistant Discovery Support (+4.1k code, +6 bytes mem) #define USE_HOME_ASSISTANT // Enable Home Assistant Discovery Support (+4.1k code, +6 bytes mem)
#define HOME_ASSISTANT_DISCOVERY_PREFIX "homeassistant" // Home Assistant discovery prefix #define HOME_ASSISTANT_DISCOVERY_PREFIX "homeassistant" // Home Assistant discovery prefix
#define HOME_ASSISTANT_LWT_TOPIC "homeassistant/status" // homeassistant Birth and Last Will Topic (default = homeassistant/status) #define HOME_ASSISTANT_LWT_TOPIC "homeassistant/status" // home Assistant Birth and Last Will Topic (default = homeassistant/status)
#define HOME_ASSISTANT_LWT_SUBSCRIBE true // Subscribe to Home Assistant Birth and Last Will Topic (default = true) #define HOME_ASSISTANT_LWT_SUBSCRIBE true // Subscribe to Home Assistant Birth and Last Will Topic (default = true)
// -- MQTT - TLS - AWS IoT ------------------------ // -- MQTT - TLS - AWS IoT ------------------------

View File

@ -823,7 +823,7 @@ void HAssAnnounceDeviceInfoAndStatusSensor(void)
MqttPublish(stopic, true); MqttPublish(stopic, true);
if (!Settings.flag.hass_discovery) { if (!Settings.flag.hass_discovery) {
masterlog_level = 0; masterlog_level = 0;
AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_LOG "Home Assistant Discovery disabled")); AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_LOG "Home Assistant Discovery disabled. "));
} }
} }
@ -930,13 +930,21 @@ bool HAssMqttLWT(void)
if (strncasecmp_P(XdrvMailbox.topic, PSTR(HOME_ASSISTANT_LWT_TOPIC), strlen(HOME_ASSISTANT_LWT_TOPIC)) != 0) { if (strncasecmp_P(XdrvMailbox.topic, PSTR(HOME_ASSISTANT_LWT_TOPIC), strlen(HOME_ASSISTANT_LWT_TOPIC)) != 0) {
return false; return false;
} }
if (Settings.flag.hass_discovery && (strncasecmp_P(XdrvMailbox.data, PSTR("online"), strlen("online")) == 0) && (XdrvMailbox.data_len == 6)) {
if ((strncasecmp_P(XdrvMailbox.data, PSTR("online"), strlen("online")) == 0) && (XdrvMailbox.data_len == 6)) {
MqttPublishTeleState(); MqttPublishTeleState();
return true; return true;
} }
} }
void HassLwtSubscribe(bool hasslwt)
{
char htopic[TOPSZ];
snprintf_P(htopic, sizeof(htopic), PSTR(HOME_ASSISTANT_LWT_TOPIC));
if (hasslwt) {
MqttSubscribe(htopic);
} else { MqttUnsubscribe(htopic); }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Interface * Interface
\*********************************************************************************************/ \*********************************************************************************************/
@ -964,7 +972,6 @@ bool Xdrv12(uint8_t function)
if (hass_tele_period >= Settings.tele_period) if (hass_tele_period >= Settings.tele_period)
{ {
hass_tele_period = 0; hass_tele_period = 0;
mqtt_data[0] = '\0'; mqtt_data[0] = '\0';
HAssPublishStatus(); HAssPublishStatus();
} }
@ -981,17 +988,11 @@ bool Xdrv12(uint8_t function)
// AddLog_P2(LOG_LEVEL_INFO, PSTR("MQT: homeassistant/49A3BC/Discovery = {\"dev\":{\"ids\":[\"49A3BC\"]},\"cmd_t\":\"cmnd/test1/\",\"Discovery\":0}")); // AddLog_P2(LOG_LEVEL_INFO, PSTR("MQT: homeassistant/49A3BC/Discovery = {\"dev\":{\"ids\":[\"49A3BC\"]},\"cmd_t\":\"cmnd/test1/\",\"Discovery\":0}"));
// } // }
case FUNC_MQTT_SUBSCRIBE: case FUNC_MQTT_SUBSCRIBE:
if (hasslwt) { HassLwtSubscribe(hasslwt);
char htopic[TOPSZ];
snprintf_P(htopic, sizeof(htopic), PSTR(HOME_ASSISTANT_LWT_TOPIC));
MqttSubscribe(htopic);
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("MQT: subscribed to " HOME_ASSISTANT_LWT_TOPIC));
}
break; break;
case FUNC_MQTT_DATA: case FUNC_MQTT_DATA:
if (hasslwt) { result = HAssMqttLWT(); } result = HAssMqttLWT();
break; break;
} }
} }
return result; return result;