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