add support for precision in sensors

This commit is contained in:
Nicu Hodos 2024-06-30 13:26:32 +02:00
parent bf8439bdd1
commit 6c78efa3c2
2 changed files with 8 additions and 2 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.0.1",
"version": "1.1.0",
"description": "Home Assistant classes for integration with MQTT auto discovery",
"repository": {
"type": "git",

View File

@ -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<string, Sensor*> 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;
}
};