From c6f51b8ca37f9041750a5c3d2c7bd46ec65daba7 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 10 Jun 2022 10:43:36 +0200 Subject: [PATCH] Fix future compiles using discovery --- .../tasmota_xdrv_driver/xdrv_12_discovery.ino | 13 ++++---- .../xdrv_12_home_assistant.ino | 33 +++++++++++++++++-- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino b/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino index 44c3325c7..377752429 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_12_discovery.ino @@ -22,8 +22,7 @@ /*********************************************************************************************\ * Tasmota discovery * - * A version of xdrv_12_home_assistant supporting the new Tasmota Discovery used by - * latest versions of Home Assistant or TasmoManager. + * Supported by latest versions of Home Assistant (with hatasmota) and TasmoManager. * * SetOption19 0 - [DiscoverOff 0] [Discover 1] Enables discovery (default) * SetOption19 1 - [DiscoverOff 1] [Discover 0] Disables discovery and removes retained message from MQTT server @@ -129,7 +128,7 @@ void TasDiscoverMessage(void) { if (Shutter[i] != 0) { // Check if there are shutters present Relay[i] = 3; // Relay is a shutter } else { - if (i >= light_idx || (iFanMod && (0 == i))) { // First relay on Ifan controls the light + if (i >= light_idx || (iFanMod && (0 == i))) { // First relay on Ifan controls the light Relay[i] = 2; // Relay is a light } else { if (!iFanMod) { // Relays 2-4 for ifan are controlled by FANSPEED and don't need to be present if TasmotaGlobal.module_type = SONOFF_IFAN02 or SONOFF_IFAN03 @@ -228,14 +227,14 @@ void TasDiscovery(void) { TasmotaGlobal.masterlog_level = LOG_LEVEL_DEBUG_MORE; // Hide topic on clean and remove use weblog 4 to show it ResponseClear(); // Clear retained message - if (!Settings->flag.hass_discovery) { // SetOption19 - Clear retained message + if (!Settings->flag.hass_discovery) { // SetOption19 - Clear retained message TasDiscoverMessage(); // Build discovery message } char stopic[TOPSZ]; snprintf_P(stopic, sizeof(stopic), PSTR("tasmota/discovery/%s/config"), NetworkUniqueId().c_str()); MqttPublish(stopic, true); - if (!Settings->flag.hass_discovery) { // SetOption19 - Clear retained message + if (!Settings->flag.hass_discovery) { // SetOption19 - Clear retained message Response_P(PSTR("{\"sn\":")); MqttShowSensor(true); ResponseAppend_P(PSTR(",\"ver\":1}")); @@ -252,7 +251,7 @@ void TasRediscover(void) { void TasDiscoverInit(void) { if (ResetReason() != REASON_DEEP_SLEEP_AWAKE) { - Settings->flag.hass_discovery = 0; // SetOption19 - Enable Tasmota discovery and Disable legacy Hass discovery + Settings->flag.hass_discovery = 0; // SetOption19 - Enable Tasmota discovery and Disable legacy Hass discovery TasmotaGlobal.discovery_counter = 10; // Delayed discovery } } @@ -295,7 +294,7 @@ void CmndTasDiscover(void) { bool Xdrv12(uint8_t function) { bool result = false; - if (Settings->flag.mqtt_enabled) { // SetOption3 - Enable MQTT + if (Settings->flag.mqtt_enabled) { // SetOption3 - Enable MQTT switch (function) { case FUNC_EVERY_SECOND: if (TasmotaGlobal.discovery_counter) { diff --git a/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino b/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino index ffc099e5d..e9e976acb 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_12_home_assistant.ino @@ -1,23 +1,40 @@ /* xdrv_12_home_assistant.ino - home assistant support for Tasmota + Copyright (C) 2021 Erik Montnemery, Federico Leoni and Theo Arends + This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program. If not, see . */ #ifdef USE_HOME_ASSISTANT #undef USE_TASMOTA_DISCOVERY +/*********************************************************************************************\ + * Legacy Home Assistant discovery now replaced by Tasmota Discovery and hatasmota in HASS +\*********************************************************************************************/ #define XDRV_12 12 +#ifndef HOME_ASSISTANT_DISCOVERY_PREFIX +#define HOME_ASSISTANT_DISCOVERY_PREFIX "homeassistant" // Home Assistant discovery prefix +#endif +#ifndef HOME_ASSISTANT_LWT_TOPIC +#define HOME_ASSISTANT_LWT_TOPIC "homeassistant/status" // home Assistant Birth and Last Will Topic (default = homeassistant/status) +#endif +#ifndef HOME_ASSISTANT_LWT_SUBSCRIBE +#define HOME_ASSISTANT_LWT_SUBSCRIBE true // Subscribe to Home Assistant Birth and Last Will Topic (default = true) +#endif + // List of sensors ready for discovery const char kHAssJsonSensorTypes[] PROGMEM = D_JSON_TEMPERATURE "|" D_JSON_DEWPOINT "|" D_JSON_PRESSURE "|" D_JSON_PRESSUREATSEALEVEL "|" @@ -199,7 +216,16 @@ const char kHAssError3[] PROGMEM = uint8_t hass_mode = 0; int hass_tele_period = 0; -// NEW DISCOVERY +/*********************************************************************************************\ + * New discovery direct copy of Tasmota discovery + * + * Supported by latest versions of Home Assistant (with hatasmota) and TasmoManager. + * + * SetOption19 0 - [DiscoverOff 0] [Discover 1] Enables discovery (default) + * SetOption19 1 - [DiscoverOff 1] [Discover 0] Disables discovery and removes retained message from MQTT server + * SetOption73 1 - [DiscoverButton] Enable discovery for buttons + * SetOption114 1 - [DiscoverSwitch] Enable discovery for switches +\*********************************************************************************************/ void HassDiscoverMessage(void) { uint32_t ip_address = (uint32_t)WiFi.localIP(); char* hostname = TasmotaGlobal.hostname; @@ -412,7 +438,10 @@ void NewHAssDiscovery(void) { TasmotaGlobal.masterlog_level = LOG_LEVEL_NONE; // Restore WebLog state } -// NEW DISCOVERY + +/*********************************************************************************************\ + * Legacy discovery +\*********************************************************************************************/ void TryResponseAppend_P(const char *format, ...) { #ifdef MQTT_DATA_STRING