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.*;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient;
21 import org.openhab.binding.homeconnect.internal.client.exception.ApplianceOfflineException;
22 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
23 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
24 import org.openhab.binding.homeconnect.internal.type.HomeConnectDynamicStateDescriptionProvider;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.UnDefType;
32 * The {@link HomeConnectCoffeeMakerHandler} is responsible for handling commands, which are
33 * sent to one of the channels of a coffee machine.
35 * @author Jonas BrĂ¼stel - Initial contribution
38 public class HomeConnectCoffeeMakerHandler extends AbstractHomeConnectThingHandler {
40 public HomeConnectCoffeeMakerHandler(Thing thing,
41 HomeConnectDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
42 super(thing, dynamicStateDescriptionProvider);
46 protected void configureChannelUpdateHandlers(Map<String, ChannelUpdateHandler> handlers) {
47 // register default update handlers
48 handlers.put(CHANNEL_OPERATION_STATE, defaultOperationStateChannelUpdateHandler());
49 handlers.put(CHANNEL_POWER_STATE, defaultPowerStateChannelUpdateHandler());
50 handlers.put(CHANNEL_REMOTE_START_ALLOWANCE_STATE, defaultRemoteStartAllowanceChannelUpdateHandler());
51 handlers.put(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE, defaultLocalControlActiveStateChannelUpdateHandler());
52 handlers.put(CHANNEL_SELECTED_PROGRAM_STATE, defaultSelectedProgramStateUpdateHandler());
53 handlers.put(CHANNEL_ACTIVE_PROGRAM_STATE, defaultActiveProgramStateUpdateHandler());
57 protected void configureEventHandlers(Map<String, EventHandler> handlers) {
58 // register default SSE event handlers
59 handlers.put(EVENT_REMOTE_CONTROL_START_ALLOWED,
60 defaultBooleanEventHandler(CHANNEL_REMOTE_START_ALLOWANCE_STATE));
61 handlers.put(EVENT_LOCAL_CONTROL_ACTIVE, defaultBooleanEventHandler(CHANNEL_LOCAL_CONTROL_ACTIVE_STATE));
62 handlers.put(EVENT_SELECTED_PROGRAM, defaultSelectedProgramStateEventHandler());
63 handlers.put(EVENT_COFFEEMAKER_BEAN_CONTAINER_EMPTY,
64 defaultEventPresentStateEventHandler(CHANNEL_COFFEEMAKER_BEAN_CONTAINER_EMPTY_STATE));
65 handlers.put(EVENT_COFFEEMAKER_DRIP_TRAY_FULL,
66 defaultEventPresentStateEventHandler(CHANNEL_COFFEEMAKER_DRIP_TRAY_FULL_STATE));
67 handlers.put(EVENT_COFFEEMAKER_WATER_TANK_EMPTY,
68 defaultEventPresentStateEventHandler(CHANNEL_COFFEEMAKER_WATER_TANK_EMPTY_STATE));
69 handlers.put(EVENT_ACTIVE_PROGRAM, defaultActiveProgramEventHandler());
70 handlers.put(EVENT_POWER_STATE, defaultPowerStateEventHandler());
71 handlers.put(EVENT_OPERATION_STATE, defaultOperationStateEventHandler());
73 // register coffee maker specific SSE event handlers
74 handlers.put(EVENT_PROGRAM_PROGRESS, event -> {
75 if (event.getValue() == null || event.getValueAsInt() == 0) {
76 getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE)
77 .ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
79 defaultPercentQuantityTypeEventHandler(CHANNEL_PROGRAM_PROGRESS_STATE).handle(event);
85 protected void handleCommand(ChannelUID channelUID, Command command, HomeConnectApiClient apiClient)
86 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
87 super.handleCommand(channelUID, command, apiClient);
89 // turn coffee maker on and standby
90 if (command instanceof OnOffType && CHANNEL_POWER_STATE.equals(channelUID.getId())) {
91 apiClient.setPowerState(getThingHaId(),
92 OnOffType.ON.equals(command) ? STATE_POWER_ON : STATE_POWER_STANDBY);
97 public String toString() {
98 return "HomeConnectCoffeeMakerHandler [haId: " + getThingHaId() + "]";
102 protected void resetProgramStateChannels() {
103 super.resetProgramStateChannels();
104 getThingChannel(CHANNEL_PROGRAM_PROGRESS_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));
105 getThingChannel(CHANNEL_ACTIVE_PROGRAM_STATE).ifPresent(c -> updateState(c.getUID(), UnDefType.UNDEF));