From 01c7eb8479f58f2c12f929a1b65992ac80aa4045 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 2 Oct 2025 22:07:02 +0200 Subject: [PATCH] re-organize state and command topics by removing component type from path --- library.json | 2 +- src/ha.h | 15 ++++++++------- src/mqtt.h | 2 +- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/library.json b/library.json index 3a1aa10..c170cb0 100644 --- a/library.json +++ b/library.json @@ -1,7 +1,7 @@ { "$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json", "name": "ha-mqtt", - "version": "1.11.0", + "version": "1.12.0", "description": "Home Assistant classes for integration with MQTT auto discovery", "repository": { "type": "git", diff --git a/src/ha.h b/src/ha.h index 0aff058..c07fa31 100644 --- a/src/ha.h +++ b/src/ha.h @@ -6,9 +6,10 @@ using namespace std; -#define JSON_SIZE 512 -#define TOPIC_SIZE 255 -#define BASE_TOPIC "homeassistant/%s/%s/%s" +#define JSON_SIZE 512 +#define TOPIC_SIZE 255 +#define CONFIG_TOPIC "homeassistant/%s/" MAIN_DEVICE_ID "/%s" +#define BASE_TOPIC MAIN_DEVICE_ID "/%s" namespace Ha { uint16(*publisher)(const char*, const char*); @@ -133,9 +134,9 @@ namespace Ha { void buildConfigTopic() { if (multiValueComponent && deviceClass) { - snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"_%s""/config", type, MAIN_DEVICE_ID, deviceClass, id); + snprintf(configTopic, sizeof(configTopic), CONFIG_TOPIC"_%s""/config", type, deviceClass, id); } else { - snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"/config", type, MAIN_DEVICE_ID, id); + snprintf(configTopic, sizeof(configTopic), CONFIG_TOPIC"/config", type, id); } } }; @@ -146,7 +147,7 @@ namespace Ha { inline static unordered_map mapCommandIds; Command(Component* cmp, onMessage f) : f(f), cmp(cmp) { - snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", cmp->type, MAIN_DEVICE_ID, cmp->id); + snprintf(commandTopic, sizeof(commandTopic), BASE_TOPIC"/set", cmp->id); mapCommandTopics.insert({ string(commandTopic), this }); mapCommandIds.insert({ string(cmp->id), this }); } @@ -179,7 +180,7 @@ namespace Ha { State(Component* cmp) : cmp(cmp) {} void withStateTopic() { - snprintf(stateTopic, sizeof(stateTopic), BASE_TOPIC"/state", cmp->type, MAIN_DEVICE_ID, cmp->id); + snprintf(stateTopic, sizeof(stateTopic), BASE_TOPIC"/state", cmp->id); } void updateState(const char* message) { diff --git a/src/mqtt.h b/src/mqtt.h index f4424c7..c8131d9 100644 --- a/src/mqtt.h +++ b/src/mqtt.h @@ -5,7 +5,7 @@ #include #include "esp.h" -#define MAIN_TOPIC "homeassistant/+/" MAIN_DEVICE_ID "/#" +#define MAIN_TOPIC MAIN_DEVICE_ID "/#" using namespace Ha;