From 6887224c29dca4058bbc94d7be3a021d06fb9fad Mon Sep 17 00:00:00 2001 From: Jimmy Tanagra Date: Thu, 26 Dec 2024 00:29:29 +1000 Subject: [PATCH] [mqtt.generic] Fix ClassCastException when receiving ON/OFF on a dimmer channel Signed-off-by: Jimmy Tanagra --- .../openhab/binding/mqtt/generic/values/PercentageValue.java | 2 +- .../org/openhab/binding/mqtt/generic/values/ValueTests.java | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java index 6a521fbe54752..eb4bfb280be6e 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/main/java/org/openhab/binding/mqtt/generic/values/PercentageValue.java @@ -75,7 +75,7 @@ public PercentageValue(@Nullable BigDecimal min, @Nullable BigDecimal max, @Null @Override public Command parseCommand(Command command) throws IllegalArgumentException { - PercentType oldvalue = (state instanceof UnDefType) ? new PercentType() : (PercentType) state; + PercentType oldvalue = (state instanceof UnDefType) ? new PercentType() : state.as(PercentType.class); // Nothing do to -> We have received a percentage if (command instanceof PercentType percent) { return percent; diff --git a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java index b602e16897c4a..c79a0c8834abb 100644 --- a/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java +++ b/bundles/org.openhab.binding.mqtt.generic/src/test/java/org/openhab/binding/mqtt/generic/values/ValueTests.java @@ -313,6 +313,8 @@ public void percentCalc() { assertThat(v.parseCommand(new DecimalType(10.0)), is(PercentType.ZERO)); assertThat(v.getMQTTpublishValue(PercentType.ZERO, null), is("10")); + v.update(OnOffType.OFF); + assertThat(v.parseCommand(OnOffType.ON), is(OnOffType.ON)); assertThat(v.getMQTTpublishValue(OnOffType.ON, null), is("110")); assertThat(v.parseCommand(OnOffType.OFF), is(OnOffType.OFF));