Skip to content

Commit

Permalink
Fix discovery fails when using %hostname% in a topic (arendst#12710)
Browse files Browse the repository at this point in the history
  • Loading branch information
arendst committed Jul 21, 2021
1 parent 4fb8f67 commit 520612b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
10 changes: 8 additions & 2 deletions tasmota/support_tasmota.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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("//"), "/");
Expand Down
6 changes: 6 additions & 0 deletions tasmota/tasmota.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 520612b

Please sign in to comment.