Fix discovery fails when using ``%hostname%`` in a topic (#12710)

This commit is contained in:
Theo Arends 2021-07-21 11:41:31 +02:00
parent 4fb8f679d7
commit 520612b67c
4 changed files with 20 additions and 2 deletions

View File

@ -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

View File

@ -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.

View File

@ -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("//"), "/");

View File

@ -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();