2 * Copyright (c) 2010-2021 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.homeconnect.internal.handler;
15 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
17 import java.util.Arrays;
18 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient;
23 import org.openhab.binding.homeconnect.internal.client.exception.ApplianceOfflineException;
24 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
25 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
26 import org.openhab.binding.homeconnect.internal.type.HomeConnectDynamicStateDescriptionProvider;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.ChannelUID;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.types.Command;
31 import org.openhab.core.types.UnDefType;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link HomeConnectWasherHandler} is responsible for handling commands, which are
37 * sent to one of the channels of a washing machine.
39 * @author Jonas BrĂ¼stel - Initial contribution
42 public class HomeConnectWasherHandler extends AbstractHomeConnectThingHandler {
44 private static final List<String> INACTIVE_STATE = Arrays.asList(OPERATION_STATE_INACTIVE, OPERATION_STATE_READY);
46 private final Logger logger = LoggerFactory.getLogger(HomeConnectWasherHandler.class);
48 public HomeConnectWasherHandler(Thing thing,
49 HomeConnectDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
50 super(thing, dynamicStateDescriptionProvider);
54 protected void configureChannelUpdateHandlers(Map<String, ChannelUpdateHandler> handlers) {
55 // register default update handlers
56 handlers.put(CHANNEL_DOOR_STATE, defaultDoorStateChannelUpdateHandler());
57 handlers.put(CHANNEL_OPERATION_STATE, defaultOperationStateChannelUpdateHandler());
58 handlers.put(CHANNEL_REMOTE_CONTROL_ACTIVE_STATE, defaultRemoteControlActiveStateChannelUpdateHandler());
59 handlers.put(CHANNEL_REMOTE_START_ALLOWANCE_STATE, defaultRemoteStartAllowanceChannelUpdateHandler());
60 handlers.put(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE, defaultLocalControlActiveStateChannelUpdateHandler());
61 handlers.put(CHANNEL_ACTIVE_PROGRAM_STATE, defaultActiveProgramStateUpdateHandler());
62 handlers.put(CHANNEL_SELECTED_PROGRAM_STATE,
63 updateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
65 // register washer specific handlers
66 handlers.put(CHANNEL_WASHER_SPIN_SPEED,
67 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
68 handlers.put(CHANNEL_WASHER_TEMPERATURE,
69 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
70 handlers.put(CHANNEL_WASHER_IDOS1_LEVEL,
71 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
72 handlers.put(CHANNEL_WASHER_IDOS2_LEVEL,
73 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
74 handlers.put(CHANNEL_WASHER_IDOS1,
75 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
76 handlers.put(CHANNEL_WASHER_IDOS2,
77 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
78 handlers.put(CHANNEL_WASHER_VARIO_PERFECT,
79 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
80 handlers.put(CHANNEL_WASHER_LESS_IRONING,
81 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
82 handlers.put(CHANNEL_WASHER_PRE_WASH,
83 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
84 handlers.put(CHANNEL_WASHER_RINSE_PLUS,
85 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
86 handlers.put(CHANNEL_WASHER_SOAK,
87 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
88 handlers.put(CHANNEL_PROGRAM_ENERGY,
89 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
90 handlers.put(CHANNEL_PROGRAM_WATER,
91 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
95 protected void configureEventHandlers(Map<String, EventHandler> handlers) {
96 // register default event handlers
97 handlers.put(EVENT_DOOR_STATE, defaultDoorStateEventHandler());
98 handlers.put(EVENT_REMOTE_CONTROL_ACTIVE, updateRemoteControlActiveAndProgramOptionsStateEventHandler());
99 handlers.put(EVENT_REMOTE_CONTROL_START_ALLOWED,
100 defaultBooleanEventHandler(CHANNEL_REMOTE_START_ALLOWANCE_STATE));
101 handlers.put(EVENT_FINISH_IN_RELATIVE, defaultRemainingProgramTimeEventHandler());
102 handlers.put(EVENT_REMAINING_PROGRAM_TIME, defaultRemainingProgramTimeEventHandler());
103 handlers.put(EVENT_PROGRAM_PROGRESS, defaultPercentQuantityTypeEventHandler(CHANNEL_PROGRAM_PROGRESS_STATE));
104 handlers.put(EVENT_LOCAL_CONTROL_ACTIVE, defaultBooleanEventHandler(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE));
105 handlers.put(EVENT_ACTIVE_PROGRAM, updateProgramOptionsAndActiveProgramStateEventHandler());
106 handlers.put(EVENT_OPERATION_STATE, defaultOperationStateEventHandler());
107 handlers.put(EVENT_SELECTED_PROGRAM, updateProgramOptionsAndSelectedProgramStateEventHandler());
109 // register washer specific event handlers
110 handlers.put(EVENT_WASHER_TEMPERATURE,
111 event -> getThingChannel(CHANNEL_WASHER_TEMPERATURE).ifPresent(channel -> updateState(channel.getUID(),
112 event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
113 handlers.put(EVENT_WASHER_SPIN_SPEED,
114 event -> getThingChannel(CHANNEL_WASHER_SPIN_SPEED).ifPresent(channel -> updateState(channel.getUID(),
115 event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
116 handlers.put(EVENT_WASHER_IDOS_1_DOSING_LEVEL,
117 event -> getThingChannel(CHANNEL_WASHER_IDOS1_LEVEL).ifPresent(channel -> updateState(channel.getUID(),
118 event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
119 handlers.put(EVENT_WASHER_IDOS_2_DOSING_LEVEL,
120 event -> getThingChannel(CHANNEL_WASHER_IDOS2_LEVEL).ifPresent(channel -> updateState(channel.getUID(),
121 event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
125 protected boolean isChannelLinkedToProgramOptionNotFullySupportedByApi() {
126 return (getThingChannel(CHANNEL_WASHER_IDOS1).isPresent() && isLinked(CHANNEL_WASHER_IDOS1))
127 || (getThingChannel(CHANNEL_WASHER_IDOS2).isPresent() && isLinked(CHANNEL_WASHER_IDOS2))
128 || (getThingChannel(CHANNEL_WASHER_VARIO_PERFECT).isPresent() && isLinked(CHANNEL_WASHER_VARIO_PERFECT))
129 || (getThingChannel(CHANNEL_WASHER_LESS_IRONING).isPresent() && isLinked(CHANNEL_WASHER_LESS_IRONING))
130 || (getThingChannel(CHANNEL_WASHER_PRE_WASH).isPresent() && isLinked(CHANNEL_WASHER_PRE_WASH))
131 || (getThingChannel(CHANNEL_WASHER_RINSE_PLUS).isPresent() && isLinked(CHANNEL_WASHER_RINSE_PLUS))
132 || (getThingChannel(CHANNEL_WASHER_SOAK).isPresent() && isLinked(CHANNEL_WASHER_SOAK))
133 || (getThingChannel(CHANNEL_PROGRAM_ENERGY).isPresent() && isLinked(CHANNEL_PROGRAM_ENERGY))
134 || (getThingChannel(CHANNEL_PROGRAM_WATER).isPresent() && isLinked(CHANNEL_PROGRAM_WATER));
138 protected void handleCommand(final ChannelUID channelUID, final Command command,
139 final HomeConnectApiClient apiClient)
140 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
141 super.handleCommand(channelUID, command, apiClient);
142 String operationState = getOperationState();
144 // only handle these commands if operation state allows it
145 if (operationState != null && INACTIVE_STATE.contains(operationState) && command instanceof StringType) {
146 switch (channelUID.getId()) {
147 case CHANNEL_WASHER_TEMPERATURE:
148 apiClient.setProgramOptions(getThingHaId(), OPTION_WASHER_TEMPERATURE, command.toFullString(), null,
151 case CHANNEL_WASHER_SPIN_SPEED:
152 apiClient.setProgramOptions(getThingHaId(), OPTION_WASHER_SPIN_SPEED, command.toFullString(), null,
155 case CHANNEL_WASHER_IDOS1_LEVEL:
156 apiClient.setProgramOptions(getThingHaId(), OPTION_WASHER_IDOS_1_DOSING_LEVEL,
157 command.toFullString(), null, false, false);
159 case CHANNEL_WASHER_IDOS2_LEVEL:
160 apiClient.setProgramOptions(getThingHaId(), OPTION_WASHER_IDOS_2_DOSING_LEVEL,
161 command.toFullString(), null, false, false);
165 logger.debug("Device can not handle command {} in current operation state ({}). haId={}", command,
166 operationState, getThingHaId());
171 public String toString() {
172 return "HomeConnectWasherHandler [haId: " + getThingHaId() + "]";
176 protected void resetProgramStateChannels(boolean offline) {
177 super.resetProgramStateChannels(offline);
178 getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
179 getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
180 getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
182 getThingChannel(CHANNEL_WASHER_TEMPERATURE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
183 getThingChannel(CHANNEL_WASHER_SPIN_SPEED).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
184 getThingChannel(CHANNEL_WASHER_IDOS1_LEVEL).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
185 getThingChannel(CHANNEL_WASHER_IDOS2_LEVEL).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
186 getThingChannel(CHANNEL_WASHER_IDOS1).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
187 getThingChannel(CHANNEL_WASHER_IDOS2).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
188 getThingChannel(CHANNEL_WASHER_VARIO_PERFECT).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
189 getThingChannel(CHANNEL_WASHER_LESS_IRONING).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
190 getThingChannel(CHANNEL_WASHER_PRE_WASH).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
191 getThingChannel(CHANNEL_WASHER_RINSE_PLUS).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
192 getThingChannel(CHANNEL_WASHER_SOAK).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
193 getThingChannel(CHANNEL_PROGRAM_ENERGY).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
194 getThingChannel(CHANNEL_PROGRAM_WATER).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));