#pragma once #include #include "TaskScheduler.h" #include "ha.h" extern Scheduler ts; using namespace Ha; namespace HaESP { struct { bool heapStats = false; bool restartInfo = false; bool wifiInfo = false; } activeSensors; Task tHeap(5 * TASK_MINUTE, TASK_FOREVER, []() { uint32_t hfree; uint32_t hmax; uint8_t hfrag; ESP.getHeapStats(&hfree, &hmax, &hfrag); char value[128]; snprintf_P(value, sizeof(value), PSTR("{\"fragmentation\":%d,\"heap\":{\"Heap free\":\"%d B\",\"Heap max free block\":\"%d B\"}}"), hfrag, hfree, hmax); GenericSensor::mapSensors["heap_fragmentation"]->updateState(value); GenericSensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str()); GenericSensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str()); }, &ts); Task tRestartInfo(TASK_IMMEDIATE, TASK_ONCE, []() { GenericSensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str()); }, &ts); Task tRestart(1 * TASK_SECOND, TASK_ONCE, []() { ESP.restart(); }, &ts); Task tWifi(3 * TASK_MINUTE, TASK_FOREVER, []() { GenericSensor::mapSensors["wifi_signal_strength"]->updateState(to_string(WiFi.RSSI()).c_str()); }, &ts); template Builder& heapStats(Builder& builder) { builder.addDiagnostic(Builder(new Sensor{ "Heap fragmentation", "heap_fragmentation" }) .withUnitMeasure("%") .withPrecision(0) .withValueTemplate("{{ value_json.fragmentation }}") .withJsonAttributes("{{ value_json.heap | tojson }}") .build()); builder.addDiagnostic(Builder(new Sensor{ "Heap free", "heap_free" }) .withDeviceClass("data_size") .withUnitMeasure("B") .withPrecision(0) .build() ); builder.addDiagnostic(Builder(new Sensor{ "Heap max free block", "heap_max_free_block" }) .withDeviceClass("data_size") .withUnitMeasure("B") .withPrecision(0) .build() ); activeSensors.heapStats = true; return builder; } template Builder& restartInfo(Builder& builder) { builder.addDiagnostic((new Sensor{ "Restart reason", "restart_reason" })); activeSensors.restartInfo = true; return builder; } template Builder& wifiInfo(Builder& builder) { builder.addDiagnostic(Builder(( new Sensor{ "WiFi Signal (RSSI)", "wifi_signal_strength" })) .withDeviceClass("signal_strength") .withUnitMeasure("dBm") .withPrecision(0) .build() ); activeSensors.wifiInfo = true; return builder; } Builder