2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.tplinksmarthome.internal.device;
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;
20 import java.io.IOException;
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;
33 * Test class for {@link BulbDevice} class.
35 * @author Hilbrand Bouwkamp - Initial contribution
38 public class BulbDeviceTest extends DeviceTestBase<BulbDevice> {
40 private static final String DEVICE_OFF = "bulb_get_sysinfo_response_off";
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");
49 public void setUp() throws IOException {
51 setSocketReturnAssert("bulb_transition_light_state_response");
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");
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");
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");
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");
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");
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");
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");
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");
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");
114 public void testUpdateChannelBrightnessOn() {
115 assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
116 "Brightness should be on");
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");
127 public void testUpdateChannelColorOn() {
128 assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
129 "Color should be on");
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");
140 public void testUpdateChannelSwitchOn() {
141 assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
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");
151 public void testUpdateChannelColorTemperature() {
152 assertEquals(new PercentType(2), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState),
153 "Color temperature should be set");
157 public void testUpdateChannelColorTemperatureAbs() {
158 assertEquals(new DecimalType(2630), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState),
159 "Color temperature should be set");
163 public void testUpdateChannelOther() {
164 assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
165 "Unknown channel should return UNDEF");
169 public void testUpdateChannelPower() {
170 assertEquals(new DecimalType(10.8), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
171 "Power values should be set");