2 * Copyright (c) 2010-2023 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.mielecloud.internal.handler;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.any;
17 import static org.mockito.Mockito.*;
18 import static org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants.Channels.*;
19 import static org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants.DRYER_DEVICE_THING_UID;
21 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.junit.jupiter.api.Test;
25 import org.openhab.binding.mielecloud.internal.MieleCloudBindingConstants;
26 import org.openhab.binding.mielecloud.internal.util.MieleCloudBindingIntegrationTestConstants;
27 import org.openhab.binding.mielecloud.internal.webservice.api.ActionsState;
28 import org.openhab.binding.mielecloud.internal.webservice.api.DeviceState;
29 import org.openhab.binding.mielecloud.internal.webservice.api.PowerStatus;
30 import org.openhab.binding.mielecloud.internal.webservice.api.ProgramStatus;
31 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
32 import org.openhab.core.library.types.DecimalType;
33 import org.openhab.core.library.types.OnOffType;
34 import org.openhab.core.library.types.StringType;
37 * @author Björn Lange - Initial contribution
38 * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
39 * @author Björn Lange - Add elapsed time channel
42 public class DryerDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
44 protected AbstractMieleThingHandler setUpThingHandler() {
45 return createThingHandler(MieleCloudBindingConstants.THING_TYPE_DRYER, DRYER_DEVICE_THING_UID,
46 DryerDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER);
50 public void testChannelUpdatesForNullValues() throws Exception {
52 setUpBridgeAndThing();
54 DeviceState deviceState = mock(DeviceState.class);
55 when(deviceState.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
56 when(deviceState.getStateType()).thenReturn(Optional.empty());
57 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
58 when(deviceState.getSelectedProgram()).thenReturn(Optional.empty());
59 when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
60 when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
61 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
62 when(deviceState.getStatus()).thenReturn(Optional.empty());
63 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
64 when(deviceState.getStartTime()).thenReturn(Optional.empty());
65 when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
66 when(deviceState.getDryingTarget()).thenReturn(Optional.empty());
67 when(deviceState.getDryingTargetRaw()).thenReturn(Optional.empty());
68 when(deviceState.getLightState()).thenReturn(Optional.empty());
69 when(deviceState.getDoorState()).thenReturn(Optional.empty());
72 getBridgeHandler().onDeviceStateUpdated(deviceState);
76 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE));
77 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE_RAW));
78 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
79 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
80 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
81 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
82 assertEquals(new StringType(ProgramStatus.PROGRAM_STOPPED.getState()), getChannelState(PROGRAM_START_STOP));
83 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
84 assertEquals(NULL_VALUE_STATE, getChannelState(DELAYED_START_TIME));
85 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
86 assertEquals(NULL_VALUE_STATE, getChannelState(DRYING_TARGET));
87 assertEquals(NULL_VALUE_STATE, getChannelState(DRYING_TARGET_RAW));
88 assertEquals(NULL_VALUE_STATE, getChannelState(LIGHT_SWITCH));
89 assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
94 public void testChannelUpdatesForValidValues() throws Exception {
96 setUpBridgeAndThing();
98 DeviceState deviceState = mock(DeviceState.class);
99 when(deviceState.isInState(any())).thenCallRealMethod();
100 when(deviceState.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
101 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
102 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
103 when(deviceState.getSelectedProgram()).thenReturn(Optional.of("Baumwolle"));
104 when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(34L));
105 when(deviceState.getProgramPhase()).thenReturn(Optional.of("Schleudern"));
106 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(3));
107 when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
108 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
109 when(deviceState.getStartTime()).thenReturn(Optional.of(3600));
110 when(deviceState.getElapsedTime()).thenReturn(Optional.of(61));
111 when(deviceState.getDryingTarget()).thenReturn(Optional.of("Schranktrocken"));
112 when(deviceState.getDryingTargetRaw()).thenReturn(Optional.of(3));
113 when(deviceState.hasError()).thenReturn(true);
114 when(deviceState.hasInfo()).thenReturn(true);
115 when(deviceState.getLightState()).thenReturn(Optional.of(false));
116 when(deviceState.getDoorState()).thenReturn(Optional.of(false));
119 getBridgeHandler().onDeviceStateUpdated(deviceState);
122 waitForAssert(() -> {
123 assertEquals(new StringType("Baumwolle"), getChannelState(PROGRAM_ACTIVE));
124 assertEquals(new DecimalType(34), getChannelState(PROGRAM_ACTIVE_RAW));
125 assertEquals(new StringType("Schleudern"), getChannelState(PROGRAM_PHASE));
126 assertEquals(new DecimalType(3), getChannelState(PROGRAM_PHASE_RAW));
127 assertEquals(new StringType("Running"), getChannelState(OPERATION_STATE));
128 assertEquals(new DecimalType(StateType.RUNNING.getCode()), getChannelState(OPERATION_STATE_RAW));
129 assertEquals(new StringType(ProgramStatus.PROGRAM_STARTED.getState()), getChannelState(PROGRAM_START_STOP));
130 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
131 assertEquals(new DecimalType(3600), getChannelState(DELAYED_START_TIME));
132 assertEquals(new DecimalType(61), getChannelState(PROGRAM_ELAPSED_TIME));
133 assertEquals(new StringType("Schranktrocken"), getChannelState(DRYING_TARGET));
134 assertEquals(new DecimalType(3), getChannelState(DRYING_TARGET_RAW));
135 assertEquals(OnOffType.ON, getChannelState(ERROR_STATE));
136 assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
137 assertEquals(OnOffType.OFF, getChannelState(LIGHT_SWITCH));
138 assertEquals(OnOffType.OFF, getChannelState(DOOR_STATE));
143 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
145 setUpBridgeAndThing();
147 DeviceState deviceStateBefore = mock(DeviceState.class);
148 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
149 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
150 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
152 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
154 DeviceState deviceStateAfter = mock(DeviceState.class);
155 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
156 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
157 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
160 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
163 waitForAssert(() -> {
164 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
169 public void testTransitionChannelUpdatesForNullValues() throws Exception {
171 setUpBridgeAndThing();
173 DeviceState deviceStateBefore = mock(DeviceState.class);
174 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
175 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
176 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
177 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
178 when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
180 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
182 DeviceState deviceStateAfter = mock(DeviceState.class);
183 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
184 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
185 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
186 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
187 when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
190 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
192 waitForAssert(() -> {
193 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
194 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
199 public void testTransitionChannelUpdatesForValidValues() throws Exception {
201 setUpBridgeAndThing();
203 DeviceState deviceStateBefore = mock(DeviceState.class);
204 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
205 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
206 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
207 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
208 when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
210 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
212 DeviceState deviceStateAfter = mock(DeviceState.class);
213 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
214 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
215 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
216 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
217 when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
220 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
222 waitForAssert(() -> {
223 assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
224 assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
229 public void testActionsChannelUpdatesForValidValues() throws Exception {
231 setUpBridgeAndThing();
233 ActionsState actionsState = mock(ActionsState.class);
234 when(actionsState.getDeviceIdentifier()).thenReturn(DRYER_DEVICE_THING_UID.getId());
235 when(actionsState.canBeStarted()).thenReturn(true);
236 when(actionsState.canBeStopped()).thenReturn(false);
237 when(actionsState.canBeSwitchedOn()).thenReturn(true);
238 when(actionsState.canBeSwitchedOff()).thenReturn(false);
239 when(actionsState.canControlLight()).thenReturn(true);
242 getBridgeHandler().onProcessActionUpdated(actionsState);
245 waitForAssert(() -> {
246 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_STARTED));
247 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_STOPPED));
248 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
249 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));
250 assertEquals(OnOffType.ON, getChannelState(LIGHT_CAN_BE_CONTROLLED));