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) {
51 HSBType hsb = (HSBType) command;
52 double hue = hsb.getHue().doubleValue() / 3.60;
53 String value = String.format("[%.0f, %d, %d ]", hue, hsb.getSaturation().intValue(),
54 hsb.getBrightness().intValue());
55 jsonMsg = String.format(
56 "{\"capabilityKey\": \"%s\", \"deviceDisplayName\": \"%s\", \"capabilityAttribute\": \"%s\", \"value\": %s}",
57 thingTypeId, smartthingsName, channelUid.getId(), value);
59 jsonMsg = defaultConvertToSmartthings(channelUid, command);
66 public State convertToOpenHab(@Nullable String acceptedChannelType, SmartthingsStateData dataFromSmartthings) {
67 // Here we have to multiply the value from Smartthings by 3.6 to convert from 0-100 to 0-360
68 String deviceType = dataFromSmartthings.capabilityAttribute;
69 Object deviceValue = dataFromSmartthings.value;
71 if (deviceValue == null) {
72 logger.warn("Failed to convert Number {} because Smartthings returned a null value.", deviceType);
73 return UnDefType.UNDEF;
76 if (acceptedChannelType != null && "Number".contentEquals(acceptedChannelType)) {
77 if (deviceValue instanceof String) {
78 double d = Double.parseDouble((String) deviceValue);
80 return new DecimalType(d);
81 } else if (deviceValue instanceof Long) {
82 double d = ((Long) deviceValue).longValue();
84 return new DecimalType(d);
85 } else if (deviceValue instanceof BigDecimal) {
86 double d = ((BigDecimal) deviceValue).doubleValue();
88 return new DecimalType(d);
89 } else if (deviceValue instanceof Number) {
90 double d = ((Number) deviceValue).doubleValue();
92 return new DecimalType(d);
94 logger.warn("Failed to convert Number {} with a value of {} from class {} to an appropriate type.",
95 deviceType, deviceValue, deviceValue.getClass().getName());
96 return UnDefType.UNDEF;
99 return defaultConvertToOpenHab(acceptedChannelType, dataFromSmartthings);