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