From f1901a1b5318449fd901d3cbef14d8ae75c6731b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Tue, 8 Oct 2024 20:56:27 +0200 Subject: [PATCH] add support for precondfigured components - heap stats --- src/esp.h | 36 ++++++++++++++++++++++++++++++++++++ src/ha.h | 4 ++++ 2 files changed, 40 insertions(+) create mode 100644 src/esp.h diff --git a/src/esp.h b/src/esp.h new file mode 100644 index 0000000..1a97c90 --- /dev/null +++ b/src/esp.h @@ -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 + Builder& heapStats(Builder& builder) { + builder.addDiagnostic(Builder::instance(new Sensor{ "Heap fragmentation", "heap_fragmentation" }) + .withUnitMeasure("%") + .withPrecision(0) + .build()); + builder.addDiagnostic(Builder::instance(new Sensor{ "Heap free", "heap_free" }) + .withDeviceClass("data_size") + .withUnitMeasure("B") + .withPrecision(0) + .build() + ); + builder.addDiagnostic(Builder::instance(new Sensor{ "Heap max free block", "heap_max_free_block" }) + .withDeviceClass("data_size") + .withUnitMeasure("B") + .withPrecision(0) + .build() + ); + tHeap.enable(); + return builder; + } +} \ No newline at end of file diff --git a/src/ha.h b/src/ha.h index 37ec441..df756bc 100644 --- a/src/ha.h +++ b/src/ha.h @@ -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);