add support for jsonAttributesTemplate and remove overrideConfig
This commit is contained in:
parent
92b5837538
commit
0eec971560
@ -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<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
|
||||
.withDeviceClass("data_size")
|
||||
|
||||
46
src/ha.h
46
src/ha.h
@ -70,22 +70,6 @@ namespace Ha {
|
||||
};
|
||||
|
||||
struct Component : Config {
|
||||
|
||||
template <typename V>
|
||||
struct JsonPairs {
|
||||
typedef pair<const char*, V> JsonPair;
|
||||
unordered_map<const char*, V> 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<bool> jsonBooleans;
|
||||
JsonPairs<float> jsonNumbers;
|
||||
JsonPairs<const char*> jsonStrings;
|
||||
DeviceConfig* mainDevice = nullptr;
|
||||
static List<Component> 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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user