From d69a0a59cb774cb23cfda2bf762fdf6fce55052b Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 2 Oct 2025 08:08:12 +0200 Subject: [PATCH] add binary sensor which requires the introduction of GenericSensor class --- library.json | 2 +- src/esp.h | 10 +++++----- src/ha.h | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/library.json b/library.json index 84f9b1b..3a1aa10 100644 --- a/library.json +++ b/library.json @@ -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", diff --git a/src/esp.h b/src/esp.h index 834585e..976785f 100644 --- a/src/esp.h +++ b/src/esp.h @@ -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 diff --git a/src/ha.h b/src/ha.h index 3559738..0aff058 100644 --- a/src/ha.h +++ b/src/ha.h @@ -299,13 +299,10 @@ namespace Ha { } }; - struct Sensor : Component, State { - const char* unitMeasure = nullptr; - unsigned int precision = 2; - SensorStateClass sensorStateClass; - inline static unordered_map mapSensors; + struct GenericSensor : Component, State { + inline static unordered_map 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(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;