re-organize state and command topics by removing component type from path

This commit is contained in:
Nicu Hodos 2025-10-02 22:07:02 +02:00
parent 21b96b481c
commit 01c7eb8479
3 changed files with 10 additions and 9 deletions

View File

@ -1,7 +1,7 @@
{ {
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json", "$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "ha-mqtt", "name": "ha-mqtt",
"version": "1.11.0", "version": "1.12.0",
"description": "Home Assistant classes for integration with MQTT auto discovery", "description": "Home Assistant classes for integration with MQTT auto discovery",
"repository": { "repository": {
"type": "git", "type": "git",

View File

@ -6,9 +6,10 @@
using namespace std; using namespace std;
#define JSON_SIZE 512 #define JSON_SIZE 512
#define TOPIC_SIZE 255 #define TOPIC_SIZE 255
#define BASE_TOPIC "homeassistant/%s/%s/%s" #define CONFIG_TOPIC "homeassistant/%s/" MAIN_DEVICE_ID "/%s"
#define BASE_TOPIC MAIN_DEVICE_ID "/%s"
namespace Ha { namespace Ha {
uint16(*publisher)(const char*, const char*); uint16(*publisher)(const char*, const char*);
@ -133,9 +134,9 @@ namespace Ha {
void buildConfigTopic() { void buildConfigTopic() {
if (multiValueComponent && deviceClass) { 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 { } 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<string, Command*> mapCommandIds; inline static unordered_map<string, Command*> mapCommandIds;
Command(Component* cmp, onMessage f) : f(f), cmp(cmp) { 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 }); mapCommandTopics.insert({ string(commandTopic), this });
mapCommandIds.insert({ string(cmp->id), this }); mapCommandIds.insert({ string(cmp->id), this });
} }
@ -179,7 +180,7 @@ namespace Ha {
State(Component* cmp) : cmp(cmp) {} State(Component* cmp) : cmp(cmp) {}
void withStateTopic() { 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) { void updateState(const char* message) {

View File

@ -5,7 +5,7 @@
#include <TaskScheduler.h> #include <TaskScheduler.h>
#include "esp.h" #include "esp.h"
#define MAIN_TOPIC "homeassistant/+/" MAIN_DEVICE_ID "/#" #define MAIN_TOPIC MAIN_DEVICE_ID "/#"
using namespace Ha; using namespace Ha;