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.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.NonNullByDefault;
25 import org.eclipse.jdt.annotation.Nullable;
26 import org.junit.jupiter.api.BeforeEach;
27 import org.junit.jupiter.api.Test;
28 import org.openhab.binding.dmx.test.AbstractDmxThingTestParent;
29 import org.openhab.core.config.core.Configuration;
30 import org.openhab.core.library.types.OnOffType;
31 import org.openhab.core.library.types.PercentType;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ChannelUID;
34 import org.openhab.core.thing.Thing;
35 import org.openhab.core.thing.ThingUID;
36 import org.openhab.core.thing.binding.builder.ChannelBuilder;
37 import org.openhab.core.thing.binding.builder.ThingBuilder;
40 * Tests cases for {@link org.openhab.binding.dmx.internal.handler.TunableWhiteThingHandler}.
42 * @author Jan N. Klug - Initial contribution
45 public class TunableWhiteThingHandlerTest extends AbstractDmxThingTestParent {
46 private static final String TEST_CHANNEL_CONFIG = "100/2";
47 private static final int TEST_FADE_TIME = 1500;
48 private static final ThingUID THING_UID_DIMMER = new ThingUID(THING_TYPE_TUNABLEWHITE, "testdimmer");
49 private static final ChannelUID CHANNEL_UID_BRIGHTNESS = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS);
50 private static final ChannelUID CHANNEL_UID_BRIGHTNESS_CW = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_CW);
51 private static final ChannelUID CHANNEL_UID_BRIGHTNESS_WW = new ChannelUID(THING_UID_DIMMER, CHANNEL_BRIGHTNESS_WW);
52 private static final ChannelUID CHANNEL_UID_COLOR_TEMP = new ChannelUID(THING_UID_DIMMER,
53 CHANNEL_COLOR_TEMPERATURE);
55 private @NonNullByDefault({}) Map<String, Object> thingProperties;
56 private @NonNullByDefault({}) Thing dimmerThing;
57 private @NonNullByDefault({}) TunableWhiteThingHandler dimmerThingHandler;
63 thingProperties = new HashMap<>();
64 thingProperties.put(CONFIG_DMX_ID, TEST_CHANNEL_CONFIG);
65 thingProperties.put(CONFIG_DIMMER_FADE_TIME, TEST_FADE_TIME);
66 thingProperties.put(CONFIG_DIMMER_TURNONVALUE, "127,127");
67 thingProperties.put(CONFIG_DIMMER_DYNAMICTURNONVALUE, true);
68 dimmerThing = ThingBuilder.create(THING_TYPE_TUNABLEWHITE, "testdimmer").withLabel("Dimmer Thing")
69 .withBridge(bridge.getUID()).withConfiguration(new Configuration(thingProperties))
70 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS, "Brightness")
71 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
72 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_CW, "Brightness CW")
73 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
74 .withChannel(ChannelBuilder.create(CHANNEL_UID_BRIGHTNESS_WW, "Brightness WW")
75 .withType(BRIGHTNESS_CHANNEL_TYPEUID).build())
76 .withChannel(ChannelBuilder.create(CHANNEL_UID_COLOR_TEMP, "Color temperature")
77 .withType(COLOR_TEMPERATURE_CHANNEL_TYPEUID).build())
79 dimmerThingHandler = new TunableWhiteThingHandler(dimmerThing) {
81 protected @Nullable Bridge getBridge() {
85 initializeHandler(dimmerThingHandler);
89 public void testThingStatus() {
90 assertThingStatus(dimmerThing);
94 public void testThingStatusNoBridge() {
95 // check that thing is offline if no bridge found
96 TunableWhiteThingHandler dimmerHandlerWithoutBridge = new TunableWhiteThingHandler(dimmerThing) {
98 protected @Nullable Bridge getBridge() {
102 assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
106 public void testOnOffCommand() {
108 long currentTime = System.currentTimeMillis();
110 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
111 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
113 waitForAssert(() -> {
114 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
115 state -> assertEquals(OnOffType.ON, state.as(OnOffType.class)));
116 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
117 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50, 0.5))));
118 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
119 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50, 0.5))));
123 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
124 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
126 waitForAssert(() -> {
127 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
128 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
129 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW, state -> assertEquals(PercentType.ZERO, state));
130 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW, state -> assertEquals(PercentType.ZERO, state));
135 public void testDynamicTurnOnValue() {
136 long currentTime = System.currentTimeMillis();
139 // turn on with arbitrary value
140 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
141 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
143 waitForAssert(() -> {
144 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
145 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
148 // turn off and hopefully store last value
149 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
150 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
152 waitForAssert(() -> {
153 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
154 state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
157 // turn on and restore value
158 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
159 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
161 waitForAssert(() -> {
162 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
163 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
168 public void testPercentTypeCommand() {
169 assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);
173 public void testColorTemperature() {
174 long currentTime = System.currentTimeMillis();
176 dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
177 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
179 waitForAssert(() -> {
180 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
181 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
182 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
183 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(50.0, 0.5))));
187 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.ZERO);
188 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
190 waitForAssert(() -> {
191 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
192 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.1))));
193 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
194 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
195 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
196 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
197 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
198 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
202 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.HUNDRED);
203 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
205 waitForAssert(() -> {
206 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
207 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.1))));
208 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
209 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
210 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
211 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(0.0, 0.5))));
212 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
213 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 0.5))));
216 // intermediate white
217 dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, new PercentType(75));
218 currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
220 waitForAssert(() -> {
221 assertChannelStateUpdate(CHANNEL_UID_COLOR_TEMP,
222 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.1))));
223 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
224 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(100.0, 1.0))));
225 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_CW,
226 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(25.0, 0.5))));
227 assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS_WW,
228 state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(75.0, 0.5))));