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.CHANNEL_DOOR_STATE;
16 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_FREEZER_SETPOINT_TEMPERATURE;
17 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_FREEZER_SUPER_MODE;
18 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE;
19 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.CHANNEL_REFRIGERATOR_SUPER_MODE;
20 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_DOOR_STATE;
21 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FREEZER_SETPOINT_TEMPERATURE;
22 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FREEZER_SUPER_MODE;
23 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FRIDGE_SETPOINT_TEMPERATURE;
24 import static org.openhab.binding.homeconnect.internal.HomeConnectBindingConstants.EVENT_FRIDGE_SUPER_MODE;
27 import java.util.Optional;
29 import javax.measure.UnconvertibleException;
31 import org.eclipse.jdt.annotation.NonNullByDefault;
32 import org.openhab.binding.homeconnect.internal.client.HomeConnectApiClient;
33 import org.openhab.binding.homeconnect.internal.client.exception.ApplianceOfflineException;
34 import org.openhab.binding.homeconnect.internal.client.exception.AuthorizationException;
35 import org.openhab.binding.homeconnect.internal.client.exception.CommunicationException;
36 import org.openhab.binding.homeconnect.internal.client.model.Data;
37 import org.openhab.binding.homeconnect.internal.type.HomeConnectDynamicStateDescriptionProvider;
38 import org.openhab.core.library.types.OnOffType;
39 import org.openhab.core.library.types.QuantityType;
40 import org.openhab.core.thing.ChannelUID;
41 import org.openhab.core.thing.Thing;
42 import org.openhab.core.types.Command;
43 import org.openhab.core.types.UnDefType;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * The {@link HomeConnectFridgeFreezerHandler} is responsible for handling commands, which are
49 * sent to one of the channels of a fridge/freezer.
51 * @author Jonas BrĂ¼stel - Initial contribution
54 public class HomeConnectFridgeFreezerHandler extends AbstractHomeConnectThingHandler {
56 private final Logger logger = LoggerFactory.getLogger(HomeConnectFridgeFreezerHandler.class);
58 public HomeConnectFridgeFreezerHandler(Thing thing,
59 HomeConnectDynamicStateDescriptionProvider dynamicStateDescriptionProvider) {
60 super(thing, dynamicStateDescriptionProvider);
64 protected void configureChannelUpdateHandlers(Map<String, ChannelUpdateHandler> handlers) {
65 // register default update handlers
66 handlers.put(CHANNEL_DOOR_STATE, defaultDoorStateChannelUpdateHandler());
68 // register fridge/freezer specific handlers
69 handlers.put(CHANNEL_FREEZER_SETPOINT_TEMPERATURE,
70 (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
71 Optional<HomeConnectApiClient> apiClient = getApiClient();
72 if (apiClient.isPresent()) {
73 Data data = apiClient.get().getFreezerSetpointTemperature(getThingHaId());
74 if (data.getValue() != null) {
75 return new QuantityType<>(data.getValueAsInt(), mapTemperature(data.getUnit()));
77 return UnDefType.UNDEF;
80 return UnDefType.UNDEF;
82 handlers.put(CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE,
83 (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
84 Optional<HomeConnectApiClient> apiClient = getApiClient();
85 if (apiClient.isPresent()) {
86 Data data = apiClient.get().getFridgeSetpointTemperature(getThingHaId());
87 if (data.getValue() != null) {
88 return new QuantityType<>(data.getValueAsInt(), mapTemperature(data.getUnit()));
90 return UnDefType.UNDEF;
93 return UnDefType.UNDEF;
95 handlers.put(CHANNEL_REFRIGERATOR_SUPER_MODE,
96 (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
97 Optional<HomeConnectApiClient> apiClient = getApiClient();
98 if (apiClient.isPresent()) {
99 Data data = apiClient.get().getFridgeSuperMode(getThingHaId());
100 if (data.getValue() != null) {
101 return OnOffType.from(data.getValueAsBoolean());
103 return UnDefType.UNDEF;
106 return UnDefType.UNDEF;
108 handlers.put(CHANNEL_FREEZER_SUPER_MODE,
109 (channelUID, cache) -> updateState(channelUID, cache.putIfAbsentAndGet(channelUID, () -> {
110 Optional<HomeConnectApiClient> apiClient = getApiClient();
112 if (apiClient.isPresent()) {
113 Data data = apiClient.get().getFreezerSuperMode(getThingHaId());
114 if (data.getValue() != null) {
115 return OnOffType.from(data.getValueAsBoolean());
117 return UnDefType.UNDEF;
120 return UnDefType.UNDEF;
125 protected void configureEventHandlers(Map<String, EventHandler> handlers) {
126 // register default event handlers
127 handlers.put(EVENT_DOOR_STATE, defaultDoorStateEventHandler());
128 handlers.put(EVENT_FREEZER_SUPER_MODE, defaultBooleanEventHandler(CHANNEL_FREEZER_SUPER_MODE));
129 handlers.put(EVENT_FRIDGE_SUPER_MODE, defaultBooleanEventHandler(CHANNEL_REFRIGERATOR_SUPER_MODE));
131 // register fridge/freezer specific event handlers
132 handlers.put(EVENT_FREEZER_SETPOINT_TEMPERATURE,
133 event -> getThingChannel(CHANNEL_FREEZER_SETPOINT_TEMPERATURE)
134 .ifPresent(channel -> updateState(channel.getUID(),
135 new QuantityType<>(event.getValueAsInt(), mapTemperature(event.getUnit())))));
136 handlers.put(EVENT_FRIDGE_SETPOINT_TEMPERATURE,
137 event -> getThingChannel(CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE)
138 .ifPresent(channel -> updateState(channel.getUID(),
139 new QuantityType<>(event.getValueAsInt(), mapTemperature(event.getUnit())))));
143 protected void handleCommand(final ChannelUID channelUID, final Command command,
144 final HomeConnectApiClient apiClient)
145 throws CommunicationException, AuthorizationException, ApplianceOfflineException {
146 super.handleCommand(channelUID, command, apiClient);
149 if (CHANNEL_REFRIGERATOR_SETPOINT_TEMPERATURE.equals(channelUID.getId())
150 || CHANNEL_FREEZER_SETPOINT_TEMPERATURE.equals(channelUID.getId())) {
151 handleTemperatureCommand(channelUID, command, apiClient);
152 } else if (command instanceof OnOffType) {
153 if (CHANNEL_FREEZER_SUPER_MODE.equals(channelUID.getId())) {
154 apiClient.setFreezerSuperMode(getThingHaId(), OnOffType.ON.equals(command));
155 } else if (CHANNEL_REFRIGERATOR_SUPER_MODE.equals(channelUID.getId())) {
156 apiClient.setFridgeSuperMode(getThingHaId(), OnOffType.ON.equals(command));
159 } catch (UnconvertibleException e) {
160 logger.debug("Could not set setpoint! haId={}, error={}", getThingHaId(), e.getMessage());
165 protected void updateSelectedProgramStateDescription() {
170 protected void removeSelectedProgramStateDescription() {
175 public String toString() {
176 return "HomeConnectFridgeFreezerHandler [haId: " + getThingHaId() + "]";