]> git.basschouten.com Git - openhab-addons.git/blob
8662d5da058c72679dd0a8cf1c47b4aed400becd
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.boschshc.internal.devices.smartbulb;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.*;
18
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
22 import org.junit.jupiter.api.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.openhab.binding.boschshc.internal.devices.AbstractBoschSHCDeviceHandlerTest;
26 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
27 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
28 import org.openhab.binding.boschshc.internal.services.binaryswitch.dto.BinarySwitchServiceState;
29 import org.openhab.binding.boschshc.internal.services.hsbcoloractuator.dto.HSBColorActuatorServiceState;
30 import org.openhab.binding.boschshc.internal.services.multilevelswitch.dto.MultiLevelSwitchServiceState;
31 import org.openhab.core.library.types.HSBType;
32 import org.openhab.core.library.types.OnOffType;
33 import org.openhab.core.library.types.PercentType;
34 import org.openhab.core.thing.ChannelUID;
35 import org.openhab.core.thing.ThingTypeUID;
36 import org.openhab.core.thing.ThingUID;
37
38 import com.google.gson.JsonElement;
39 import com.google.gson.JsonParser;
40
41 /**
42  * Unit tests for {@link SmartBulbHandler}.
43  *
44  * @author David Pace - Initial contribution
45  *
46  */
47 public class SmartBulbHandlerTest extends AbstractBoschSHCDeviceHandlerTest<SmartBulbHandler> {
48
49     @Captor
50     private ArgumentCaptor<BinarySwitchServiceState> binarySwitchServiceStateCaptor;
51
52     @Captor
53     private ArgumentCaptor<MultiLevelSwitchServiceState> multiLevelSwitchServiceStateCaptor;
54
55     @Captor
56     private ArgumentCaptor<HSBColorActuatorServiceState> hsbColorActuatorServiceStateCaptor;
57
58     @Override
59     protected SmartBulbHandler createFixture() {
60         return new SmartBulbHandler(getThing());
61     }
62
63     @Override
64     protected String getDeviceID() {
65         return "hdm:ZigBee:f0d1b80000f2a3e9";
66     }
67
68     @Override
69     protected ThingTypeUID getThingTypeUID() {
70         return BoschSHCBindingConstants.THING_TYPE_SMART_BULB;
71     }
72
73     @Test
74     public void testHandleCommand_BinarySwitch()
75             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
76
77         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH),
78                 OnOffType.ON);
79         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("BinarySwitch"),
80                 binarySwitchServiceStateCaptor.capture());
81         BinarySwitchServiceState state = binarySwitchServiceStateCaptor.getValue();
82         assertTrue(state.on);
83
84         getFixture().handleCommand(new ChannelUID(new ThingUID(getThingTypeUID(), "abcdef"),
85                 BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
86         verify(getBridgeHandler(), times(2)).putState(eq(getDeviceID()), eq("BinarySwitch"),
87                 binarySwitchServiceStateCaptor.capture());
88         state = binarySwitchServiceStateCaptor.getValue();
89         assertFalse(state.on);
90     }
91
92     @Test
93     public void testHandleCommand_MultiLevelSwitch()
94             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
95
96         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BRIGHTNESS),
97                 new PercentType(42));
98         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("MultiLevelSwitch"),
99                 multiLevelSwitchServiceStateCaptor.capture());
100         MultiLevelSwitchServiceState state = multiLevelSwitchServiceStateCaptor.getValue();
101         assertEquals(42, state.level);
102     }
103
104     @Test
105     public void testHandleCommand_HSBColorActuator()
106             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
107
108         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
109                 HSBType.BLUE);
110         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("HSBColorActuator"),
111                 hsbColorActuatorServiceStateCaptor.capture());
112         HSBColorActuatorServiceState state = hsbColorActuatorServiceStateCaptor.getValue();
113         assertEquals(-16776961, state.rgb);
114     }
115
116     @Test
117     public void testUpdateChannel_BinarySwitchState() {
118         JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"binarySwitchState\",\"on\":true}");
119         getFixture().processUpdate("BinarySwitch", jsonObject);
120         verify(getCallback()).stateUpdated(
121                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.ON);
122
123         jsonObject = JsonParser.parseString("{\"@type\":\"binarySwitchState\",\"on\":false}");
124         getFixture().processUpdate("BinarySwitch", jsonObject);
125         verify(getCallback()).stateUpdated(
126                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_POWER_SWITCH), OnOffType.OFF);
127     }
128
129     @Test
130     public void testUpdateChannel_MultiLevelSwitchState() {
131         JsonElement jsonObject = JsonParser.parseString("{\"@type\":\"multiLevelSwitchState\",\"level\":16}");
132         getFixture().processUpdate("MultiLevelSwitch", jsonObject);
133         verify(getCallback()).stateUpdated(
134                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_BRIGHTNESS), new PercentType(16));
135     }
136
137     @Test
138     public void testUpdateChannel_HSBColorActuatorState() {
139         JsonElement jsonObject = JsonParser.parseString("{\"colorTemperatureRange\": {\n" + "        \"minCt\": 153,\n"
140                 + "        \"maxCt\": 526\n" + "    },\n" + "    \"@type\": \"colorState\",\n"
141                 + "    \"gamut\": \"LEDVANCE_GAMUT_A\",\n" + "    \"rgb\": -12427}");
142         getFixture().processUpdate("HSBColorActuator", jsonObject);
143         verify(getCallback()).stateUpdated(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_COLOR),
144                 HSBType.fromRGB(255, 207, 117));
145     }
146 }