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.hue.internal;
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17 import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
18 import static org.junit.jupiter.api.Assertions.assertTrue;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.hue.internal.api.dto.clip1.ColorTemperature;
23 import org.openhab.binding.hue.internal.api.dto.clip1.State;
24 import org.openhab.binding.hue.internal.api.dto.clip1.State.ColorMode;
25 import org.openhab.binding.hue.internal.api.dto.clip1.StateUpdate;
26 import org.openhab.binding.hue.internal.handler.LightStateConverter;
27 import org.openhab.core.library.types.DecimalType;
28 import org.openhab.core.library.types.HSBType;
29 import org.openhab.core.library.types.PercentType;
33 * @author Markus Bösling - Initial contribution
34 * @author Denis Dudnik - switched to internally integrated source of Jue library
35 * @author Markus Rathgeb - migrated to plain Java test
38 public class LightStateConverterTest {
41 public void colorTemperatureLightStateConverterConversionIsBijectiveDefaultColorTemperatureCapabilities() {
42 final State lightState = new State();
43 final ColorTemperature colorTemperature = new ColorTemperature();
44 for (int percent = 1; percent <= 100; ++percent) {
45 StateUpdate stateUpdate = LightStateConverter
46 .toColorTemperatureLightStateFromPercentType(new PercentType(percent), colorTemperature);
47 assertThat(stateUpdate.commands, hasSize(1));
48 assertThat(stateUpdate.commands.get(0).key, is("ct"));
49 lightState.ct = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
50 assertThat(LightStateConverter.toColorTemperaturePercentType(lightState, colorTemperature).intValue(),
56 public void colorTemperatureLightStateConverterConversionIsBijectiveIndividualColorTemperatureCapabilities() {
57 final State lightState = new State();
58 final ColorTemperature colorTemperature = new ColorTemperature();
59 colorTemperature.min = 250;
60 colorTemperature.max = 454;
61 for (int percent = 1; percent <= 100; ++percent) {
62 StateUpdate stateUpdate = LightStateConverter
63 .toColorTemperatureLightStateFromPercentType(new PercentType(percent), colorTemperature);
64 assertThat(stateUpdate.commands, hasSize(1));
65 assertThat(stateUpdate.commands.get(0).key, is("ct"));
66 lightState.ct = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
67 assertThat(LightStateConverter.toColorTemperaturePercentType(lightState, colorTemperature).intValue(),
73 public void brightnessOfZeroIsZero() {
74 final State lightState = new State();
75 // 0 percent should not be sent to the Hue interface
76 StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(PercentType.ZERO);
77 assertThat(stateUpdate.commands, hasSize(1));
78 // a brightness of 0 should result in 0 percent
80 assertThat(LightStateConverter.toBrightnessPercentType(lightState), is(PercentType.ZERO));
84 public void brightnessLightStateConverterConversionIsBijective() {
85 final State lightState = new State();
86 for (int percent = 1; percent <= 100; ++percent) {
87 StateUpdate stateUpdate = LightStateConverter.toBrightnessLightState(new PercentType(percent));
88 assertThat(stateUpdate.commands, hasSize(2));
89 assertThat(stateUpdate.commands.get(1).key, is("bri"));
90 lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
91 assertThat(LightStateConverter.toBrightnessPercentType(lightState).intValue(), is(percent));
96 public void brightnessAlwaysGreaterThanZero() {
97 final State lightState = new State();
98 // a brightness greater than 1 should result in a percentage greater than 1
99 for (int brightness = 1; brightness <= 254; ++brightness) {
100 lightState.bri = brightness;
101 assertTrue(LightStateConverter.toBrightnessPercentType(lightState).intValue() > 0);
106 public void colorWithBightnessOfZeroIsZero() {
107 final State lightState = new State();
108 lightState.colormode = ColorMode.CT.toString();
109 // 0 percent should not be sent to the Hue interface
110 final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, PercentType.ZERO);
111 StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
112 assertThat(stateUpdate.commands, hasSize(1));
113 // a brightness of 0 should result in 0 percent
115 assertThat(LightStateConverter.toHSBType(lightState).getBrightness(), is(PercentType.ZERO));
119 public void colorLightStateConverterForBrightnessConversionIsBijective() {
120 final State lightState = new State();
121 lightState.colormode = ColorMode.CT.toString();
122 for (int percent = 1; percent <= 100; ++percent) {
123 final HSBType hsbType = new HSBType(DecimalType.ZERO, PercentType.ZERO, new PercentType(percent));
124 StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
125 assertThat(stateUpdate.commands, hasSize(2));
126 assertThat(stateUpdate.commands.get(1).key, is("bri"));
127 lightState.bri = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
128 assertThat(LightStateConverter.toHSBType(lightState).getBrightness().intValue(), is(percent));
133 public void hsbBrightnessAlwaysGreaterThanZero() {
134 final State lightState = new State();
135 lightState.colormode = ColorMode.CT.toString();
136 // a brightness greater than 1 should result in a percentage greater than 1
137 for (int brightness = 1; brightness <= 254; ++brightness) {
138 lightState.bri = brightness;
139 assertTrue(LightStateConverter.toHSBType(lightState).getBrightness().intValue() > 0);
144 public void hsbHueAlwaysGreaterThanZeroAndLessThan360() {
145 final State lightState = new State();
146 for (int hue = 0; hue <= 65535; ++hue) {
147 lightState.hue = hue;
148 assertTrue(LightStateConverter.toHSBType(lightState).getHue().intValue() >= 0);
149 assertTrue(LightStateConverter.toHSBType(lightState).getHue().intValue() < 360);
154 public void colorLightStateConverterForSaturationConversionIsBijective() {
155 final State lightState = new State();
156 lightState.colormode = ColorMode.HS.toString();
157 for (int percent = 0; percent <= 100; ++percent) {
158 final HSBType hsbType = new HSBType(DecimalType.ZERO, new PercentType(percent), PercentType.HUNDRED);
159 StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
160 assertThat(stateUpdate.commands, hasSize(3));
161 assertThat(stateUpdate.commands.get(1).key, is("sat"));
162 lightState.sat = Integer.parseInt(stateUpdate.commands.get(1).value.toString());
163 assertThat(LightStateConverter.toHSBType(lightState).getSaturation().intValue(), is(percent));
168 public void colorLightStateConverterForHueConversionIsBijective() {
169 final State lightState = new State();
170 lightState.colormode = ColorMode.HS.toString();
171 for (int hue = 0; hue < 360; ++hue) {
172 final HSBType hsbType = new HSBType(new DecimalType(hue), PercentType.HUNDRED, PercentType.HUNDRED);
173 StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
174 assertThat(stateUpdate.commands, hasSize(3));
175 assertThat(stateUpdate.commands.get(0).key, is("hue"));
176 lightState.hue = Integer.parseInt(stateUpdate.commands.get(0).value.toString());
177 assertThat(LightStateConverter.toHSBType(lightState).getHue().intValue(), is(hue));
182 public void colorLightStateConverterColorModeSelection() {
183 final State lightState = new State();
184 final HSBType hsbType = new HSBType(PercentType.HUNDRED, PercentType.HUNDRED, PercentType.HUNDRED);
186 lightState.colormode = null;
187 StateUpdate stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
188 assertThat(stateUpdate.commands, hasSize(2));
189 assertThat(stateUpdate.commands.get(0).key, is("xy"));
191 lightState.colormode = ColorMode.CT.toString();
192 stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
193 assertThat(stateUpdate.commands, hasSize(2));
194 assertThat(stateUpdate.commands.get(0).key, is("xy"));
196 lightState.colormode = ColorMode.HS.toString();
197 stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
198 assertThat(stateUpdate.commands, hasSize(3));
199 assertThat(stateUpdate.commands.get(0).key, is("hue"));
201 lightState.colormode = ColorMode.XY.toString();
202 stateUpdate = LightStateConverter.toColorLightState(hsbType, lightState);
203 assertThat(stateUpdate.commands, hasSize(2));
204 assertThat(stateUpdate.commands.get(0).key, is("xy"));
208 public void hsbSaturationAlwaysGreaterThanZero() {
209 final State lightState = new State();
210 lightState.colormode = ColorMode.CT.toString();
211 // a saturation greater than 1 should result in a percentage greater than 1
212 for (int saturation = 1; saturation <= 254; ++saturation) {
213 lightState.sat = saturation;
214 assertTrue(LightStateConverter.toHSBType(lightState).getSaturation().intValue() > 0);