From 520612b67c8f7c51aadf8697fbbf74740477dc32 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 21 Jul 2021 11:41:31 +0200 Subject: [PATCH] Fix discovery fails when using ``%hostname%`` in a topic (#12710) --- CHANGELOG.md | 4 ++++ RELEASENOTES.md | 2 ++ tasmota/support_tasmota.ino | 10 ++++++++-- tasmota/tasmota.ino | 6 ++++++ 4 files changed, 20 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c9e5995e..0b9267cb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ All notable changes to this project will be documented in this file. ### Changed - Disable PSRAM on unsupported hardware +- Replace spaces by hyphens in final hostname (#12710) + +### Fixed +- Discovery fails when using ``%hostname%`` in a topic (#12710) ## [9.5.0.2] 20210714 ### Added diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 7d293427c..c93f3ae44 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -126,6 +126,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo - Refactor platformio [#12442](https://github.com/arendst/Tasmota/issues/12442) - Allow buttons to work in AP normal mode [#12518](https://github.com/arendst/Tasmota/issues/12518) - Enable Ping and rule features for any device compiled with more than 1M flash size [#12539](https://github.com/arendst/Tasmota/issues/12539) +- Replace spaces by hyphens in final hostname [#12710](https://github.com/arendst/Tasmota/issues/12710) ### Fixed - ESP32 core v2.0.0 setting hostname @@ -142,6 +143,7 @@ The latter links can be used for OTA upgrades too like ``OtaUrl http://ota.tasmo - Exception 28 when unable to send MQTT message and a topic name without a slash '/' [#12555](https://github.com/arendst/Tasmota/issues/12555) - Wi-Fi initial setup workaround for 11n only routers [#12566](https://github.com/arendst/Tasmota/issues/12566) - ESP32 do not use chip temperature sensor as global temperature if external temperature sensor is used [#12630](https://github.com/arendst/Tasmota/issues/12630) +- Discovery fails when using ``%hostname%`` in a topic [#12710](https://github.com/arendst/Tasmota/issues/12710) ### Noted - ESP32 single core **tasmota32solo1.bin** binary can only be uploaded using the GUI as OTA upload will trigger the watchdog timer. Fixed once https://github.com/espressif/arduino-esp32/pull/5426 is merged. \ No newline at end of file diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 621881439..7c74aa479 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -77,6 +77,13 @@ char* GetOtaUrl(char *otaurl, size_t otaurl_size) return otaurl; } +String ResolveToken(const char* input) { + String resolved = input; + resolved.replace(F("%hostname%"), TasmotaGlobal.hostname); + resolved.replace(F("%id%"), NetworkUniqueId()); + return resolved; +} + char* GetTopic_P(char *stopic, uint32_t prefix, char *topic, const char* subtopic) { /* prefix 0 = Cmnd @@ -120,8 +127,7 @@ char* GetTopic_P(char *stopic, uint32_t prefix, char *topic, const char* subtopi fulltopic.replace(FPSTR(MQTT_TOKEN_PREFIX), SettingsText(SET_MQTTPREFIX1 + prefix)); fulltopic.replace(FPSTR(MQTT_TOKEN_TOPIC), (const __FlashStringHelper *)topic); - fulltopic.replace(F("%hostname%"), TasmotaGlobal.hostname); - fulltopic.replace(F("%id%"), NetworkUniqueId()); + fulltopic = ResolveToken(fulltopic.c_str()); } fulltopic.replace(F("#"), ""); fulltopic.replace(F("//"), "/"); diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 1c6e351e1..aeab9e8ed 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -380,6 +380,12 @@ void setup(void) { } else { snprintf_P(TasmotaGlobal.hostname, sizeof(TasmotaGlobal.hostname)-1, SettingsText(SET_HOSTNAME)); } + char *s = TasmotaGlobal.hostname; + while (*s) { + if (' ' == *s) { *s = '_'; } + s++; + } + snprintf_P(TasmotaGlobal.mqtt_topic, sizeof(TasmotaGlobal.mqtt_topic), ResolveToken(TasmotaGlobal.mqtt_topic).c_str()); RtcInit(); GpioInit();