From 6ebe06344c1b2529afe6f2a2a72cabda874e9ef6 Mon Sep 17 00:00:00 2001 From: Nicu Hodos Date: Thu, 16 May 2024 10:05:24 +0200 Subject: [PATCH] use same Builder for sensor specific fields --- gateway/include/devices.h | 1 + gateway/include/ha.h | 18 ++++++++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/gateway/include/devices.h b/gateway/include/devices.h index 4a1451f..0a51ba3 100644 --- a/gateway/include/devices.h +++ b/gateway/include/devices.h @@ -18,6 +18,7 @@ struct OilTankRoomSensorBuilder { ->withParent(gatewayDevice); return Builder::instance(id) .asDevice(device) + .withValueTemplate("{{ value_json.sensor.temperature }}") .withDiagnostic(new VoltageSensor{id, "Battery voltage", "{{ value_json.sensor.diagnostic.voltage }}"}) .withDiagnostic(new BatterySensor{id, "Battery level", "{{ ((states('sensor.oil_tank_room_battery_voltage')|float-2.5)|round(2)*100/2)|int }}"}) .build(); diff --git a/gateway/include/ha.h b/gateway/include/ha.h index f5a4685..1897ea7 100644 --- a/gateway/include/ha.h +++ b/gateway/include/ha.h @@ -145,6 +145,21 @@ namespace Ha { return static_cast(cmp); } + Builder& withDeviceClass(const char* value) { + cmp->deviceClass = value; + return *this; + } + + Builder& withUnitMseasure(const char* value) { + cmp->unitMeasure = value; + return *this; + } + + Builder& withValueTemplate(const char* value) { + cmp->valueTemplate = value; + return *this; + } + Builder& withSecondary(Component* c) { c->mainDevice = new DeviceConfig{ cmp->id }; return *this; @@ -255,10 +270,9 @@ namespace Ha { }; struct TemperatureSensor : Sensor { - TemperatureSensor(const char* id) : Sensor("Temperature", id) { + TemperatureSensor(const char* id, const char* name = "Temperature") : Sensor(name, id) { deviceClass = "temperature"; unitMeasure = "°C"; - valueTemplate = "{{ value_json.sensor.temperature }}"; } };