From 21de80180e5d65e887077b947b51a059265f1e18 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Sun, 24 Dec 2023 04:11:42 -0500 Subject: [PATCH] [tuya] Improve decode color format error handling (#541) Signed-off-by: jsetton (cherry picked from commit a616368603306f7893ff839c8b6f71a3a69e19e7) (cherry picked from commit 068e9f394e86407d915d3c0d6b7917e7afbf9a67) --- .../internal/handler/TuyaDeviceHandler.java | 57 ++++++++++--------- 1 file changed, 31 insertions(+), 26 deletions(-) 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..06989eba87 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; @@ -163,36 +164,40 @@ private void processChannelStatus(Integer dp, Object value) { return; } - if (value instanceof String && CHANNEL_TYPE_UID_COLOR.equals(channelTypeUID)) { - oldColorMode = ((String) value).length() == 14; - updateState(channelId, ConversionUtil.hexColorDecode((String) value)); - return; - } else if (value instanceof String && CHANNEL_TYPE_UID_STRING.equals(channelTypeUID)) { - updateState(channelId, new StringType((String) value)); - return; - } else if (Double.class.isAssignableFrom(value.getClass()) - && CHANNEL_TYPE_UID_DIMMER.equals(channelTypeUID)) { - updateState(channelId, ConversionUtil.brightnessDecode((double) value, 0, configuration.max)); - return; - } else if (Double.class.isAssignableFrom(value.getClass()) - && CHANNEL_TYPE_UID_NUMBER.equals(channelTypeUID)) { - updateState(channelId, new DecimalType((double) value)); - return; - } else if (Boolean.class.isAssignableFrom(value.getClass()) - && CHANNEL_TYPE_UID_SWITCH.equals(channelTypeUID)) { - updateState(channelId, OnOffType.from((boolean) value)); - return; - } else if (value instanceof String && CHANNEL_TYPE_UID_IR_CODE.equals(channelTypeUID)) { - if (configuration.dp == 2) { - String decoded = convertBase64Code(configuration, (String) value); - logger.info("thing {} received ir code: {}", thing.getUID(), decoded); - updateState(channelId, new StringType(decoded)); - irStartLearning(configuration.activeListen); + try { + if (value instanceof String && CHANNEL_TYPE_UID_COLOR.equals(channelTypeUID)) { + oldColorMode = ((String) value).length() == 14; + updateState(channelId, ConversionUtil.hexColorDecode((String) value)); + return; + } else if (value instanceof String && CHANNEL_TYPE_UID_STRING.equals(channelTypeUID)) { + updateState(channelId, new StringType((String) value)); + return; + } else if (Double.class.isAssignableFrom(value.getClass()) + && CHANNEL_TYPE_UID_DIMMER.equals(channelTypeUID)) { + updateState(channelId, ConversionUtil.brightnessDecode((double) value, 0, configuration.max)); + return; + } else if (Double.class.isAssignableFrom(value.getClass()) + && CHANNEL_TYPE_UID_NUMBER.equals(channelTypeUID)) { + updateState(channelId, new DecimalType((double) value)); + return; + } else if (Boolean.class.isAssignableFrom(value.getClass()) + && CHANNEL_TYPE_UID_SWITCH.equals(channelTypeUID)) { + updateState(channelId, OnOffType.from((boolean) value)); + return; + } else if (value instanceof String && CHANNEL_TYPE_UID_IR_CODE.equals(channelTypeUID)) { + if (configuration.dp == 2) { + String decoded = convertBase64Code(configuration, (String) value); + logger.info("thing {} received ir code: {}", thing.getUID(), decoded); + updateState(channelId, new StringType(decoded)); + irStartLearning(configuration.activeListen); + } + return; } - return; + } catch (IllegalArgumentException ignored) { } logger.warn("Could not update channel '{}' of thing '{}' with value '{}'. Datatype incompatible.", channelId, getThing().getUID(), value); + updateState(channelId, UnDefType.UNDEF); } else { // try additional channelDps, only OnOffType List channelIds = dp2ToChannelId.get(dp);