]> git.basschouten.com Git - openhab-addons.git/blob
15174da3cc4524fc109bc908fe2240c936db8ee5
[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.math.BigDecimal;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.smartthings.internal.dto.SmartthingsStateData;
20 import org.openhab.core.library.types.DecimalType;
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 capability "Color Control".
32  * The Smartthings "Color Control" capability represents the hue values in the 0-100% range. OH2 uses 0-360 degrees
33  * For this converter only the hue is coming into openHAB and it is a number
34  *
35  * @author Bob Raker - Initial contribution
36  */
37 @NonNullByDefault
38 public class SmartthingsHue100Converter extends SmartthingsConverter {
39
40     private final Logger logger = LoggerFactory.getLogger(SmartthingsHue100Converter.class);
41
42     public SmartthingsHue100Converter(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public String convertToSmartthings(ChannelUID channelUid, Command command) {
48         String jsonMsg;
49
50         if (command instanceof HSBType hsbCommand) {
51             double hue = hsbCommand.getHue().doubleValue() / 3.60;
52             String value = String.format("[%.0f, %d, %d ]", hue, hsbCommand.getSaturation().intValue(),
53                     hsbCommand.getBrightness().intValue());
54             jsonMsg = String.format(
55                     "{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"capabilityAttribute\": \"%s\", \"value\": %s}",
56                     thingTypeId, smartthingsName, channelUid.getId(), value);
57         } else {
58             jsonMsg = defaultConvertToSmartthings(channelUid, command);
59         }
60
61         return jsonMsg;
62     }
63
64     @Override
65     public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
66         // Here we have to multiply the value from Smartthings by 3.6 to convert from 0-100 to 0-360
67         String deviceType = dataFromSmartthings.capabilityAttribute;
68         Object deviceValue = dataFromSmartthings.value;
69
70         if (deviceValue == null) {
71             logger.warn("Failed to convert Number {} because Smartthings returned a null value.", deviceType);
72             return UnDefType.UNDEF;
73         }
74
75         if (acceptedChannelType != null && "Number".contentEquals(acceptedChannelType)) {
76             if (deviceValue instanceof String stringCommand) {
77                 double d = Double.parseDouble(stringCommand);
78                 d *= 3.6;
79                 return new DecimalType(d);
80             } else if (deviceValue instanceof Long) {
81                 double d = ((Long) deviceValue).longValue();
82                 d *= 3.6;
83                 return new DecimalType(d);
84             } else if (deviceValue instanceof BigDecimal decimalValue) {
85                 double d = decimalValue.doubleValue();
86                 d *= 3.6;
87                 return new DecimalType(d);
88             } else if (deviceValue instanceof Number numberValue) {
89                 double d = numberValue.doubleValue();
90                 d *= 3.6;
91                 return new DecimalType(d);
92             } else {
93                 logger.warn("Failed to convert Number {} with a value of {} from class {} to an appropriate type.",
94                         deviceType, deviceValue, deviceValue.getClass().getName());
95                 return UnDefType.UNDEF;
96             }
97         } else {
98             return defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);
99         }
100     }
101 }