use same Builder for sensor specific fields

This commit is contained in:
Nicu Hodos 2024-05-16 10:05:24 +02:00
parent 44d14d9a09
commit 6ebe06344c
2 changed files with 17 additions and 2 deletions

View File

@ -18,6 +18,7 @@ struct OilTankRoomSensorBuilder {
->withParent(gatewayDevice);
return Builder<TemperatureSensor>::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();

View File

@ -145,6 +145,21 @@ namespace Ha {
return static_cast<T*>(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 }}";
}
};