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
38 * @author Karel Goderis - Initial contribution
39 * @author Jacob Laursen - Added UoM for temperatures, raw channels
41 public enum FridgeChannelSelector implements ApplianceChannelSelector {
43 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
44 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
45 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
46 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
47 SUPERCOOL(null, SUPERCOOL_CHANNEL_ID, OnOffType.class, false),
48 FRIDGECURRENTTEMP("currentTemperature", "current", QuantityType.class, false) {
50 public State getState(String s, DeviceMetaData dmd) {
51 return getTemperatureState(s);
54 FRIDGETARGETTEMP("targetTemperature", "target", QuantityType.class, false) {
56 public State getState(String s, DeviceMetaData dmd) {
57 return getTemperatureState(s);
60 DOOR("signalDoor", "door", OpenClosedType.class, false) {
63 public State getState(String s, DeviceMetaData dmd) {
64 if ("true".equals(s)) {
65 return getState("OPEN");
68 if ("false".equals(s)) {
69 return getState("CLOSED");
72 return UnDefType.UNDEF;
75 START(null, "start", OnOffType.class, false);
77 private final Logger logger = LoggerFactory.getLogger(FridgeChannelSelector.class);
79 private final String mieleID;
80 private final String channelID;
81 private final Class<? extends Type> typeClass;
82 private final boolean isProperty;
84 FridgeChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
85 this.mieleID = propertyID;
86 this.channelID = channelID;
87 this.typeClass = typeClass;
88 this.isProperty = isProperty;
92 public String toString() {
97 public String getMieleID() {
102 public String getChannelID() {
107 public boolean isProperty() {
112 public boolean isExtendedState() {
117 public State getState(String s, DeviceMetaData dmd) {
119 String localizedValue = getMieleEnum(s, dmd);
120 if (localizedValue == null) {
121 localizedValue = dmd.LocalizedValue;
123 if (localizedValue == null) {
127 return getState(localizedValue);
133 public State getState(String s) {
135 Method valueOf = typeClass.getMethod("valueOf", String.class);
136 State state = (State) valueOf.invoke(typeClass, s);
140 } catch (Exception e) {
141 logger.error("An exception occurred while converting '{}' into a State", s);
147 public State getTemperatureState(String s) {
149 return DeviceUtil.getTemperatureState(s);
150 } catch (NumberFormatException e) {
151 logger.warn("An exception occurred while converting '{}' into a State", s);
152 return UnDefType.UNDEF;
156 public String getMieleEnum(String s, DeviceMetaData dmd) {
157 if (dmd.MieleEnum != null) {
158 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
159 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
160 return enumEntry.getKey();