From 6a40222db92b41cc090ee74e4c02fe15e5e4fc70 Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Fri, 14 Aug 2020 17:02:55 -0300 Subject: [PATCH 1/2] HAss Discovery LWT --- tasmota/my_user_config.h | 6 ++++-- tasmota/xdrv_12_home_assistant.ino | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index b967db5b0..0500ce716 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -368,8 +368,10 @@ #define DOMOTICZ_OUT_TOPIC "domoticz/out" // Domoticz Output Topic // -- MQTT - Home Assistant Discovery ------------- -#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 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_LWT_TOPIC "homeassistant/status" // homeassistant 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) // -- MQTT - TLS - AWS IoT ------------------------ // Using TLS starting with version v6.5.0.16 compilation will only work using Core 2.4.2 and 2.5.2. No longer supported: 2.3.0 diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index e4d523b20..3a6963fd9 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -823,7 +823,7 @@ void HAssAnnounceDeviceInfoAndStatusSensor(void) MqttPublish(stopic, true); if (!Settings.flag.hass_discovery) { masterlog_level = 0; - AddLog_P2(LOG_LEVEL_INFO, PSTR("LOG: Home Assistant Discovery disabled")); + AddLog_P2(LOG_LEVEL_INFO, PSTR(D_LOG_LOG "Home Assistant Discovery disabled")); } } @@ -925,6 +925,18 @@ void HAssAnyKey(void) MqttPublish(stopic); } +bool HAssMqttLWT(void) +{ + if (strncasecmp_P(XdrvMailbox.topic, PSTR(HOME_ASSISTANT_LWT_TOPIC), strlen(HOME_ASSISTANT_LWT_TOPIC)) != 0) { + return false; + } + + if ((strncasecmp_P(XdrvMailbox.data, PSTR("online"), strlen("online")) == 0) && (XdrvMailbox.data_len == 6)) { + MqttPublishTeleState(); + return true; + } +} + /*********************************************************************************************\ * Interface \*********************************************************************************************/ @@ -932,6 +944,7 @@ void HAssAnyKey(void) bool Xdrv12(uint8_t function) { bool result = false; + bool hasslwt = HOME_ASSISTANT_LWT_SUBSCRIBE; if (Settings.flag.mqtt_enabled) { // SetOption3 - Enable MQTT switch (function) @@ -963,10 +976,22 @@ bool Xdrv12(uint8_t function) case FUNC_MQTT_INIT: hass_mode = 0; // Discovery only if Settings.flag.hass_discovery is set hass_init_step = 2; // Delayed discovery + break; // if (!Settings.flag.hass_discovery) { // AddLog_P2(LOG_LEVEL_INFO, PSTR("MQT: homeassistant/49A3BC/Discovery = {\"dev\":{\"ids\":[\"49A3BC\"]},\"cmd_t\":\"cmnd/test1/\",\"Discovery\":0}")); // } + case FUNC_MQTT_SUBSCRIBE: + if (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; + case FUNC_MQTT_DATA: + if (hasslwt) { result = HAssMqttLWT(); } + break; + } } return result; From dab1ea9dd3bc05a6f931036e60869fe2958b495b Mon Sep 17 00:00:00 2001 From: Federico Leoni Date: Sat, 15 Aug 2020 15:23:37 -0300 Subject: [PATCH 2/2] Fix subscribe/unsubscribe without reboot --- tasmota/my_user_config.h | 2 +- tasmota/xdrv_12_home_assistant.ino | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 0500ce716..774a6dd5b 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -370,7 +370,7 @@ // -- MQTT - Home Assistant Discovery ------------- #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_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) // -- MQTT - TLS - AWS IoT ------------------------ diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index 3a6963fd9..0e016cd6e 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -823,7 +823,7 @@ void HAssAnnounceDeviceInfoAndStatusSensor(void) MqttPublish(stopic, true); if (!Settings.flag.hass_discovery) { 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) { return false; } - - if ((strncasecmp_P(XdrvMailbox.data, PSTR("online"), strlen("online")) == 0) && (XdrvMailbox.data_len == 6)) { + if (Settings.flag.hass_discovery && (strncasecmp_P(XdrvMailbox.data, PSTR("online"), strlen("online")) == 0) && (XdrvMailbox.data_len == 6)) { MqttPublishTeleState(); 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 \*********************************************************************************************/ @@ -964,7 +972,6 @@ bool Xdrv12(uint8_t function) if (hass_tele_period >= Settings.tele_period) { hass_tele_period = 0; - mqtt_data[0] = '\0'; 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}")); // } case FUNC_MQTT_SUBSCRIBE: - if (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)); - } + HassLwtSubscribe(hasslwt); break; case FUNC_MQTT_DATA: - if (hasslwt) { result = HAssMqttLWT(); } + result = HAssMqttLWT(); break; - } } return result;