From 3ef47dc87b3b4b428648aa8494532f65d01051ba Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Thu, 27 Oct 2022 00:34:50 -0600 Subject: [PATCH] [mqtt] set the proper unit in the state description (#13604) 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 --- .../openhab/binding/mqtt/generic/values/NumberValue.java | 7 ++++++- .../mqtt/homeassistant/internal/component/SensorTests.java | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java index 5b7668bc6ad04..93047181fc38d 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java @@ -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); } } diff --git a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SensorTests.java b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SensorTests.java index 56f778081e3b2..8b5e412a608c7 100644 --- a/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SensorTests.java +++ b/bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SensorTests.java @@ -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);