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