]> git.basschouten.com Git - openhab-addons.git/blob
22423988e59993c96fe20d1ecb80d2b671c09447
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dmx.internal.handler;
14
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.*;
19
20 import java.util.HashMap;
21 import java.util.Map;
22
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;
36
37 /**
38  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.TunableWhiteThingHandler}.
39  *
40  * @author Jan N. Klug - Initial contribution
41  */
42 public class TunableWhiteThingHandlerTest extends AbstractDmxThingTestParent {
43
44     private static final String TEST_CHANNEL_CONFIG = "100/2";
45     private static final int TEST_FADE_TIME = 1500;
46
47     private Map<String, Object> thingProperties;
48     private Thing dimmerThing;
49     private TunableWhiteThingHandler dimmerThingHandler;
50
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);
56
57     @Before
58     public void setUp() {
59         super.setup();
60
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())
76                 .build();
77         dimmerThingHandler = new TunableWhiteThingHandler(dimmerThing) {
78             @Override
79             protected @Nullable Bridge getBridge() {
80                 return bridge;
81             }
82         };
83         initializeHandler(dimmerThingHandler);
84     }
85
86     @Test
87     public void testThingStatus() {
88         assertThingStatus(dimmerThing);
89     }
90
91     @Test
92     public void testThingStatus_noBridge() {
93         // check that thing is offline if no bridge found
94         TunableWhiteThingHandler dimmerHandlerWithoutBridge = new TunableWhiteThingHandler(dimmerThing) {
95             @Override
96             protected @Nullable Bridge getBridge() {
97                 return null;
98             }
99         };
100         assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
101     }
102
103     @Test
104     public void testOnOffCommand() {
105         // on
106         long currentTime = System.currentTimeMillis();
107
108         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
109         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
110
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))));
118         });
119
120         // off
121         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
122         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
123
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));
129         });
130     }
131
132     @Test
133     public void testDynamicTurnOnValue() {
134         long currentTime = System.currentTimeMillis();
135         int testValue = 75;
136
137         // turn on with arbitrary value
138         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
139         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
140
141         waitForAssert(() -> {
142             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
143                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
144         });
145
146         // turn off and hopefully store last value
147         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
148         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
149
150         waitForAssert(() -> {
151             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
152                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
153         });
154
155         // turn on and restore value
156         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
157         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
158
159         waitForAssert(() -> {
160             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
161                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
162         });
163     }
164
165     @Test
166     public void testPercentTypeCommand() {
167         assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);
168     }
169
170     @Test
171     public void testColorTemperature() {
172         long currentTime = System.currentTimeMillis();
173
174         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
175         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
176
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))));
182         });
183
184         // cool white
185         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.ZERO);
186         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
187
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))));
197         });
198
199         // warm white
200         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.HUNDRED);
201         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
202
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))));
212         });
213
214         // intermediate white
215         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, new PercentType(75));
216         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
217
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))));
227         });
228     }
229 }