2 * Copyright (c) 2010-2021 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.hue.internal.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.eclipse.jdt.annotation.Nullable;
17 import org.openhab.binding.hue.internal.State;
18 import org.openhab.binding.hue.internal.State.AlertMode;
19 import org.openhab.binding.hue.internal.State.ColorMode;
20 import org.openhab.binding.hue.internal.State.Effect;
21 import org.openhab.binding.hue.internal.StateUpdate;
22 import org.openhab.binding.hue.internal.dto.ColorTemperature;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.HSBType;
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.library.types.StringType;
31 * The {@link LightStateConverter} is responsible for mapping to/from jue types.
33 * @author Dennis Nobel - Initial contribution
34 * @author Oliver Libutzki - Adjustments
35 * @author Kai Kreuzer - made code static
36 * @author Andre Fuechsel - added method for brightness
37 * @author Yordan Zhelev - added method for alert
38 * @author Denis Dudnik - switched to internally integrated source of Jue library, minor code cleanup
39 * @author Christoph Weitkamp - Added support for bulbs using CIE XY colormode only
42 public class LightStateConverter {
44 private static final double HUE_FACTOR = 65535 / 360.0;
45 private static final double SATURATION_FACTOR = 2.54;
46 private static final double BRIGHTNESS_FACTOR = 2.54;
49 * {@value #ALERT_MODE_NONE}. The light is not performing an alert effect.
51 static final String ALERT_MODE_NONE = "NONE";
53 * {@value #ALERT_MODE_SELECT}. The light is performing one breathe cycle.
55 static final String ALERT_MODE_SELECT = "SELECT";
57 * {@value #ALERT_MODE_LONG_SELECT}. The light is performing breathe cycles
58 * for 15 seconds or until an "alert": "none" command is received.
60 static final String ALERT_MODE_LONG_SELECT = "LSELECT";
62 private static final int DIM_STEPSIZE = 30;
65 * Transforms the given {@link HSBType} into a light state.
67 * @param hsbType HSB type
68 * @return light state representing the {@link HSBType}.
70 public static StateUpdate toColorLightState(HSBType hsbType, State lightState) {
71 // XY color is the implicit default: Use XY color mode if i) no color mode is set or ii) if the bulb is in
72 // CT mode or iii) already in XY mode. Only if the bulb is in HS mode, use this one.
73 StateUpdate stateUpdate = ColorMode.HS.equals(lightState.getColorMode()) ? toHSBColorLightState(hsbType)
74 : toXYColorLightState(hsbType);
76 int brightness = (int) Math.floor(hsbType.getBrightness().doubleValue() * BRIGHTNESS_FACTOR);
78 stateUpdate.setBrightness(brightness);
83 private static StateUpdate toHSBColorLightState(HSBType hsbType) {
84 int hue = (int) Math.round(hsbType.getHue().doubleValue() * HUE_FACTOR);
85 int saturation = (int) Math.floor(hsbType.getSaturation().doubleValue() * SATURATION_FACTOR);
87 return new StateUpdate().setHue(hue).setSat(saturation);
90 private static StateUpdate toXYColorLightState(HSBType hsbType) {
91 PercentType[] xy = hsbType.toXY();
92 float x = xy[0].floatValue() / 100.0f;
93 float y = xy[1].floatValue() / 100.0f;
95 return new StateUpdate().setXY(x, y);
99 * Transforms the given {@link OnOffType} into a light state containing the
102 * @param onOffType on or off state
103 * @return light state containing the 'on' value
105 public static StateUpdate toOnOffLightState(OnOffType onOffType) {
106 return new StateUpdate().setOn(OnOffType.ON.equals(onOffType));
110 * Transforms the given {@link PercentType} into a light state containing
111 * the brightness and the 'on' value represented by {@link PercentType}.
113 * @param percentType brightness represented as {@link PercentType}
114 * @return light state containing the brightness and the 'on' value
116 public static StateUpdate toBrightnessLightState(PercentType percentType) {
117 boolean on = !percentType.equals(PercentType.ZERO);
118 final StateUpdate stateUpdate = new StateUpdate().setOn(on);
120 int brightness = (int) Math.floor(percentType.doubleValue() * BRIGHTNESS_FACTOR);
121 if (brightness > 0) {
122 stateUpdate.setBrightness(brightness);
128 * Adjusts the given brightness using the {@link IncreaseDecreaseType} and
129 * returns the updated value.
131 * @param command The {@link IncreaseDecreaseType} to be used
132 * @param currentBrightness The current brightness
133 * @return The adjusted brightness value
135 public static int toAdjustedBrightness(IncreaseDecreaseType command, int currentBrightness) {
137 if (command == IncreaseDecreaseType.DECREASE) {
138 newBrightness = Math.max(currentBrightness - DIM_STEPSIZE, 0);
140 newBrightness = Math.min(currentBrightness + DIM_STEPSIZE, (int) (BRIGHTNESS_FACTOR * 100));
142 return newBrightness;
146 * Transforms the given {@link PercentType} into a light state containing
147 * the color temperature represented by {@link PercentType}.
149 * @param percentType color temperature represented as {@link PercentType}
150 * @return light state containing the color temperature
152 public static StateUpdate toColorTemperatureLightStateFromPercentType(PercentType percentType,
153 ColorTemperature capabilities) {
154 int colorTemperature = capabilities.min
155 + Math.round(((capabilities.max - capabilities.min) * percentType.floatValue()) / 100);
156 return new StateUpdate().setColorTemperature(colorTemperature, capabilities);
159 public static int kelvinToMired(int kelvinValue) {
160 return (int) (1000000.0 / kelvinValue);
164 * Transforms the given {@link DecimalType} into a light state containing
165 * the color temperature in Kelvin.
167 * @param decimalType color temperature in Kelvin
168 * @return light state containing the color temperature
170 public static StateUpdate toColorTemperatureLightState(DecimalType decimalType, ColorTemperature capabilities) {
171 return new StateUpdate().setColorTemperature(kelvinToMired(decimalType.intValue()), capabilities);
175 * Adjusts the given color temperature using the {@link IncreaseDecreaseType} and returns the updated value.
177 * @param type The {@link IncreaseDecreaseType} to be used
178 * @param currentColorTemp The current color temperature
179 * @return The adjusted color temperature value
181 public static int toAdjustedColorTemp(IncreaseDecreaseType type, int currentColorTemp,
182 ColorTemperature capabilities) {
184 if (type == IncreaseDecreaseType.DECREASE) {
185 newColorTemp = Math.max(currentColorTemp - DIM_STEPSIZE, capabilities.min);
187 newColorTemp = Math.min(currentColorTemp + DIM_STEPSIZE, capabilities.max);
193 * Transforms Hue Light {@link State} into {@link PercentType} representing
194 * the color temperature.
196 * @param lightState light state
197 * @return percent type representing the color temperature
199 public static PercentType toColorTemperaturePercentType(State lightState, ColorTemperature capabilities) {
200 int percent = (int) Math.round(((lightState.getColorTemperature() - capabilities.min) * 100.0)
201 / (capabilities.max - capabilities.min));
202 return new PercentType(restrictToBounds(percent));
205 public static int miredToKelvin(int miredValue) {
206 return (int) (1000000.0 / miredValue);
210 * Transforms Hue Light {@link State} into {@link DecimalType} representing
211 * the color temperature in Kelvin.
213 * @param lightState light state
214 * @return percent type representing the color temperature in Kelvin
216 public static DecimalType toColorTemperature(State lightState) {
217 return new DecimalType(miredToKelvin(lightState.getColorTemperature()));
221 * Transforms Hue Light {@link State} into {@link PercentType} representing
224 * @param lightState light state
225 * @return percent type representing the brightness
227 public static PercentType toBrightnessPercentType(State lightState) {
228 int percent = (int) Math.ceil(lightState.getBrightness() / BRIGHTNESS_FACTOR);
229 return new PercentType(restrictToBounds(percent));
233 * Transforms {@link State} into {@link StringType} representing the {@link AlertMode}.
235 * @param lightState light state.
236 * @return string type representing the alert mode.
238 public static StringType toAlertStringType(State lightState) {
239 AlertMode alertMode = lightState.getAlertMode();
240 if (alertMode == null) {
241 return new StringType("NULL");
243 return new StringType(alertMode.toString());
248 * Transforms Hue Light {@link State} into {@link HSBType} representing the
251 * @param lightState light state
252 * @return HSB type representing the color
254 public static HSBType toHSBType(State lightState) {
255 // even if color mode is reported to be XY, xy field of lightState might be null, while hsb is available
256 boolean isInXYMode = ColorMode.XY.equals(lightState.getColorMode()) && lightState.getXY() != null;
257 return isInXYMode ? fromXYtoHSBType(lightState) : fromHSBtoHSBType(lightState);
260 private static HSBType fromHSBtoHSBType(State lightState) {
261 int hue = (int) Math.round(lightState.getHue() / HUE_FACTOR) % 360;
263 int saturationInPercent = (int) Math.ceil(lightState.getSaturation() / SATURATION_FACTOR);
264 saturationInPercent = restrictToBounds(saturationInPercent);
266 int brightnessInPercent = (int) Math.ceil(lightState.getBrightness() / BRIGHTNESS_FACTOR);
267 brightnessInPercent = restrictToBounds(brightnessInPercent);
269 return new HSBType(new DecimalType(hue), new PercentType(saturationInPercent),
270 new PercentType(brightnessInPercent));
273 private static HSBType fromXYtoHSBType(State lightState) {
274 float[] xy = lightState.getXY();
275 HSBType hsb = HSBType.fromXY(xy[0], xy[1]);
277 int brightnessInPercent = (int) Math.ceil(lightState.getBrightness() / BRIGHTNESS_FACTOR);
278 brightnessInPercent = restrictToBounds(brightnessInPercent);
280 return new HSBType(hsb.getHue(), hsb.getSaturation(), new PercentType(brightnessInPercent));
284 * Transforms the given {@link StringType} into a light state containing the {@link AlertMode} to be triggered.
286 * @param alertType {@link StringType} representing the required {@link AlertMode} . <br>
287 * Supported values are:
289 * <li>{@value #ALERT_MODE_NONE}.
290 * <li>{@value #ALERT_MODE_SELECT}.
291 * <li>{@value #ALERT_MODE_LONG_SELECT}.
293 * @return light state containing the {@link AlertMode} or <b><code>null </code></b> if the provided
294 * {@link StringType} represents unsupported mode.
296 public static @Nullable StateUpdate toAlertState(StringType alertType) {
299 switch (alertType.toString()) {
300 case ALERT_MODE_NONE:
301 alertMode = State.AlertMode.NONE;
303 case ALERT_MODE_SELECT:
304 alertMode = State.AlertMode.SELECT;
306 case ALERT_MODE_LONG_SELECT:
307 alertMode = State.AlertMode.LSELECT;
312 return new StateUpdate().setAlert(alertMode);
316 * Transforms the given {@link OnOffType} into a light state containing the {@link Effect} value.
317 * {@link OnOffType#ON} will result in {@link Effect#COLORLOOP}. {@link OnOffType#OFF} will result in
318 * {@link Effect#NONE}.
320 * @param onOffType on or off state
321 * @return light state containing the {@link Effect} value
323 public static StateUpdate toOnOffEffectState(OnOffType onOffType) {
324 StateUpdate stateUpdate;
326 if (OnOffType.ON.equals(onOffType)) {
327 stateUpdate = new StateUpdate().setEffect(Effect.COLORLOOP);
329 stateUpdate = new StateUpdate().setEffect(Effect.NONE);
335 private static int restrictToBounds(int percentValue) {
336 if (percentValue < 0) {
338 } else if (percentValue > 100) {