]> git.basschouten.com Git - openhab-addons.git/commitdiff
[mqtt] set the proper unit in the state description (#13604)
authorCody Cutrer <cody@cutrer.us>
Thu, 27 Oct 2022 06:34:50 +0000 (00:34 -0600)
committerGitHub <noreply@github.com>
Thu, 27 Oct 2022 06:34:50 +0000 (08:34 +0200)
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>
bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/NumberValue.java
bundles/org.openhab.binding.mqtt.homeassistant/src/test/java/org/openhab/binding/mqtt/homeassistant/internal/component/SensorTests.java

index 5b7668bc6ad045582fd7bfae4e387b63d2e7fd14..93047181fc38dc98440e577124fa528761a81920 100644 (file)
@@ -149,6 +149,11 @@ public class NumberValue extends Value {
         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);
     }
 }
index 56f778081e3b251ec8336469d41e330e56a7a96f..8b5e412a608c7992380b44e1a1e1eb2c8d2e89a1 100644 (file)
@@ -75,7 +75,7 @@ public class SensorTests extends AbstractComponentTests {
         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);