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.math.BigDecimal;
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;
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
35 * @author Bob Raker - Initial contribution
38 public class SmartthingsHue100Converter extends SmartthingsConverter {
40 private final Logger logger = LoggerFactory.getLogger(SmartthingsHue100Converter.class);
42 public SmartthingsHue100Converter(Thing thing) {
47 public String convertToSmartthings(ChannelUID channelUid, Command command) {
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);
58 jsonMsg = defaultConvertToSmartthings(channelUid, command);
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;
70 if (deviceValue == null) {
71 logger.warn("Failed to convert Number {} because Smartthings returned a null value.", deviceType);
72 return UnDefType.UNDEF;
75 if (acceptedChannelType != null && "Number".contentEquals(acceptedChannelType)) {
76 if (deviceValue instanceof String stringCommand) {
77 double d = Double.parseDouble(stringCommand);
79 return new DecimalType(d);
80 } else if (deviceValue instanceof Long) {
81 double d = ((Long) deviceValue).longValue();
83 return new DecimalType(d);
84 } else if (deviceValue instanceof BigDecimal decimalValue) {
85 double d = decimalValue.doubleValue();
87 return new DecimalType(d);
88 } else if (deviceValue instanceof Number numberValue) {
89 double d = numberValue.doubleValue();
91 return new DecimalType(d);
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;
98 return defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);