]> git.basschouten.com Git - openhab-addons.git/blob
c74a176d149c838fc535bbbddb926e788e7a7e1f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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         // register dryer specific handlers
65         handlers.put(CHANNEL_DRYER_DRYING_TARGET,
66                 getAndUpdateProgramOptionsStateDescriptionsAndSelectedProgramStateUpdateHandler());
67     }
68
69     @Override
70     protected void configureEventHandlers(Map<String, EventHandler> handlers) {
71         // register default event handlers
72         handlers.put(EVENT_DOOR_STATE, defaultDoorStateEventHandler());
73         handlers.put(EVENT_REMOTE_CONTROL_ACTIVE, updateRemoteControlActiveAndProgramOptionsStateEventHandler());
74         handlers.put(EVENT_REMOTE_CONTROL_START_ALLOWED,
75                 defaultBooleanEventHandler(CHANNEL_REMOTE_START_ALLOWANCE_STATE));
76         handlers.put(EVENT_FINISH_IN_RELATIVE, defaultRemainingProgramTimeEventHandler());
77         handlers.put(EVENT_REMAINING_PROGRAM_TIME, defaultRemainingProgramTimeEventHandler());
78         handlers.put(EVENT_PROGRAM_PROGRESS, defaultPercentQuantityTypeEventHandler(CHANNEL_PROGRAM_PROGRESS_STATE));
79         handlers.put(EVENT_LOCAL_CONTROL_ACTIVE, defaultBooleanEventHandler(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE));
80         handlers.put(EVENT_ACTIVE_PROGRAM, defaultActiveProgramEventHandler());
81         handlers.put(EVENT_OPERATION_STATE, defaultOperationStateEventHandler());
82         handlers.put(EVENT_SELECTED_PROGRAM, updateProgramOptionsAndSelectedProgramStateEventHandler());
83
84         // register dryer specific event handlers
85         handlers.put(EVENT_DRYER_DRYING_TARGET,
86                 event -> getLinkedChannel(CHANNEL_DRYER_DRYING_TARGET)
87                         .ifPresent(channel -> updateState(channel.getUID(),
88                                 event.getValue() == null ? UnDefType.UNDEF : new StringType(event.getValue()))));
89     }
90
91     @Override
92     protected void handleCommand(final ChannelUID channelUID, final Command command,
93             final HomeConnectApiClient apiClient)
94             throws CommunicationException, AuthorizationException, ApplianceOfflineException {
95         super.handleCommand(channelUID, command, apiClient);
96
97         String operationState = getOperationState();
98
99         // only handle these commands if operation state allows it
100         if (operationState != null && INACTIVE_STATE.contains(operationState)) {
101             // set drying target option
102             if (command instanceof StringType && CHANNEL_DRYER_DRYING_TARGET.equals(channelUID.getId())) {
103                 apiClient.setProgramOptions(getThingHaId(), OPTION_DRYER_DRYING_TARGET, command.toFullString(), null,
104                         false, false);
105             }
106         } else {
107             logger.debug("Device can not handle command {} in current operation state ({}). thing={}, haId={}", command,
108                     operationState, getThingLabel(), getThingHaId());
109         }
110     }
111
112     @Override
113     public String toString() {
114         return "HomeConnectDryerHandler [haId: " + getThingHaId() + "]";
115     }
116
117     @Override
118     protected void resetProgramStateChannels(boolean offline) {
119         super.resetProgramStateChannels(offline);
120         getLinkedChannel(CHANNEL_REMAINING_PROGRAM_TIME_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
121         getLinkedChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
122         getLinkedChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
123         if (offline) {
124             getLinkedChannel(CHANNEL_DRYER_DRYING_TARGET).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
125         }
126     }
127 }