]> git.basschouten.com Git - openhab-addons.git/blob
35a21e546b82e4339da47031d17cad24abe31f29
[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.thermostat;
14
15 import static org.junit.jupiter.api.Assertions.assertSame;
16 import static org.mockito.ArgumentMatchers.eq;
17 import static org.mockito.Mockito.verify;
18
19 import java.util.concurrent.ExecutionException;
20 import java.util.concurrent.TimeoutException;
21
22 import javax.measure.quantity.Temperature;
23
24 import org.eclipse.jdt.annotation.NonNullByDefault;
25 import org.junit.jupiter.api.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.mockito.Captor;
28 import org.openhab.binding.boschshc.internal.devices.AbstractBatteryPoweredDeviceHandlerTest;
29 import org.openhab.binding.boschshc.internal.devices.BoschSHCBindingConstants;
30 import org.openhab.binding.boschshc.internal.exceptions.BoschSHCException;
31 import org.openhab.binding.boschshc.internal.services.childlock.dto.ChildLockServiceState;
32 import org.openhab.binding.boschshc.internal.services.childlock.dto.ChildLockState;
33 import org.openhab.binding.boschshc.internal.services.silentmode.SilentModeState;
34 import org.openhab.binding.boschshc.internal.services.silentmode.dto.SilentModeServiceState;
35 import org.openhab.core.library.types.DecimalType;
36 import org.openhab.core.library.types.OnOffType;
37 import org.openhab.core.library.types.QuantityType;
38 import org.openhab.core.library.unit.SIUnits;
39 import org.openhab.core.thing.ChannelUID;
40 import org.openhab.core.thing.ThingStatus;
41 import org.openhab.core.thing.ThingStatusDetail;
42 import org.openhab.core.thing.ThingStatusInfo;
43 import org.openhab.core.thing.ThingTypeUID;
44 import org.openhab.core.thing.binding.builder.ThingStatusInfoBuilder;
45
46 import com.google.gson.JsonElement;
47 import com.google.gson.JsonParser;
48
49 /**
50  * Unit Tests for {@link ThermostatHandler}.
51  *
52  * @author David Pace - Initial contribution
53  *
54  */
55 @NonNullByDefault
56 class ThermostatHandlerTest extends AbstractBatteryPoweredDeviceHandlerTest<ThermostatHandler> {
57
58     private @Captor @NonNullByDefault({}) ArgumentCaptor<ChildLockServiceState> childLockServiceStateCaptor;
59
60     private @Captor @NonNullByDefault({}) ArgumentCaptor<SilentModeServiceState> silentModeServiceStateCaptor;
61
62     @Override
63     protected ThermostatHandler createFixture() {
64         return new ThermostatHandler(getThing());
65     }
66
67     @Override
68     protected String getDeviceID() {
69         return "hdm:ZigBee:000d6f0017f1ace2";
70     }
71
72     @Override
73     protected ThingTypeUID getThingTypeUID() {
74         return BoschSHCBindingConstants.THING_TYPE_THERMOSTAT;
75     }
76
77     @Test
78     void testHandleCommandChildLockService()
79             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
80         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
81                 OnOffType.ON);
82         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("Thermostat"), childLockServiceStateCaptor.capture());
83         ChildLockServiceState state = childLockServiceStateCaptor.getValue();
84         assertSame(ChildLockState.ON, state.childLock);
85     }
86
87     @Test
88     void testHandleCommandSilentModeService()
89             throws InterruptedException, TimeoutException, ExecutionException, BoschSHCException {
90         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE),
91                 OnOffType.ON);
92         verify(getBridgeHandler()).putState(eq(getDeviceID()), eq("SilentMode"),
93                 silentModeServiceStateCaptor.capture());
94         SilentModeServiceState state = silentModeServiceStateCaptor.getValue();
95         assertSame(SilentModeState.MODE_SILENT, state.mode);
96     }
97
98     @Test
99     void testHandleCommandUnknownCommandChildLockService() {
100         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK),
101                 new DecimalType(42));
102         ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
103                 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
104                 .withDescription(
105                         "Error when service Thermostat should handle command org.openhab.core.library.types.DecimalType: Thermostat: Can not handle command org.openhab.core.library.types.DecimalType")
106                 .build();
107         verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
108     }
109
110     @Test
111     void testHandleCommandUnknownCommandSilentModeService() {
112         getFixture().handleCommand(new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE),
113                 new DecimalType(42));
114         ThingStatusInfo expectedThingStatusInfo = ThingStatusInfoBuilder
115                 .create(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR)
116                 .withDescription(
117                         "Error when service SilentMode should handle command org.openhab.core.library.types.DecimalType: SilentMode: Can not handle command org.openhab.core.library.types.DecimalType")
118                 .build();
119         verify(getCallback()).statusUpdated(getThing(), expectedThingStatusInfo);
120     }
121
122     @Test
123     void testUpdateChannelsTemperatureLevelService() {
124         JsonElement jsonObject = JsonParser.parseString("""
125                 {
126                    "@type": "temperatureLevelState",
127                    "temperature": 21.5
128                  }\
129                 """);
130         getFixture().processUpdate("TemperatureLevel", jsonObject);
131         verify(getCallback()).stateUpdated(
132                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_TEMPERATURE),
133                 new QuantityType<Temperature>(21.5, SIUnits.CELSIUS));
134     }
135
136     @Test
137     void testUpdateChannelsValveTappetService() {
138         JsonElement jsonObject = JsonParser
139                 .parseString("{\n" + "   \"@type\": \"valveTappetState\",\n" + "   \"position\": 42\n" + " }");
140         getFixture().processUpdate("ValveTappet", jsonObject);
141         verify(getCallback()).stateUpdated(
142                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_VALVE_TAPPET_POSITION),
143                 new DecimalType(42));
144     }
145
146     @Test
147     void testUpdateChannelsChildLockService() {
148         JsonElement jsonObject = JsonParser
149                 .parseString("{\n" + "   \"@type\": \"childLockState\",\n" + "   \"childLock\": \"ON\"\n" + " }");
150         getFixture().processUpdate("Thermostat", jsonObject);
151         verify(getCallback()).stateUpdated(
152                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_CHILD_LOCK), OnOffType.ON);
153     }
154
155     @Test
156     void testUpdateChannelsSilentModeService() {
157         JsonElement jsonObject = JsonParser.parseString("{\"@type\": \"silentModeState\", \"mode\": \"MODE_SILENT\"}");
158         getFixture().processUpdate("SilentMode", jsonObject);
159         verify(getCallback()).stateUpdated(
160                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE), OnOffType.ON);
161     }
162
163     @Test
164     void testUpdateChannelsSilentModeServiceNormal() {
165         JsonElement jsonObject = JsonParser.parseString("{\"@type\": \"silentModeState\", \"mode\": \"MODE_NORMAL\"}");
166         getFixture().processUpdate("SilentMode", jsonObject);
167         verify(getCallback()).stateUpdated(
168                 new ChannelUID(getThing().getUID(), BoschSHCBindingConstants.CHANNEL_SILENT_MODE), OnOffType.OFF);
169     }
170 }