Merge branch 'v1.7.0'

This commit is contained in:
Nicu Hodos 2025-06-20 11:30:17 +02:00
commit a717ca9611
4 changed files with 62 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",

30
src/enums.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
class EnumClass {
const char* value = nullptr;
public:
EnumClass() {}
EnumClass(const char* v) {
value = v;
}
void operator=(const char* v) {
value = v;
}
operator const char*() {
return value;
}
operator bool() {
return value != nullptr;
}
};
namespace Ha {
struct SensorStateClass : EnumClass {
SensorStateClass() : EnumClass() {}
SensorStateClass(const char* value) : EnumClass(value) {}
} MEASUREMENT{ "measurement" }, MEASUREMENT_ANGLE{ "measurement_angle" }, TOTAL{ "total" }, TOTAL_INCREASING{ "total_increasing" };
}

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();
}
}

View File

@ -2,6 +2,7 @@
#include <ArduinoJson.h>
#include "list.h"
#include "enums.h"
using namespace std;
@ -289,6 +290,7 @@ namespace Ha {
struct Sensor : Component, State {
const char* unitMeasure = nullptr;
unsigned int precision = 2;
SensorStateClass sensorStateClass;
inline static unordered_map<string, Sensor*> mapSensors;
Sensor(const char* name, const char* id) : Component(id, name, "sensor"), State(this) {
@ -301,6 +303,7 @@ namespace Ha {
State::buildConfig(jsonDoc);
if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure;
if (isNumericSensor()) jsonDoc["suggested_display_precision"] = precision;
if (sensorStateClass) jsonDoc["state_class"] = static_cast<const char*>(sensorStateClass);
}
private:
@ -313,6 +316,7 @@ namespace Ha {
TemperatureSensor(const char* id, const char* name = "Temperature") : Sensor(name, id) {
deviceClass = "temperature";
unitMeasure = "°C";
sensorStateClass = MEASUREMENT;
}
};
@ -320,6 +324,7 @@ namespace Ha {
HumiditySensor(const char* id, const char* name = "Humidity") : Sensor(name, id) {
deviceClass = "humidity";
unitMeasure = "%";
sensorStateClass = MEASUREMENT;
}
};
@ -343,6 +348,7 @@ namespace Ha {
this->valueTemplate = valueTemplate;
deviceClass = "battery";
unitMeasure = "%";
sensorStateClass = MEASUREMENT;
}
};
@ -407,6 +413,11 @@ namespace Ha {
return *this;
}
Builder& withSensorStateClass(SensorStateClass value) {
cmp->sensorStateClass = value;
return *this;
}
Builder& withMin(unsigned int value) {
cmp->min = value;
return *this;