add support for jsonAttributesTemplate and remove overrideConfig
This commit is contained in:
parent
92b5837538
commit
0eec971560
@ -35,8 +35,7 @@ namespace HaESP {
|
|||||||
.withUnitMeasure("%")
|
.withUnitMeasure("%")
|
||||||
.withPrecision(0)
|
.withPrecision(0)
|
||||||
.withValueTemplate("{{ value_json.fragmentation }}")
|
.withValueTemplate("{{ value_json.fragmentation }}")
|
||||||
.overrideConfig("json_attributes_topic", heap->stateTopic)
|
.withJsonAttributes("{{ value_json.heap | tojson }}")
|
||||||
.overrideConfig("json_attributes_template", "{{ value_json.heap | tojson }}")
|
|
||||||
.build());
|
.build());
|
||||||
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
|
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
|
||||||
.withDeviceClass("data_size")
|
.withDeviceClass("data_size")
|
||||||
|
|||||||
46
src/ha.h
46
src/ha.h
@ -70,22 +70,6 @@ namespace Ha {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Component : Config {
|
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* id = nullptr;
|
||||||
const char* type = nullptr;
|
const char* type = nullptr;
|
||||||
@ -94,9 +78,6 @@ namespace Ha {
|
|||||||
const char* entityCategory = nullptr;
|
const char* entityCategory = nullptr;
|
||||||
const char* deviceClass = nullptr;
|
const char* deviceClass = nullptr;
|
||||||
char configTopic[TOPIC_SIZE];
|
char configTopic[TOPIC_SIZE];
|
||||||
JsonPairs<bool> jsonBooleans;
|
|
||||||
JsonPairs<float> jsonNumbers;
|
|
||||||
JsonPairs<const char*> jsonStrings;
|
|
||||||
DeviceConfig* mainDevice = nullptr;
|
DeviceConfig* mainDevice = nullptr;
|
||||||
static List<Component> components;
|
static List<Component> components;
|
||||||
bool multiValueComponent = false;
|
bool multiValueComponent = false;
|
||||||
@ -137,10 +118,6 @@ namespace Ha {
|
|||||||
buildUniqueId();
|
buildUniqueId();
|
||||||
jsonDoc["unique_id"] = (const char*)uniqueId;
|
jsonDoc["unique_id"] = (const char*)uniqueId;
|
||||||
buildConfigTopic();
|
buildConfigTopic();
|
||||||
|
|
||||||
jsonBooleans.addToJson(jsonDoc);
|
|
||||||
jsonNumbers.addToJson(jsonDoc);
|
|
||||||
jsonStrings.addToJson(jsonDoc);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -215,18 +192,8 @@ namespace Ha {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Builder& overrideConfig(const char* key, bool value) {
|
Builder& withJsonAttributes(const char* value) {
|
||||||
cmp->jsonBooleans.add({key, value});
|
cmp->jsonAttributesTemplate = 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});
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -308,6 +275,7 @@ namespace Ha {
|
|||||||
|
|
||||||
struct State : Config {
|
struct State : Config {
|
||||||
char stateTopic[TOPIC_SIZE];
|
char stateTopic[TOPIC_SIZE];
|
||||||
|
const char* jsonAttributesTemplate = nullptr;
|
||||||
|
|
||||||
State(Component* cmp) : cmp(cmp) {}
|
State(Component* cmp) : cmp(cmp) {}
|
||||||
|
|
||||||
@ -321,7 +289,13 @@ namespace Ha {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void buildConfig(JsonDocument& jsonDoc) override {
|
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:
|
private:
|
||||||
Component* cmp;
|
Component* cmp;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user