2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.smartthings.internal.converter;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.smartthings.internal.dto.SmartthingsStateData;
21 import org.openhab.core.library.types.HSBType;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.UnDefType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * Converter class for Smartthings "Color" capability and not the "Color Control" capability.
32 * The Smartthings Color capability seems to be a later capability where the hue is in the standard 0 - 360 range and
33 * therefore doesn't need to be converted for openHAB
35 * @author Bob Raker - Initial contribution
38 public class SmartthingsColorConverter extends SmartthingsConverter {
40 private Pattern rgbInputPattern = Pattern.compile("^#[0-9a-fA-F]{6}");
42 private final Logger logger = LoggerFactory.getLogger(SmartthingsColorConverter.class);
44 public SmartthingsColorConverter(Thing thing) {
49 public String convertToSmartthings(ChannelUID channelUid, Command command) {
50 String jsonMsg = defaultConvertToSmartthings(channelUid, command);
57 * @see org.openhab.binding.smartthings.internal.converter.SmartthingsConverter#convertToOpenHab(java.lang.String,
58 * org.openhab.binding.smartthings.internal.SmartthingsStateData)
61 public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
62 // The color value from Smartthings will look like "#123456" which is the RGB color
63 // This needs to be converted into HSB type
64 String value = dataFromSmartthings.value;
66 logger.warn("Failed to convert color {} because Smartthings returned a null value.",
67 dataFromSmartthings.deviceDisplayName);
68 return UnDefType.UNDEF;
71 // First verify the format the string is valid
72 Matcher matcher = rgbInputPattern.matcher(value);
73 if (!matcher.matches()) {
75 "The \"value\" in the following message is not a valid color. Expected a value like \"#123456\" instead of {}",
76 dataFromSmartthings.toString());
77 return UnDefType.UNDEF;
81 int rgb[] = new int[3];
82 for (int i = 0, pos = 1; i < 3; i++, pos += 2) {
83 String c = value.substring(pos, pos + 2);
84 rgb[i] = Integer.parseInt(c, 16);
88 State state = HSBType.fromRGB(rgb[0], rgb[1], rgb[2]);