uniqueId, configTopic & commandTopic:

- for sensors, use deviceClass as part of uniqueId & configTopic
- use same commandTopic for all Commands - with /set at the end
This commit is contained in:
Nicu Hodos 2024-05-16 10:13:36 +02:00
parent 2d71abfaeb
commit ae95b123ae

View File

@ -72,18 +72,15 @@ namespace Ha {
components.add(this);
}
virtual void buildUniqueId(char* uniqueId) = 0;
virtual void buildUniqueId(char* uniqueId) {
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, id);
}
virtual void buildConfigTopic() {
if (String{"diagnostic"}.equals(entityCategory) && deviceClass) {
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id);
} else {
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
}
sprintf(configTopic, "homeassistant/%s/%s/%s/config", type, MAIN_DEVICE_ID, id);
}
virtual void buildConfig(JsonDocument& jsonDoc) {
buildConfigTopic();
if (mainDevice) mainDevice->buildConfig(jsonDoc);
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
if (deviceClass) jsonDoc["device_class"] = deviceClass;
@ -91,6 +88,7 @@ namespace Ha {
char uniqueId[50];
buildUniqueId(uniqueId);
jsonDoc["unique_id"] = uniqueId;
buildConfigTopic();
}
void publishConfig() {
@ -183,7 +181,7 @@ namespace Ha {
static unordered_map<string, Command*> mapCommands;
Command(const char* name, const char* id, const char* type, onMessage f) : Component(name, id, type), f(f) {
buildCommandTopic();
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
mapCommands.insert({ string(commandTopic), this });
}
@ -191,14 +189,6 @@ namespace Ha {
if (f) f(msg);
}
virtual void buildCommandTopic() {
sprintf(commandTopic, "homeassistant/%s/%s/%s", type, MAIN_DEVICE_ID, id);
}
void buildUniqueId(char* uniqueId) override {
sprintf(uniqueId, "%s_%s", MAIN_DEVICE_ID, this->id);
}
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
jsonDoc["command_topic"] = commandTopic;
@ -227,14 +217,9 @@ namespace Ha {
Switch(const char* name, const char* id, onMessage f = nullptr) : Command(name, id, "switch", f) {}
void buildCommandTopic() override {
sprintf(commandTopic, "homeassistant/%s/%s/%s/set", type, MAIN_DEVICE_ID, id);
}
void buildConfig(JsonDocument& jsonDoc) override {
Command::buildConfig(jsonDoc);
jsonDoc["name"] = nullptr;
jsonDoc["device_class"] = "outlet";
// jsonDoc["retain"] = true;
if (stateTopic[0]) jsonDoc["state_topic"] = stateTopic;
}
@ -257,7 +242,19 @@ namespace Ha {
}
void buildUniqueId(char* uniqueId) override {
sprintf(uniqueId, "%s_%s", deviceClass, id);
if (deviceClass) {
sprintf(uniqueId, "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
} else {
Component::buildUniqueId(uniqueId);
}
}
void buildConfigTopic() override {
if (deviceClass) {
sprintf(configTopic, "homeassistant/%s/%s/%s_%s/config", type, MAIN_DEVICE_ID, deviceClass, id);
} else {
Component::buildConfigTopic();
}
}
void buildConfig(JsonDocument& jsonDoc) override {