]> git.basschouten.com Git - openhab-addons.git/blob
b00752dfc776dae02a422975a5a0cbde6bddbe9e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.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.*;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
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;
38
39 /**
40  * Tests cases for {@link org.openhab.binding.dmx.internal.handler.TunableWhiteThingHandler}.
41  *
42  * @author Jan N. Klug - Initial contribution
43  */
44 @NonNullByDefault
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);
54
55     private @NonNullByDefault({}) Map<String, Object> thingProperties;
56     private @NonNullByDefault({}) Thing dimmerThing;
57     private @NonNullByDefault({}) TunableWhiteThingHandler dimmerThingHandler;
58
59     @BeforeEach
60     public void setUp() {
61         super.setup();
62
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())
78                 .build();
79         dimmerThingHandler = new TunableWhiteThingHandler(dimmerThing) {
80             @Override
81             protected @Nullable Bridge getBridge() {
82                 return bridge;
83             }
84         };
85         initializeHandler(dimmerThingHandler);
86     }
87
88     @Test
89     public void testThingStatus() {
90         assertThingStatus(dimmerThing);
91     }
92
93     @Test
94     public void testThingStatusNoBridge() {
95         // check that thing is offline if no bridge found
96         TunableWhiteThingHandler dimmerHandlerWithoutBridge = new TunableWhiteThingHandler(dimmerThing) {
97             @Override
98             protected @Nullable Bridge getBridge() {
99                 return null;
100             }
101         };
102         assertThingStatusWithoutBridge(dimmerHandlerWithoutBridge);
103     }
104
105     @Test
106     public void testOnOffCommand() {
107         // on
108         long currentTime = System.currentTimeMillis();
109
110         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
111         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
112
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))));
120         });
121
122         // off
123         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
124         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
125
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));
131         });
132     }
133
134     @Test
135     public void testDynamicTurnOnValue() {
136         long currentTime = System.currentTimeMillis();
137         int testValue = 75;
138
139         // turn on with arbitrary value
140         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(testValue));
141         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
142
143         waitForAssert(() -> {
144             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
145                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
146         });
147
148         // turn off and hopefully store last value
149         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.OFF);
150         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
151
152         waitForAssert(() -> {
153             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
154                     state -> assertEquals(OnOffType.OFF, state.as(OnOffType.class)));
155         });
156
157         // turn on and restore value
158         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
159         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
160
161         waitForAssert(() -> {
162             assertChannelStateUpdate(CHANNEL_UID_BRIGHTNESS,
163                     state -> assertThat(((PercentType) state).doubleValue(), is(closeTo(testValue, 1.0))));
164         });
165     }
166
167     @Test
168     public void testPercentTypeCommand() {
169         assertPercentTypeCommands(dimmerThingHandler, CHANNEL_UID_BRIGHTNESS, TEST_FADE_TIME);
170     }
171
172     @Test
173     public void testColorTemperature() {
174         long currentTime = System.currentTimeMillis();
175
176         dimmerThingHandler.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON);
177         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
178
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))));
184         });
185
186         // cool white
187         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.ZERO);
188         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
189
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))));
199         });
200
201         // warm white
202         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, PercentType.HUNDRED);
203         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
204
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))));
214         });
215
216         // intermediate white
217         dimmerThingHandler.handleCommand(CHANNEL_UID_COLOR_TEMP, new PercentType(75));
218         currentTime = dmxBridgeHandler.calcBuffer(currentTime, TEST_FADE_TIME);
219
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))));
229         });
230     }
231 }