add support for precondfigured components - heap stats

This commit is contained in:
Nicu Hodos 2024-10-08 20:56:27 +02:00
parent 7327533b24
commit f1901a1b53
2 changed files with 40 additions and 0 deletions

36
src/esp.h Normal file
View File

@ -0,0 +1,36 @@
#pragma once
#include "TaskScheduler.h"
#include "ha.h"
using namespace Ha;
namespace HaESP {
Task tHeap(1 * TASK_MINUTE, TASK_FOREVER, []() {
Sensor::mapSensors["heap_fragmentation"]->updateState(to_string(ESP.getHeapFragmentation()).c_str());
Sensor::mapSensors["heap_free"]->updateState(to_string(ESP.getFreeHeap()).c_str());
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(ESP.getMaxFreeBlockSize()).c_str());
}, &ts);
template <class T>
Builder<T>& heapStats(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap fragmentation", "heap_fragmentation" })
.withUnitMeasure("%")
.withPrecision(0)
.build());
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap free", "heap_free" })
.withDeviceClass("data_size")
.withUnitMeasure("B")
.withPrecision(0)
.build()
);
builder.addDiagnostic(Builder<Sensor>::instance(new Sensor{ "Heap max free block", "heap_max_free_block" })
.withDeviceClass("data_size")
.withUnitMeasure("B")
.withPrecision(0)
.build()
);
tHeap.enable();
return builder;
}
}

View File

@ -223,6 +223,10 @@ namespace Ha {
return addSecondary(c);
}
Builder& addPreconfigured(Builder& (*factoryBuilder)(Builder& builder)) {
return factoryBuilder(*this);
}
Builder& addConfiguration(Component* c) {
c->entityCategory = "config";
return addSecondary(c);