Skip to content

Commit

Permalink
[tuya] Improve decode color format error handling
Browse files Browse the repository at this point in the history
Signed-off-by: jsetton <[email protected]>
  • Loading branch information
jsetton committed Dec 21, 2023
1 parent d1ee7fd commit ad1212b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand Down

0 comments on commit ad1212b

Please sign in to comment.