2 * Copyright (c) 2010-2020 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.core.Is.is;
16 import static org.hamcrest.number.IsCloseTo.closeTo;
17 import static org.junit.Assert.*;
18 import static org.openhab.binding.dmx.internal.DmxBindingConstants.*;
20 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
27 import org.openhab.core.config.core.Configuration;
28 import org.openhab.core.library.types.OnOffType;
29 import org.openhab.core.library.types.PercentType;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ChannelUID;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.builder.ChannelBuilder;
35 import org.openhab.core.thing.binding.builder.ThingBuilder;
38 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.TunableWhiteThingHandler}.
40 * @author Jan N. Klug - Initial contribution
42 public class TunableWhiteThingHandlerTest extends AbstractDmxThingTestParent {
44 private static final String TEST_CHANNEL_CONFIG = "100/2";
45 private static final int TEST_FADE_TIME = 1500;
47 private Map<String, Object> thingProperties;
48 private Thing dimmerThing;
49 private TunableWhiteThingHandler dimmerThingHandler;
51 private final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_TUNABLEWHITE, "testdimmer");
52 private final ChannelUID CHANNEL_UID_BRIGHTNESS = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS);
53 private final ChannelUID CHANNEL_UID_BRIGHTNESS_CW = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_CW);
54 private final ChannelUID CHANNEL_UID_BRIGHTNESS_WW = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_WW);
55 private final ChannelUID CHANNEL_UID_COLOR_TEMP = new ChannelUID(THING_UID_DIMMER, CHANNEL_COLOR_TEMPERATURE);
61 thingProperties = new HashMap<>();
62 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
63 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
64 thingProperties.put(CONFIG_DIMMER_TURNONVALUE, "127,127");
65 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
66 dimmerThing = ThingBuilder.create(THING_TYPE_TUNABLEWHITE, "testdimmer").withLabel("Dimmer Thing")
67 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
68 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS, "Brightness")
69 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
70 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_CW, "Brightness CW")
71 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
72 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_WW, "Brightness WW")
73 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
74 .withChannel(ChannelBuilder.create(CHANNEL_UID_COLOR_TEMP, "Color temperature")
75 .withType(COLOR_TEMPERATURE_CHANNEL_TYPEUID).build())
77 dimmerThingHandler = new TunableWhiteThingHandler(dimmerThing) {
79 protected @Nullable Bridge getBridge() {
83 initializeHandler(dimmerThingHandler);
87 public void testThingStatus() {
88 assertThingStatus(dimmerThing);
92 public void testThingStatus_noBridge() {
93 // check that thing is offline if no bridge found
94 TunableWhiteThingHandler dimmerHandlerWithoutBridge = new TunableWhiteThingHandler(dimmerThing) {
96 protected @Nullable Bridge getBridge() {
100 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
104 public void testOnOffCommand() {
106 long currentTime = System.currentTimeMillis();
108 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
109 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
111 waitForAssert(() -> {
112 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
113 state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
114 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
115 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50, 0.5))));
116 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
117 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50, 0.5))));
121 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
122 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
124 waitForAssert(() -> {
125 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
126 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
127 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW, state -> assertEquals(PercentType.ZERO, state));
128 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW, state -> assertEquals(PercentType.ZERO, state));
133 public void testDynamicTurnOnValue() {
134 long currentTime = System.currentTimeMillis();
137 // turn on with arbitrary value
138 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
139 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
141 waitForAssert(() -> {
142 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
143 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
146 // turn off and hopefully store last value
147 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
148 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
150 waitForAssert(() -> {
151 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
152 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
155 // turn on and restore value
156 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
157 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
159 waitForAssert(() -> {
160 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
161 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
166 public void testPercentTypeCommand() {
167 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);
171 public void testColorTemperature() {
172 long currentTime = System.currentTimeMillis();
174 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
175 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
177 waitForAssert(() -> {
178 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
179 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
180 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
181 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
185 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.ZERO);
186 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
188 waitForAssert(() -> {
189 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
190 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.1))));
191 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
192 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
193 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
194 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
195 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
196 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
200 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.HUNDRED);
201 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
203 waitForAssert(() -> {
204 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
205 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.1))));
206 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
207 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
208 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
209 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
210 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
211 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
214 // intermediate white
215 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, new PercentType(75));
216 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
218 waitForAssert(() -> {
219 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
220 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.1))));
221 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
222 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 1.0))));
223 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
224 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(25.0, 0.5))));
225 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
226 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.5))));