From 0eec971560bb1a36c1fd3cc2a584337205fcc481 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Mon, 28 Oct 2024 12:13:42 +0100 Subject: [PATCH] add support for jsonAttributesTemplate and remove overrideConfig --- src/esp.h | 3 +-- src/ha.h | 46 ++++++++++------------------------------------ 2 files changed, 11 insertions(+), 38 deletions(-) diff --git a/src/esp.h b/src/esp.h index cfa0034..5cfb8a7 100644 --- a/src/esp.h +++ b/src/esp.h @@ -35,8 +35,7 @@ namespace HaESP { .withUnitMeasure("%") .withPrecision(0) .withValueTemplate("{{ value_json.fragmentation }}") - .overrideConfig("json_attributes_topic", heap->stateTopic) - .overrideConfig("json_attributes_template", "{{ value_json.heap | tojson }}") + .withJsonAttributes("{{ value_json.heap | tojson }}") .build()); builder.addDiagnostic(Builder::instance(new Sensor{ "Heap free", "heap_free" }) .withDeviceClass("data_size") diff --git a/src/ha.h b/src/ha.h index 216559f..ef4c2d7 100644 --- a/src/ha.h +++ b/src/ha.h @@ -70,22 +70,6 @@ namespace Ha { }; struct Component : Config { - - template - struct JsonPairs { - typedef pair JsonPair; - unordered_map jsonPairs; - - void add(JsonPair pair) { - jsonPairs.insert(pair); - } - - void addToJson(JsonDocument& jsonDoc) { - for (JsonPair el : jsonPairs) { - jsonDoc[el.first] = el.second; - } - } - }; const char* id = nullptr; const char* type = nullptr; @@ -94,9 +78,6 @@ namespace Ha { const char* entityCategory = nullptr; const char* deviceClass = nullptr; char configTopic[TOPIC_SIZE]; - JsonPairs jsonBooleans; - JsonPairs jsonNumbers; - JsonPairs jsonStrings; DeviceConfig* mainDevice = nullptr; static List components; bool multiValueComponent = false; @@ -137,10 +118,6 @@ namespace Ha { buildUniqueId(); jsonDoc["unique_id"] = (const char*)uniqueId; buildConfigTopic(); - - jsonBooleans.addToJson(jsonDoc); - jsonNumbers.addToJson(jsonDoc); - jsonStrings.addToJson(jsonDoc); } }; @@ -215,18 +192,8 @@ namespace Ha { return *this; } - Builder& overrideConfig(const char* key, bool value) { - cmp->jsonBooleans.add({key, value}); - return *this; - } - - Builder& overrideConfig(const char* key, float value) { - cmp->jsonNumbers.add({key, value}); - return *this; - } - - Builder& overrideConfig(const char* key, const char* value) { - cmp->jsonStrings.add({key, value}); + Builder& withJsonAttributes(const char* value) { + cmp->jsonAttributesTemplate = value; return *this; } @@ -308,6 +275,7 @@ namespace Ha { struct State : Config { char stateTopic[TOPIC_SIZE]; + const char* jsonAttributesTemplate = nullptr; State(Component* cmp) : cmp(cmp) {} @@ -321,7 +289,13 @@ namespace Ha { protected: void buildConfig(JsonDocument& jsonDoc) override { - if (stateTopic[0]) jsonDoc["state_topic"] = (const char*)stateTopic; + if (stateTopic[0]) { + jsonDoc["state_topic"] = (const char*)stateTopic; + if (jsonAttributesTemplate) { + jsonDoc["json_attributes_template"] = jsonAttributesTemplate; + jsonDoc["json_attributes_topic"] = (const char*)stateTopic; + } + } } private: Component* cmp;