]> git.basschouten.com Git - openhab-addons.git/blob
bc7c3d97798a5be9efb0334bb07d3a5f18b59928
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.tplinksmarthome.internal.device;
14
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.junit.jupiter.api.Assertions.assertSame;
17 import static org.junit.jupiter.api.Assertions.assertTrue;
18 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_BRIGHTNESS;
19 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_COLOR;
20 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_COLOR_TEMPERATURE;
21 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_COLOR_TEMPERATURE_ABS;
22 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_ENERGY_POWER;
23 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_OTHER;
24 import static org.openhab.binding.tplinksmarthome.internal.ChannelUIDConstants.CHANNEL_UID_SWITCH;
25 import static org.openhab.binding.tplinksmarthome.internal.TPLinkSmartHomeThingType.LB130;
26
27 import java.io.IOException;
28
29 import org.eclipse.jdt.annotation.NonNullByDefault;
30 import org.junit.jupiter.api.BeforeEach;
31 import org.junit.jupiter.api.Test;
32 import org.openhab.binding.tplinksmarthome.internal.model.ModelTestUtil;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.HSBType;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.library.types.PercentType;
37 import org.openhab.core.types.UnDefType;
38
39 /**
40  * Test class for {@link BulbDevice} class.
41  *
42  * @author Hilbrand Bouwkamp - Initial contribution
43  */
44 @NonNullByDefault
45 public class BulbDeviceTest extends DeviceTestBase<BulbDevice> {
46
47     private static final String DEVICE_OFF = "bulb_get_sysinfo_response_off";
48
49     public BulbDeviceTest() throws IOException {
50         super(new BulbDevice(LB130), "bulb_get_sysinfo_response_on");
51     }
52
53     @BeforeEach
54     @Override
55     public void setUp() throws IOException {
56         super.setUp();
57         setSocketReturnAssert("bulb_transition_light_state_response");
58     }
59
60     @Test
61     public void testHandleCommandBrightness() throws IOException {
62         assertInput("bulb_transition_light_state_brightness");
63         assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, new PercentType(33)),
64                 "Brightness channel should be handled");
65     }
66
67     @Test
68     public void testHandleCommandBrightnessOnOff() throws IOException {
69         assertInput("bulb_transition_light_state_on");
70         assertTrue(device.handleCommand(CHANNEL_UID_BRIGHTNESS, OnOffType.ON),
71                 "Brightness channel with OnOff state should be handled");
72     }
73
74     @Test
75     public void testHandleCommandColor() throws IOException {
76         assertInput("bulb_transition_light_state_color");
77         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new HSBType("55,44,33")), "Color channel should be handled");
78     }
79
80     public void testHandleCommandColorBrightness() throws IOException {
81         assertInput("bulb_transition_light_state_brightness");
82         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, new PercentType(33)),
83                 "Color channel with Percentage state (=brightness) should be handled");
84     }
85
86     public void testHandleCommandColorOnOff() throws IOException {
87         assertInput("bulb_transition_light_state_on");
88         assertTrue(device.handleCommand(CHANNEL_UID_COLOR, OnOffType.ON),
89                 "Color channel with OnOff state should be handled");
90     }
91
92     @Test
93     public void testHandleCommandColorTemperature() throws IOException {
94         assertInput("bulb_transition_light_state_color_temp");
95         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, new PercentType(40)),
96                 "Color temperature channel should be handled");
97     }
98
99     @Test
100     public void testHandleCommandColorTemperatureAbs() throws IOException {
101         assertInput("bulb_transition_light_state_color_temp");
102         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE_ABS, new DecimalType(5100)),
103                 "Color temperature channel should be handled");
104     }
105
106     @Test
107     public void testHandleCommandColorTemperatureOnOff() throws IOException {
108         assertInput("bulb_transition_light_state_on");
109         assertTrue(device.handleCommand(CHANNEL_UID_COLOR_TEMPERATURE, OnOffType.ON),
110                 "Color temperature channel with OnOff state should be handled");
111     }
112
113     @Test
114     public void testHandleCommandSwitch() throws IOException {
115         assertInput("bulb_transition_light_state_on");
116         assertTrue(device.handleCommand(CHANNEL_UID_SWITCH, OnOffType.ON), "Switch channel should be handled");
117     }
118
119     @Test
120     public void testUpdateChannelBrightnessOn() {
121         assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
122                 "Brightness should be on");
123     }
124
125     @Test
126     public void testUpdateChannelBrightnessOff() throws IOException {
127         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
128         assertEquals(PercentType.ZERO, device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
129                 "Brightness should be off");
130     }
131
132     @Test
133     public void testUpdateChannelColorOn() {
134         assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
135                 "Color should be on");
136     }
137
138     @Test
139     public void testUpdateChannelColorOff() throws IOException {
140         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
141         assertEquals(new HSBType("7,44,0"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
142                 "Color should be off");
143     }
144
145     @Test
146     public void testUpdateChannelSwitchOn() {
147         assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
148     }
149
150     @Test
151     public void testUpdateChannelSwitchOff() throws IOException {
152         deviceState = new DeviceState(ModelTestUtil.readJson(DEVICE_OFF));
153         assertSame(OnOffType.OFF, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be off");
154     }
155
156     @Test
157     public void testUpdateChannelColorTemperature() {
158         assertEquals(new PercentType(2), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState),
159                 "Color temperature should be set");
160     }
161
162     @Test
163     public void testUpdateChannelColorTemperatureAbs() {
164         assertEquals(new DecimalType(2630), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState),
165                 "Color temperature should be set");
166     }
167
168     @Test
169     public void testUpdateChannelOther() {
170         assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
171                 "Unknown channel should return UNDEF");
172     }
173
174     @Test
175     public void testUpdateChannelPower() {
176         assertEquals(new DecimalType(10.8), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
177                 "Power values should be set");
178     }
179 }