]> git.basschouten.com Git - openhab-addons.git/blob
9d1d97d2ac2f83002cedb2877d90ee4862582cd4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.homeconnect.internal.handler;
14
15 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.*;
16
17 import java.util.Arrays;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient;
24 import org.openhab.binding.homeconnect.internal.client.exception.ApplianceOfflineException;
25 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
26 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
27 import org.openhab.binding.homeconnect.internal.type.HomeConnectDynamicStateDescriptionProvider;
28 import org.openhab.core.library.types.StringType;
29 import org.openhab.core.thing.Channel;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.UnDefType;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38  * The {@link HomeConnectWasherDryerHandler} is responsible for handling commands, which are
39  * sent to one of the channels of a washer dryer combined machine.
40  *
41  * @author Jonas BrĂ¼stel - Initial contribution
42  */
43 @NonNullByDefault
44 public class HomeConnectWasherDryerHandler extends AbstractHomeConnectThingHandler {
45
46     private static final List<String> INACTIVE_STATE = Arrays.asList(OPERATION_STATE_INACTIVE, OPERATION_STATE_READY);
47
48     private final Logger logger = LoggerFactory.getLogger(HomeConnectWasherDryerHandler.class);
49
50     public HomeConnectWasherDryerHandler(Thing thing,
51             HomeConnectDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
52         super(thing, dynamicStateDescriptionProvider);
53     }
54
55     @Override
56     protected void configureChannelUpdateHandlers(Map<String, ChannelUpdateHandler> handlers) {
57         // register default update handlers
58         handlers.put(CHANNEL_DOOR_STATE, defaultDoorStateChannelUpdateHandler());
59         handlers.put(CHANNEL_OPERATION_STATE, defaultOperationStateChannelUpdateHandler());
60         handlers.put(CHANNEL_REMOTE_CONTROL_ACTIVE_STATE, defaultRemoteControlActiveStateChannelUpdateHandler());
61         handlers.put(CHANNEL_REMOTE_START_ALLOWANCE_STATE, defaultRemoteStartAllowanceChannelUpdateHandler());
62         handlers.put(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE, defaultLocalControlActiveStateChannelUpdateHandler());
63         handlers.put(CHANNEL_ACTIVE_PROGRAM_STATE, defaultActiveProgramStateUpdateHandler());
64         handlers.put(CHANNEL_SELECTED_PROGRAM_STATE,
65                 updateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
66
67         // register washer specific handlers
68         handlers.put(CHANNEL_WASHER_SPIN_SPEED, (channelUID, cache) -> {
69             Optional<Channel> channel = getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE);
70             if (channel.isPresent()) {
71                 updateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler()
72                         .handle(channel.get().getUID(), cache);
73             }
74         });
75         handlers.put(CHANNEL_WASHER_TEMPERATURE, (channelUID, cache) -> {
76             Optional<Channel> channel = getThingChannel(CHANNEL_SELECTED_PROGRAM_STATE);
77             if (channel.isPresent()) {
78                 updateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler()
79                         .handle(channel.get().getUID(), cache);
80             }
81         });
82     }
83
84     @Override
85     protected void configureEventHandlers(Map<String, EventHandler> handlers) {
86         // register default event handlers
87         handlers.put(EVENT_DOOR_STATE, defaultDoorStateEventHandler());
88         handlers.put(EVENT_REMOTE_CONTROL_ACTIVE, updateRemoteControlActiveAndProgramOptionsStateEventHandler());
89         handlers.put(EVENT_REMOTE_CONTROL_START_ALLOWED,
90                 defaultBooleanEventHandler(CHANNEL_REMOTE_START_ALLOWANCE_STATE));
91         handlers.put(EVENT_FINISH_IN_RELATIVE, defaultRemainingProgramTimeEventHandler());
92         handlers.put(EVENT_REMAINING_PROGRAM_TIME, defaultRemainingProgramTimeEventHandler());
93         handlers.put(EVENT_PROGRAM_PROGRESS, defaultPercentQuantityTypeEventHandler(CHANNEL_PROGRAM_PROGRESS_STATE));
94         handlers.put(EVENT_LOCAL_CONTROL_ACTIVE, defaultBooleanEventHandler(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE));
95         handlers.put(EVENT_ACTIVE_PROGRAM, defaultActiveProgramEventHandler());
96         handlers.put(EVENT_OPERATION_STATE, defaultOperationStateEventHandler());
97         handlers.put(EVENT_SELECTED_PROGRAM, updateProgramOptionsAndSelectedProgramStateEventHandler());
98
99         // register washer specific event handlers
100         handlers.put(EVENT_WASHER_TEMPERATURE,
101                 event -> getThingChannel(CHANNEL_WASHER_TEMPERATURE).ifPresent(channel -> updateState(channel.getUID(),
102                         event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
103         handlers.put(EVENT_WASHER_SPIN_SPEED,
104                 event -> getThingChannel(CHANNEL_WASHER_SPIN_SPEED).ifPresent(channel -> updateState(channel.getUID(),
105                         event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
106         handlers.put(EVENT_DRYER_DRYING_TARGET,
107                 event -> getThingChannel(CHANNEL_DRYER_DRYING_TARGET).ifPresent(channel -> updateState(channel.getUID(),
108                         event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
109     }
110
111     @Override
112     protected void handleCommand(final ChannelUID channelUID, final Command command,
113             final HomeConnectApiClient apiClient)
114             throws CommunicationException, AuthorizationException, ApplianceOfflineException {
115         super.handleCommand(channelUID, command, apiClient);
116         String operationState = getOperationState();
117
118         // only handle these commands if operation state allows it
119         if (operationState != null && INACTIVE_STATE.contains(operationState) && command instanceof StringType) {
120             switch (channelUID.getId()) {
121                 case CHANNEL_WASHER_TEMPERATURE:
122                     apiClient.setProgramOptions(getThingHaId(), OPTION_WASHER_TEMPERATURE, command.toFullString(), null,
123                             false, false);
124                     break;
125                 case CHANNEL_WASHER_SPIN_SPEED:
126                     apiClient.setProgramOptions(getThingHaId(), OPTION_WASHER_SPIN_SPEED, command.toFullString(), null,
127                             false, false);
128                     break;
129                 case CHANNEL_DRYER_DRYING_TARGET:
130                     apiClient.setProgramOptions(getThingHaId(), OPTION_DRYER_DRYING_TARGET, command.toFullString(),
131                             null, false, false);
132                     break;
133             }
134         } else {
135             logger.debug("Device can not handle command {} in current operation state ({}). haId={}", command,
136                     operationState, getThingHaId());
137         }
138     }
139
140     @Override
141     public String toString() {
142         return "HomeConnectWasherDryerHandler [haId: " + getThingHaId() + "]";
143     }
144
145     @Override
146     protected void resetProgramStateChannels(boolean offline) {
147         super.resetProgramStateChannels(offline);
148         getThingChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
149         getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
150         getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
151         if (offline) {
152             getThingChannel(CHANNEL_WASHER_TEMPERATURE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
153             getThingChannel(CHANNEL_WASHER_SPIN_SPEED).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
154             getThingChannel(CHANNEL_DRYER_DRYING_TARGET).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
155         }
156     }
157 }