So that other pieces of openhab can know what unit it's going to be,
without it having a value yet. Importantly, any necessary conversion
that need to be applied to the other portion of the state description -
min, max, and step.
See also https://github.com/openhab/openhab-core/pull/3132
Signed-off-by: Cody Cutrer <cody@cutrer.us>
if (min != null) {
builder = builder.withMinimum(min);
}
- return builder.withStep(step).withPattern("%s %unit%");
+ if (!unit.equals(Units.ONE)) {
+ builder.withPattern("%s " + unit);
+ } else {
+ builder.withPattern("%s %unit%");
+ }
+ return builder.withStep(step);
}
}
publishMessage("zigbee2mqtt/sensor/state", "20");
assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(20, Units.WATT));
assertThat(component.getChannel(Sensor.SENSOR_CHANNEL_ID).getState().getCache().createStateDescription(true)
- .build().getPattern(), is("%s %unit%"));
+ .build().getPattern(), is("%s W"));
waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 5000, 200);