add sensor for wifi signal strength

This commit is contained in:
Nicu Hodos 2025-06-04 17:14:14 +02:00
parent e129097b52
commit 9b1ecf73ce
2 changed files with 21 additions and 1 deletions

View File

@ -1,7 +1,7 @@
{
"$schema": "https://raw.githubusercontent.com/platformio/platformio-core/develop/platformio/assets/schema/library.json",
"name": "ha-mqtt",
"version": "1.6.0",
"version": "1.7.0",
"description": "Home Assistant classes for integration with MQTT auto discovery",
"repository": {
"type": "git",

View File

@ -1,5 +1,6 @@
#pragma once
#include <ESP8266WiFi.h>
#include "TaskScheduler.h"
#include "ha.h"
@ -9,6 +10,7 @@ namespace HaESP {
struct {
bool heapStats = false;
bool restartInfo = false;
bool wifiInfo = false;
} activeSensors;
Task tHeap(5 * TASK_MINUTE, TASK_FOREVER, []() {
@ -28,6 +30,10 @@ namespace HaESP {
Sensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
}, &ts);
Task tWifi(3 * TASK_MINUTE, TASK_FOREVER, []() {
Sensor::mapSensors["wifi_signal_strength"]->updateState(to_string(WiFi.RSSI()).c_str());
}, &ts);
template <class T>
Builder<T>& heapStats(Builder<T>& builder) {
auto heap = new Sensor{ "Heap fragmentation", "heap_fragmentation" };
@ -60,6 +66,19 @@ namespace HaESP {
return builder;
}
template <class T>
Builder<T>& wifiInfo(Builder<T>& builder) {
builder.addDiagnostic(Builder<Sensor>::instance((
new Sensor{ "WiFi Signal (RSSI)", "wifi_signal_strength" }))
.withDeviceClass("signal_strength")
.withUnitMeasure("dBm")
.withPrecision(0)
.build()
);
activeSensors.wifiInfo = true;
return builder;
}
Builder<Button>& restartButton() {
return Builder<Button>::instance(new Button{"Restart", "restart",
[](const char* msg) {
@ -72,5 +91,6 @@ namespace HaESP {
void enableSensors() {
if (activeSensors.heapStats) tHeap.enable();
if (activeSensors.restartInfo) tRestartInfo.enable();
if (activeSensors.wifiInfo) tWifi.enable();
}
}