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