add binary sensor which requires the introduction of GenericSensor class

This commit is contained in:
Nicu Hodos 2025-10-02 08:08:12 +02:00
parent 259b001d5e
commit d69a0a59cb
3 changed files with 37 additions and 12 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.10.1",
"version": "1.11.0",
"description": "Home Assistant classes for integration with MQTT auto discovery",
"repository": {
"type": "git",

View File

@ -23,13 +23,13 @@ namespace HaESP {
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);
Sensor::mapSensors["heap_fragmentation"]->updateState(value);
Sensor::mapSensors["heap_free"]->updateState(to_string(hfree).c_str());
Sensor::mapSensors["heap_max_free_block"]->updateState(to_string(hmax).c_str());
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, []() {
Sensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
GenericSensor::mapSensors["restart_reason"]->updateState(ESP.getResetReason().c_str());
}, &ts);
Task tRestart(1 * TASK_SECOND, TASK_ONCE, []() {
@ -37,7 +37,7 @@ namespace HaESP {
}, &ts);
Task tWifi(3 * TASK_MINUTE, TASK_FOREVER, []() {
Sensor::mapSensors["wifi_signal_strength"]->updateState(to_string(WiFi.RSSI()).c_str());
GenericSensor::mapSensors["wifi_signal_strength"]->updateState(to_string(WiFi.RSSI()).c_str());
}, &ts);
template <class T>

View File

@ -299,13 +299,10 @@ namespace Ha {
}
};
struct Sensor : Component, State {
const char* unitMeasure = nullptr;
unsigned int precision = 2;
SensorStateClass sensorStateClass;
inline static unordered_map<string, Sensor*> mapSensors;
struct GenericSensor : Component, State {
inline static unordered_map<string, GenericSensor*> mapSensors;
Sensor(const char* name, const char* id) : Component(id, name, "sensor"), State(this) {
GenericSensor(const char* id, const char* name, const char* type) : Component(id, name, type), State(this) {
withStateTopic();
mapSensors.insert({ string(id), this });
}
@ -313,6 +310,29 @@ namespace Ha {
void buildConfig(JsonDocument& jsonDoc) override {
Component::buildConfig(jsonDoc);
State::buildConfig(jsonDoc);
}
};
struct BinarySensor : GenericSensor {
unsigned int off_delay_seconds = 0;
BinarySensor(const char* id, const char* name = nullptr) : GenericSensor(id, name, "binary_sensor") {}
void buildConfig(JsonDocument& jsonDoc) override {
GenericSensor::buildConfig(jsonDoc);
if (off_delay_seconds) jsonDoc["off_delay"] = off_delay_seconds;
}
};
struct Sensor : GenericSensor {
const char* unitMeasure = nullptr;
unsigned int precision = 2;
SensorStateClass sensorStateClass;
Sensor(const char* name, const char* id) : GenericSensor(id, name, "sensor") {}
void buildConfig(JsonDocument& jsonDoc) override {
GenericSensor::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);
@ -445,6 +465,11 @@ namespace Ha {
return *this;
}
Builder& withOffDelaySeconds(unsigned int value) {
cmp->off_delay_seconds = value;
return *this;
}
Builder& withPattern(const char* value) {
cmp->pattern = value;
return *this;