Skip to content

Commit

Permalink
[mqtt] set the proper unit in the state description (openhab#13604)
Browse files Browse the repository at this point in the history
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 openhab/openhab-core#3132

Signed-off-by: Cody Cutrer <cody@cutrer.us>
Signed-off-by: Andras Uhrin <andras.uhrin@gmail.com>
  • Loading branch information
ccutrer authored and andrasU committed Nov 12, 2022
1 parent 9122018 commit 0065360
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ public StateDescriptionFragmentBuilder createStateDescription(boolean readOnly)
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void test() throws InterruptedException {
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);

Expand Down

0 comments on commit 0065360

Please sign in to comment.