2 * Copyright (c) 2010-2020 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 java.lang.reflect.Method;
16 import java.util.Map.Entry;
18 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.OpenClosedType;
22 import org.openhab.core.library.types.StringType;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.Type;
25 import org.openhab.core.types.UnDefType;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
29 import com.google.gson.JsonElement;
32 * The {@link ApplianceChannelSelector} for fridges
34 * @author Karel Goderis - Initial contribution
36 public enum FridgeChannelSelector implements ApplianceChannelSelector {
38 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
39 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
40 BRAND_ID("brandId", "brandId", StringType.class, true),
41 COMPANY_ID("companyId", "companyId", StringType.class, true),
42 STATE("state", "state", StringType.class, false),
43 SUPERCOOL(null, "supercool", OnOffType.class, false),
44 FRIDGECURRENTTEMP("currentTemperature", "current", DecimalType.class, false) {
46 public State getState(String s, DeviceMetaData dmd) {
50 FRIDGETARGETTEMP("targetTemperature", "target", DecimalType.class, false) {
52 public State getState(String s, DeviceMetaData dmd) {
56 DOOR("signalDoor", "door", OpenClosedType.class, false) {
59 public State getState(String s, DeviceMetaData dmd) {
60 if ("true".equals(s)) {
61 return getState("OPEN");
64 if ("false".equals(s)) {
65 return getState("CLOSED");
68 return UnDefType.UNDEF;
71 START(null, "start", OnOffType.class, false);
73 private final Logger logger = LoggerFactory.getLogger(FridgeChannelSelector.class);
75 private final String mieleID;
76 private final String channelID;
77 private final Class<? extends Type> typeClass;
78 private final boolean isProperty;
80 FridgeChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
81 this.mieleID = propertyID;
82 this.channelID = channelID;
83 this.typeClass = typeClass;
84 this.isProperty = isProperty;
88 public String toString() {
93 public String getMieleID() {
98 public String getChannelID() {
103 public Class<? extends Type> getTypeClass() {
108 public boolean isProperty() {
113 public State getState(String s, DeviceMetaData dmd) {
115 String localizedValue = getMieleEnum(s, dmd);
116 if (localizedValue == null) {
117 localizedValue = dmd.LocalizedValue;
119 if (localizedValue == null) {
123 return getState(localizedValue);
129 public State getState(String s) {
131 Method valueOf = typeClass.getMethod("valueOf", String.class);
132 State state = (State) valueOf.invoke(typeClass, s);
136 } catch (Exception e) {
137 logger.error("An exception occurred while converting '{}' into a State", s);
143 public String getMieleEnum(String s, DeviceMetaData dmd) {
144 if (dmd.MieleEnum != null) {
145 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
146 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
147 return enumEntry.getKey();