From 6c78efa3c2fb143c0a3f4f61f36063d73836c1dc Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Sun, 30 Jun 2024 13:26:32 +0200 Subject: [PATCH] add support for precision in sensors --- library.json | 2 +- src/ha.h | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/library.json b/library.json index 555653d..d4a8930 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.0.1", + "version": "1.1.0", "description": "Home Assistant classes for integration with MQTT auto discovery", "repository": { "type": "git", diff --git a/src/ha.h b/src/ha.h index 714a3f8..74ccfa6 100644 --- a/src/ha.h +++ b/src/ha.h @@ -162,6 +162,11 @@ namespace Ha { return *this; } + Builder& withPrecision(unsigned int value) { + cmp->precision = value; + return *this; + } + Builder& addSecondary(Component* c) { if (cmp->mainDevice) c->mainDevice = &DeviceConfig::create(cmp->mainDevice->id); return *this; @@ -294,6 +299,7 @@ namespace Ha { struct Sensor : Component, StateConfig { const char* unitMeasure = nullptr; const char* valueTemplate = nullptr; + unsigned int precision = 2; static unordered_map mapSensors; Sensor(const char* name, const char* id) : Component(name, id, "sensor"), StateConfig(this) { @@ -322,7 +328,7 @@ namespace Ha { StateConfig::buildConfig(jsonDoc); if (unitMeasure) jsonDoc["unit_of_measurement"] = unitMeasure; if (valueTemplate) jsonDoc["value_template"] = valueTemplate; - jsonDoc["suggested_display_precision"] = 2; + jsonDoc["suggested_display_precision"] = precision; } };