]> git.basschouten.com Git - openhab-addons.git/blob
d7273293a174bb895c092fbb6c49d79bd80eaf3c
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.smartthings.internal.converter;
14
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
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;
29
30 /**
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
34  *
35  * @author Bob Raker - Initial contribution
36  */
37 @NonNullByDefault
38 public class SmartthingsColorConverter extends SmartthingsConverter {
39
40     private Pattern rgbInputPattern = Pattern.compile("^#[0-9a-fA-F]{6}");
41
42     private final Logger logger = LoggerFactory.getLogger(SmartthingsColorConverter.class);
43
44     public SmartthingsColorConverter(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public String convertToSmartthings(ChannelUID channelUid, Command command) {
50         return defaultConvertToSmartthings(channelUid, command);
51     }
52
53     /*
54      * (non-Javadoc)
55      *
56      * @see org.openhab.binding.smartthings.internal.converter.SmartthingsConverter#convertToOpenHab(java.lang.String,
57      * org.openhab.binding.smartthings.internal.SmartthingsStateData)
58      */
59     @Override
60     public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
61         // The color value from Smartthings will look like "#123456" which is the RGB color
62         // This needs to be converted into HSB type
63         String value = dataFromSmartthings.value;
64         if (value == null) {
65             logger.warn("Failed to convert color {} because Smartthings returned a null value.",
66                     dataFromSmartthings.deviceDisplayName);
67             return UnDefType.UNDEF;
68         }
69
70         // First verify the format the string is valid
71         Matcher matcher = rgbInputPattern.matcher(value);
72         if (!matcher.matches()) {
73             logger.warn(
74                     "The \"value\" in the following message is not a valid color. Expected a value like \"#123456\" instead of {}",
75                     dataFromSmartthings.toString());
76             return UnDefType.UNDEF;
77         }
78
79         // Get the RGB colors
80         int[] rgb = new int[3];
81         for (int i = 0, pos = 1; i < 3; i++, pos += 2) {
82             String c = value.substring(pos, pos + 2);
83             rgb[i] = Integer.parseInt(c, 16);
84         }
85
86         // Convert to state
87         return HSBType.fromRGB(rgb[0], rgb[1], rgb[2]);
88     }
89 }