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.DISHWASHER_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.Quantity;
32 import org.openhab.binding.mielecloud.internal.webservice.api.json.StateType;
33 import org.openhab.core.library.types.DecimalType;
34 import org.openhab.core.library.types.OnOffType;
35 import org.openhab.core.library.types.QuantityType;
36 import org.openhab.core.library.types.StringType;
37 import org.openhab.core.library.unit.Units;
40 * @author Björn Lange - Initial contribution
41 * @author Benjamin Bolte - Add info state channel and map signal flags from API tests
42 * @author Björn Lange - Add elapsed time, current water and energy consumption channels
45 public class DishwasherDeviceThingHandlerTest extends AbstractMieleThingHandlerTest {
47 protected AbstractMieleThingHandler setUpThingHandler() {
48 return createThingHandler(MieleCloudBindingConstants.THING_TYPE_DISHWASHER, DISHWASHER_DEVICE_THING_UID,
49 DishwasherDeviceThingHandler.class, MieleCloudBindingIntegrationTestConstants.SERIAL_NUMBER, "1");
53 public void testChannelUpdatesForNullValues() throws Exception {
55 setUpBridgeAndThing();
57 DeviceState deviceState = mock(DeviceState.class);
58 when(deviceState.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
59 when(deviceState.getStateType()).thenReturn(Optional.empty());
60 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.empty());
61 when(deviceState.getSelectedProgram()).thenReturn(Optional.empty());
62 when(deviceState.getSelectedProgramId()).thenReturn(Optional.empty());
63 when(deviceState.getProgramPhase()).thenReturn(Optional.empty());
64 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.empty());
65 when(deviceState.getStatus()).thenReturn(Optional.empty());
66 when(deviceState.getStatusRaw()).thenReturn(Optional.empty());
67 when(deviceState.getStartTime()).thenReturn(Optional.empty());
68 when(deviceState.getElapsedTime()).thenReturn(Optional.empty());
69 when(deviceState.getDoorState()).thenReturn(Optional.empty());
70 when(deviceState.getCurrentWaterConsumption()).thenReturn(Optional.empty());
71 when(deviceState.getCurrentEnergyConsumption()).thenReturn(Optional.empty());
74 getBridgeHandler().onDeviceStateUpdated(deviceState);
78 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE));
79 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ACTIVE_RAW));
80 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE));
81 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PHASE_RAW));
82 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE));
83 assertEquals(NULL_VALUE_STATE, getChannelState(OPERATION_STATE_RAW));
84 assertEquals(new StringType(ProgramStatus.PROGRAM_STOPPED.getState()), getChannelState(PROGRAM_START_STOP));
85 assertEquals(new StringType(PowerStatus.POWER_ON.getState()), getChannelState(POWER_ON_OFF));
86 assertEquals(NULL_VALUE_STATE, getChannelState(DELAYED_START_TIME));
87 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_ELAPSED_TIME));
88 assertEquals(NULL_VALUE_STATE, getChannelState(DOOR_STATE));
89 assertEquals(NULL_VALUE_STATE, getChannelState(WATER_CONSUMPTION_CURRENT));
90 assertEquals(NULL_VALUE_STATE, getChannelState(ENERGY_CONSUMPTION_CURRENT));
95 public void testChannelUpdatesForValidValues() throws Exception {
97 setUpBridgeAndThing();
99 DeviceState deviceState = mock(DeviceState.class);
100 when(deviceState.isInState(any())).thenCallRealMethod();
101 when(deviceState.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
102 when(deviceState.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
103 when(deviceState.isRemoteControlEnabled()).thenReturn(Optional.of(true));
104 when(deviceState.getSelectedProgram()).thenReturn(Optional.of("Eco"));
105 when(deviceState.getSelectedProgramId()).thenReturn(Optional.of(4L));
106 when(deviceState.getProgramPhase()).thenReturn(Optional.of("Spülen"));
107 when(deviceState.getProgramPhaseRaw()).thenReturn(Optional.of(2));
108 when(deviceState.getStatus()).thenReturn(Optional.of("Running"));
109 when(deviceState.getStatusRaw()).thenReturn(Optional.of(StateType.RUNNING.getCode()));
110 when(deviceState.getStartTime()).thenReturn(Optional.of(3600));
111 when(deviceState.getElapsedTime()).thenReturn(Optional.of(4));
112 when(deviceState.hasError()).thenReturn(false);
113 when(deviceState.hasInfo()).thenReturn(true);
114 when(deviceState.getDoorState()).thenReturn(Optional.of(true));
115 when(deviceState.getCurrentWaterConsumption()).thenReturn(Optional.of(new Quantity(1.0, "l")));
116 when(deviceState.getCurrentEnergyConsumption()).thenReturn(Optional.of(new Quantity(2.5, "kWh")));
119 getBridgeHandler().onDeviceStateUpdated(deviceState);
122 waitForAssert(() -> {
123 assertEquals(new StringType("Eco"), getChannelState(PROGRAM_ACTIVE));
124 assertEquals(new DecimalType(4), getChannelState(PROGRAM_ACTIVE_RAW));
125 assertEquals(new StringType("Spülen"), getChannelState(PROGRAM_PHASE));
126 assertEquals(new DecimalType(2), 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(4), getChannelState(PROGRAM_ELAPSED_TIME));
133 assertEquals(OnOffType.OFF, getChannelState(ERROR_STATE));
134 assertEquals(OnOffType.ON, getChannelState(INFO_STATE));
135 assertEquals(OnOffType.ON, getChannelState(DOOR_STATE));
136 assertEquals(new QuantityType<>(1.0, Units.LITRE), getChannelState(WATER_CONSUMPTION_CURRENT));
137 assertEquals(new QuantityType<>(2.5, Units.KILOWATT_HOUR), getChannelState(ENERGY_CONSUMPTION_CURRENT));
142 public void testFinishStateChannelIsSetToOnWhenProgramHasFinished() throws Exception {
144 setUpBridgeAndThing();
146 DeviceState deviceStateBefore = mock(DeviceState.class);
147 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
148 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
149 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
151 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
153 DeviceState deviceStateAfter = mock(DeviceState.class);
154 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
155 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.END_PROGRAMMED));
156 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
159 getBridgeHandler().onDeviceStateUpdated(deviceStateAfter);
162 waitForAssert(() -> {
163 assertEquals(OnOffType.ON, getChannelState(FINISH_STATE));
168 public void testTransitionChannelUpdatesForNullValues() throws Exception {
170 setUpBridgeAndThing();
172 DeviceState deviceStateBefore = mock(DeviceState.class);
173 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
174 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
175 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
176 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.empty());
177 when(deviceStateBefore.getProgress()).thenReturn(Optional.empty());
179 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
181 DeviceState deviceStateAfter = mock(DeviceState.class);
182 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
183 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
184 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
185 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.empty());
186 when(deviceStateAfter.getProgress()).thenReturn(Optional.empty());
189 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
191 waitForAssert(() -> {
192 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_REMAINING_TIME));
193 assertEquals(NULL_VALUE_STATE, getChannelState(PROGRAM_PROGRESS));
198 public void testTransitionChannelUpdatesForValidValues() throws Exception {
200 setUpBridgeAndThing();
202 DeviceState deviceStateBefore = mock(DeviceState.class);
203 when(deviceStateBefore.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
204 when(deviceStateBefore.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
205 when(deviceStateBefore.isInState(any())).thenCallRealMethod();
206 when(deviceStateBefore.getRemainingTime()).thenReturn(Optional.of(10));
207 when(deviceStateBefore.getProgress()).thenReturn(Optional.of(80));
209 getThingHandler().onDeviceStateUpdated(deviceStateBefore);
211 DeviceState deviceStateAfter = mock(DeviceState.class);
212 when(deviceStateAfter.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
213 when(deviceStateAfter.getStateType()).thenReturn(Optional.of(StateType.RUNNING));
214 when(deviceStateAfter.isInState(any())).thenCallRealMethod();
215 when(deviceStateAfter.getRemainingTime()).thenReturn(Optional.of(10));
216 when(deviceStateAfter.getProgress()).thenReturn(Optional.of(80));
219 getThingHandler().onDeviceStateUpdated(deviceStateAfter);
221 waitForAssert(() -> {
222 assertEquals(new DecimalType(10), getChannelState(PROGRAM_REMAINING_TIME));
223 assertEquals(new DecimalType(80), getChannelState(PROGRAM_PROGRESS));
228 public void testActionsChannelUpdatesForValidValues() throws Exception {
230 setUpBridgeAndThing();
232 ActionsState actionsState = mock(ActionsState.class);
233 when(actionsState.getDeviceIdentifier()).thenReturn(DISHWASHER_DEVICE_THING_UID.getId());
234 when(actionsState.canBeStarted()).thenReturn(true);
235 when(actionsState.canBeStopped()).thenReturn(false);
236 when(actionsState.canBeSwitchedOn()).thenReturn(true);
237 when(actionsState.canBeSwitchedOff()).thenReturn(false);
240 getBridgeHandler().onProcessActionUpdated(actionsState);
243 waitForAssert(() -> {
244 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_STARTED));
245 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_STOPPED));
246 assertEquals(OnOffType.ON, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_ON));
247 assertEquals(OnOffType.OFF, getChannelState(REMOTE_CONTROL_CAN_BE_SWITCHED_OFF));