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.DecimalType;
22 import org.openhab.core.library.types.HSBType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Converter class for Smartthings capability "Color Control".
33 * In this case the color being delivered by Smartthings is in the for #hhssbb where hh=hue in hex, ss=saturation in hex
34 * and bb=brightness in hex
35 * And, the hue is a value from 0 to 100% but openHAB expects the hue in 0 to 360
37 * @author Bob Raker - Initial contribution
40 public class SmartthingsColor100Converter extends SmartthingsConverter {
42 private Pattern rgbInputPattern = Pattern.compile("^#[0-9a-fA-F]{6}");
44 private final Logger logger = LoggerFactory.getLogger(SmartthingsColor100Converter.class);
46 public SmartthingsColor100Converter(Thing thing) {
51 public String convertToSmartthings(ChannelUID channelUid, Command command) {
53 // The command should be of HSBType. The hue component needs to be divided by 3.6 to convert 0-360 degrees to
55 // The easiest way to do this is to create a new HSBType with the hue component changed.
56 if (command instanceof HSBType hsbCommand) {
57 double hue = Math.round((hsbCommand.getHue().doubleValue() / 3.60)); // add .5 to round
58 long hueInt = (long) hue;
59 HSBType hsb100 = new HSBType(new DecimalType(hueInt), hsbCommand.getSaturation(),
60 hsbCommand.getBrightness());
61 // now use the default converter to convert to a JSON string
62 jsonMsg = defaultConvertToSmartthings(channelUid, hsb100);
64 jsonMsg = defaultConvertToSmartthings(channelUid, command);
72 * @see org.openhab.binding.smartthings.internal.converter.SmartthingsConverter#convertToOpenHab(java.lang.String,
73 * org.openhab.binding.smartthings.internal.SmartthingsStateData)
76 public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
77 // The color value from Smartthings will look like "#123456" which is the RGB color
78 // This needs to be converted into HSB type
79 String value = dataFromSmartthings.value;
81 logger.warn("Failed to convert color {} because Smartthings returned a null value.",
82 dataFromSmartthings.deviceDisplayName);
83 return UnDefType.UNDEF;
86 // If the bulb is off the value maybe null, so better check
88 // First verify the format the string is valid
89 Matcher matcher = rgbInputPattern.matcher(value);
90 if (!matcher.matches()) {
92 "The \"value\" in the following message is not a valid color. Expected a value like \"#123456\" instead of {}",
93 dataFromSmartthings.toString());
94 return UnDefType.UNDEF;
98 int[] rgb = new int[3];
99 for (int i = 0, pos = 1; i < 3; i++, pos += 2) {
100 String c = value.substring(pos, pos + 2);
101 rgb[i] = Integer.parseInt(c, 16);
105 state = HSBType.fromRGB(rgb[0], rgb[1], rgb[2]);