2 * Copyright (c) 2010-2022 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.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;
27 import java.io.IOException;
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;
40 * Test class for {@link BulbDevice} class.
42 * @author Hilbrand Bouwkamp - Initial contribution
45 public class BulbDeviceTest extends DeviceTestBase<BulbDevice> {
47 private static final String DEVICE_OFF = "bulb_get_sysinfo_response_off";
49 public BulbDeviceTest() throws IOException {
50 super(new BulbDevice(LB130), "bulb_get_sysinfo_response_on");
55 public void setUp() throws IOException {
57 setSocketReturnAssert("bulb_transition_light_state_response");
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");
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");
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");
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");
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");
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");
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");
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");
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");
120 public void testUpdateChannelBrightnessOn() {
121 assertEquals(new PercentType(92), device.updateChannel(CHANNEL_UID_BRIGHTNESS, deviceState),
122 "Brightness should be on");
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");
133 public void testUpdateChannelColorOn() {
134 assertEquals(new HSBType("7,44,92"), device.updateChannel(CHANNEL_UID_COLOR, deviceState),
135 "Color should be on");
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");
146 public void testUpdateChannelSwitchOn() {
147 assertSame(OnOffType.ON, device.updateChannel(CHANNEL_UID_SWITCH, deviceState), "Switch should be on");
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");
157 public void testUpdateChannelColorTemperature() {
158 assertEquals(new PercentType(2), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE, deviceState),
159 "Color temperature should be set");
163 public void testUpdateChannelColorTemperatureAbs() {
164 assertEquals(new DecimalType(2630), device.updateChannel(CHANNEL_UID_COLOR_TEMPERATURE_ABS, deviceState),
165 "Color temperature should be set");
169 public void testUpdateChannelOther() {
170 assertSame(UnDefType.UNDEF, device.updateChannel(CHANNEL_UID_OTHER, deviceState),
171 "Unknown channel should return UNDEF");
175 public void testUpdateChannelPower() {
176 assertEquals(new DecimalType(10.8), device.updateChannel(CHANNEL_UID_ENERGY_POWER, deviceState),
177 "Power values should be set");