2 * Copyright (c) 2010-2022 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.dmx.internal.handler;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.core.Is.is;
17 import static org.hamcrest.number.IsCloseTo.closeTo;
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
21 import java.util.HashMap;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.junit.jupiter.api.BeforeEach;
26 import org.junit.jupiter.api.Test;
27 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
28 import org.openhab.core.config.core.Configuration;
29 import org.openhab.core.library.types.OnOffType;
30 import org.openhab.core.library.types.PercentType;
31 import org.openhab.core.thing.Bridge;
32 import org.openhab.core.thing.ChannelUID;
33 import org.openhab.core.thing.Thing;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.builder.ChannelBuilder;
36 import org.openhab.core.thing.binding.builder.ThingBuilder;
39 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.TunableWhiteThingHandler}.
41 * @author Jan N. Klug - Initial contribution
43 public class TunableWhiteThingHandlerTest extends AbstractDmxThingTestParent {
45 private static final String TEST_CHANNEL_CONFIG = "100/2";
46 private static final int TEST_FADE_TIME = 1500;
48 private Map<String, Object> thingProperties;
49 private Thing dimmerThing;
50 private TunableWhiteThingHandler dimmerThingHandler;
52 private final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_TUNABLEWHITE, "testdimmer");
53 private final ChannelUID CHANNEL_UID_BRIGHTNESS = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS);
54 private final ChannelUID CHANNEL_UID_BRIGHTNESS_CW = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_CW);
55 private final ChannelUID CHANNEL_UID_BRIGHTNESS_WW = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_WW);
56 private final ChannelUID CHANNEL_UID_COLOR_TEMP = new ChannelUID(THING_UID_DIMMER, CHANNEL_COLOR_TEMPERATURE);
62 thingProperties = new HashMap<>();
63 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
64 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
65 thingProperties.put(CONFIG_DIMMER_TURNONVALUE, "127,127");
66 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
67 dimmerThing = ThingBuilder.create(THING_TYPE_TUNABLEWHITE, "testdimmer").withLabel("Dimmer Thing")
68 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
69 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS, "Brightness")
70 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
71 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_CW, "Brightness CW")
72 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
73 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_WW, "Brightness WW")
74 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
75 .withChannel(ChannelBuilder.create(CHANNEL_UID_COLOR_TEMP, "Color temperature")
76 .withType(COLOR_TEMPERATURE_CHANNEL_TYPEUID).build())
78 dimmerThingHandler = new TunableWhiteThingHandler(dimmerThing) {
80 protected @Nullable Bridge getBridge() {
84 initializeHandler(dimmerThingHandler);
88 public void testThingStatus() {
89 assertThingStatus(dimmerThing);
93 public void testThingStatus_noBridge() {
94 // check that thing is offline if no bridge found
95 TunableWhiteThingHandler dimmerHandlerWithoutBridge = new TunableWhiteThingHandler(dimmerThing) {
97 protected @Nullable Bridge getBridge() {
101 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
105 public void testOnOffCommand() {
107 long currentTime = System.currentTimeMillis();
109 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
110 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
112 waitForAssert(() -> {
113 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
114 state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
115 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
116 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50, 0.5))));
117 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
118 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50, 0.5))));
122 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
123 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
125 waitForAssert(() -> {
126 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
127 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
128 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW, state -> assertEquals(PercentType.ZERO, state));
129 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW, state -> assertEquals(PercentType.ZERO, state));
134 public void testDynamicTurnOnValue() {
135 long currentTime = System.currentTimeMillis();
138 // turn on with arbitrary value
139 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
140 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
142 waitForAssert(() -> {
143 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
144 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
147 // turn off and hopefully store last value
148 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
149 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
151 waitForAssert(() -> {
152 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
153 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
156 // turn on and restore value
157 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
158 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
160 waitForAssert(() -> {
161 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
162 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
167 public void testPercentTypeCommand() {
168 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);
172 public void testColorTemperature() {
173 long currentTime = System.currentTimeMillis();
175 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
176 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
178 waitForAssert(() -> {
179 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
180 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
181 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
182 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
186 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.ZERO);
187 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
189 waitForAssert(() -> {
190 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
191 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.1))));
192 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
193 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
194 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
195 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
196 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
197 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
201 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.HUNDRED);
202 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
204 waitForAssert(() -> {
205 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
206 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.1))));
207 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
208 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
209 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
210 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
211 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
212 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
215 // intermediate white
216 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, new PercentType(75));
217 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
219 waitForAssert(() -> {
220 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
221 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.1))));
222 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
223 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 1.0))));
224 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
225 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(25.0, 0.5))));
226 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
227 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.5))));