simplify: move building of configTopic and uniqueId inside component
This commit is contained in:
parent
0eec971560
commit
9930171d58
51
src/ha.h
51
src/ha.h
@ -77,7 +77,6 @@ namespace Ha {
|
|||||||
const char* name = nullptr;
|
const char* name = nullptr;
|
||||||
const char* entityCategory = nullptr;
|
const char* entityCategory = nullptr;
|
||||||
const char* deviceClass = nullptr;
|
const char* deviceClass = nullptr;
|
||||||
char configTopic[TOPIC_SIZE];
|
|
||||||
DeviceConfig* mainDevice = nullptr;
|
DeviceConfig* mainDevice = nullptr;
|
||||||
static List<Component> components;
|
static List<Component> components;
|
||||||
bool multiValueComponent = false;
|
bool multiValueComponent = false;
|
||||||
@ -101,24 +100,35 @@ namespace Ha {
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
char uniqueId[50];
|
|
||||||
virtual void buildUniqueId() {
|
|
||||||
snprintf(uniqueId, sizeof(uniqueId), "%s_%s", MAIN_DEVICE_ID, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual void buildConfigTopic() {
|
|
||||||
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"/config", type, MAIN_DEVICE_ID, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
void buildConfig(JsonDocument& jsonDoc) override {
|
||||||
if (mainDevice) mainDevice->buildConfig(jsonDoc);
|
if (mainDevice) mainDevice->buildConfig(jsonDoc);
|
||||||
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
if (entityCategory) jsonDoc["entity_category"] = entityCategory;
|
||||||
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
if (deviceClass) jsonDoc["device_class"] = deviceClass;
|
||||||
jsonDoc["name"] = name;
|
jsonDoc["name"] = name;
|
||||||
buildUniqueId();
|
|
||||||
jsonDoc["unique_id"] = (const char*)uniqueId;
|
buildUniqueId(jsonDoc);
|
||||||
buildConfigTopic();
|
buildConfigTopic();
|
||||||
}
|
}
|
||||||
|
private:
|
||||||
|
char configTopic[TOPIC_SIZE];
|
||||||
|
|
||||||
|
void buildUniqueId(JsonDocument& jsonDoc) {
|
||||||
|
char uniqueId[50];
|
||||||
|
if (multiValueComponent && deviceClass) {
|
||||||
|
snprintf(uniqueId, sizeof(uniqueId), "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
|
||||||
|
} else {
|
||||||
|
snprintf(uniqueId, sizeof(uniqueId), "%s_%s", MAIN_DEVICE_ID, id);
|
||||||
|
}
|
||||||
|
jsonDoc["unique_id"] = uniqueId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void buildConfigTopic() {
|
||||||
|
if (multiValueComponent && deviceClass) {
|
||||||
|
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"_%s""/config", type, MAIN_DEVICE_ID, deviceClass, id);
|
||||||
|
} else {
|
||||||
|
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"/config", type, MAIN_DEVICE_ID, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct AbstractBuilder {
|
struct AbstractBuilder {
|
||||||
@ -368,23 +378,6 @@ namespace Ha {
|
|||||||
if (isNumericSensor()) jsonDoc["suggested_display_precision"] = precision;
|
if (isNumericSensor()) jsonDoc["suggested_display_precision"] = precision;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
|
||||||
void buildUniqueId() override {
|
|
||||||
if (multiValueComponent && deviceClass) {
|
|
||||||
snprintf(uniqueId, sizeof(uniqueId), "%s_%s_%s", MAIN_DEVICE_ID, deviceClass, id);
|
|
||||||
} else {
|
|
||||||
Component::buildUniqueId();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void buildConfigTopic() override {
|
|
||||||
if (multiValueComponent && deviceClass) {
|
|
||||||
snprintf(configTopic, sizeof(configTopic), BASE_TOPIC"_%s""/config", type, MAIN_DEVICE_ID, deviceClass, id);
|
|
||||||
} else {
|
|
||||||
Component::buildConfigTopic();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool isNumericSensor() {
|
bool isNumericSensor() {
|
||||||
return deviceClass || unitMeasure;
|
return deviceClass || unitMeasure;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user