diff --git a/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/handler/TuyaDeviceHandler.java b/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/handler/TuyaDeviceHandler.java index 5d4f39bfc4..63aa22417f 100644 --- a/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/handler/TuyaDeviceHandler.java +++ b/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/handler/TuyaDeviceHandler.java @@ -56,6 +56,7 @@ import org.openhab.core.types.Command; import org.openhab.core.types.CommandOption; import org.openhab.core.types.State; +import org.openhab.core.types.UnDefType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.smarthomej.binding.tuya.internal.config.ChannelConfiguration; @@ -165,7 +166,14 @@ private void processChannelStatus(Integer dp, Object value) { if (value instanceof String && CHANNEL_TYPE_UID_COLOR.equals(channelTypeUID)) { oldColorMode = ((String) value).length() == 14; - updateState(channelId, ConversionUtil.hexColorDecode((String) value)); + HSBType hsb = ConversionUtil.hexColorDecode((String) value); + if (hsb == null) { + logger.debug("Unknown color format value '{}', updating channel '{}' of thing '{}' as undefined.", + value, channelId, thing.getUID()); + updateState(channelId, UnDefType.UNDEF); + } else { + updateState(channelId, hsb); + } return; } else if (value instanceof String && CHANNEL_TYPE_UID_STRING.equals(channelTypeUID)) { updateState(channelId, new StringType((String) value)); diff --git a/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/util/ConversionUtil.java b/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/util/ConversionUtil.java index 296c07b689..6a08be9c33 100644 --- a/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/util/ConversionUtil.java +++ b/bundles/org.smarthomej.binding.tuya/src/main/java/org/smarthomej/binding/tuya/internal/util/ConversionUtil.java @@ -15,6 +15,7 @@ import java.math.BigDecimal; import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.openhab.core.library.types.DecimalType; import org.openhab.core.library.types.HSBType; import org.openhab.core.library.types.PercentType; @@ -35,30 +36,33 @@ private ConversionUtil() { * Convert a Tuya color string in hexadecimal notation to {@link HSBType} * * @param hexColor the input string - * @return the corresponding state + * @return the corresponding state or null if unknown color format */ - public static HSBType hexColorDecode(String hexColor) { - if (hexColor.length() == 12) { - // 2 bytes H: 0-360, 2 bytes each S,B, 0-1000 - double h = Integer.parseInt(hexColor.substring(0, 4), 16); - double s = Integer.parseInt(hexColor.substring(4, 8), 16) / 10.0; - double b = Integer.parseInt(hexColor.substring(8, 12), 16) / 10.0; - if (h == 360) { - h = 0; - } + public static @Nullable HSBType hexColorDecode(String hexColor) { + try { + if (hexColor.length() == 12) { + // 2 bytes H: 0-360, 2 bytes each S,B, 0-1000 + double h = Integer.parseInt(hexColor.substring(0, 4), 16); + double s = Integer.parseInt(hexColor.substring(4, 8), 16) / 10.0; + double b = Integer.parseInt(hexColor.substring(8, 12), 16) / 10.0; + if (h == 360) { + h = 0; + } - return new HSBType(new DecimalType(h), new PercentType(new BigDecimal(s)), - new PercentType(new BigDecimal(b))); - } else if (hexColor.length() == 14) { - // 1 byte each RGB: 0-255, 2 byte H: 0-360, 1 byte each SB: 0-255 - int r = Integer.parseInt(hexColor.substring(0, 2), 16); - int g = Integer.parseInt(hexColor.substring(2, 4), 16); - int b = Integer.parseInt(hexColor.substring(4, 6), 16); + return new HSBType(new DecimalType(h), new PercentType(new BigDecimal(s)), + new PercentType(new BigDecimal(b))); + } else if (hexColor.length() == 14) { + // 1 byte each RGB: 0-255, 2 byte H: 0-360, 1 byte each SB: 0-255 + int r = Integer.parseInt(hexColor.substring(0, 2), 16); + int g = Integer.parseInt(hexColor.substring(2, 4), 16); + int b = Integer.parseInt(hexColor.substring(4, 6), 16); - return HSBType.fromRGB(r, g, b); - } else { - throw new IllegalArgumentException("Unknown color format"); + return HSBType.fromRGB(r, g, b); + } + } catch (NumberFormatException e) { + // do nothing } + return null; } /**