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.miele.internal.handler;
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
17 import java.lang.reflect.Method;
18 import java.util.Map.Entry;
20 import org.openhab.binding.miele.internal.DeviceUtil;
21 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.QuantityType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.Type;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
33 import com.google.gson.JsonElement;
36 * The {@link ApplianceChannelSelector} for fridges with
37 * a freezer compartment
39 * @author Karel Goderis - Initial contribution
40 * @author Jacob Laursen - Added UoM for temperatures, raw channels
42 public enum FridgeFreezerChannelSelector implements ApplianceChannelSelector {
44 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
45 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
46 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
47 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
48 FREEZERSTATE("freezerState", "freezerstate", StringType.class, false),
49 FRIDGESTATE("fridgeState", "fridgestate", StringType.class, false),
50 SUPERCOOL(null, SUPERCOOL_CHANNEL_ID, OnOffType.class, false),
51 SUPERFREEZE(null, SUPERFREEZE_CHANNEL_ID, OnOffType.class, false),
52 FREEZERCURRENTTEMP("freezerCurrentTemperature", "freezercurrent", QuantityType.class, false) {
54 public State getState(String s, DeviceMetaData dmd) {
55 return getTemperatureState(s);
58 FREEZERTARGETTEMP("freezerTargetTemperature", "freezertarget", QuantityType.class, false) {
60 public State getState(String s, DeviceMetaData dmd) {
61 return getTemperatureState(s);
64 FRIDGECURRENTTEMP("fridgeCurrentTemperature", "fridgecurrent", QuantityType.class, false) {
66 public State getState(String s, DeviceMetaData dmd) {
67 return getTemperatureState(s);
70 FRIDGETARGETTEMP("fridgeTargetTemperature", "fridgetarget", QuantityType.class, false) {
72 public State getState(String s, DeviceMetaData dmd) {
73 return getTemperatureState(s);
76 DOOR("signalDoor", "door", OpenClosedType.class, false) {
79 public State getState(String s, DeviceMetaData dmd) {
80 if ("true".equals(s)) {
81 return getState("OPEN");
84 if ("false".equals(s)) {
85 return getState("CLOSED");
88 return UnDefType.UNDEF;
91 START(null, "start", OnOffType.class, false);
93 private final Logger logger = LoggerFactory.getLogger(FridgeFreezerChannelSelector.class);
95 private final String mieleID;
96 private final String channelID;
97 private final Class<? extends Type> typeClass;
98 private final boolean isProperty;
100 FridgeFreezerChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
101 boolean isProperty) {
102 this.mieleID = propertyID;
103 this.channelID = channelID;
104 this.typeClass = typeClass;
105 this.isProperty = isProperty;
109 public String toString() {
114 public String getMieleID() {
119 public String getChannelID() {
124 public boolean isProperty() {
129 public boolean isExtendedState() {
134 public State getState(String s, DeviceMetaData dmd) {
136 String localizedValue = getMieleEnum(s, dmd);
137 if (localizedValue == null) {
138 localizedValue = dmd.LocalizedValue;
140 if (localizedValue == null) {
144 return getState(localizedValue);
150 public State getState(String s) {
152 Method valueOf = typeClass.getMethod("valueOf", String.class);
153 State state = (State) valueOf.invoke(typeClass, s);
157 } catch (Exception e) {
158 logger.error("An exception occurred while converting '{}' into a State", s);
164 public State getTemperatureState(String s) {
166 return DeviceUtil.getTemperatureState(s);
167 } catch (NumberFormatException e) {
168 logger.warn("An exception occurred while converting '{}' into a State", s);
169 return UnDefType.UNDEF;
173 public String getMieleEnum(String s, DeviceMetaData dmd) {
174 if (dmd.MieleEnum != null) {
175 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
176 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
177 return enumEntry.getKey();