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.SUPERCOOL_CHANNEL_ID;
17 import java.lang.reflect.Method;
18 import java.util.Map.Entry;
20 import org.openhab.binding.miele.internal.ExtendedDeviceStateUtil;
21 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
28 import org.openhab.core.types.UnDefType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import com.google.gson.JsonElement;
35 * The {@link ApplianceChannelSelector} for fridges
37 * @author Karel Goderis - Initial contribution
39 public enum FridgeChannelSelector implements ApplianceChannelSelector {
41 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
42 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
43 BRAND_ID("brandId", "brandId", StringType.class, true),
44 COMPANY_ID("companyId", "companyId", StringType.class, true),
45 STATE("state", "state", StringType.class, false),
46 SUPERCOOL(null, SUPERCOOL_CHANNEL_ID, OnOffType.class, false),
47 FRIDGECURRENTTEMP("currentTemperature", "current", QuantityType.class, false) {
49 public State getState(String s, DeviceMetaData dmd) {
50 return getTemperatureState(s);
53 FRIDGETARGETTEMP("targetTemperature", "target", QuantityType.class, false) {
55 public State getState(String s, DeviceMetaData dmd) {
56 return getTemperatureState(s);
59 DOOR("signalDoor", "door", OpenClosedType.class, false) {
62 public State getState(String s, DeviceMetaData dmd) {
63 if ("true".equals(s)) {
64 return getState("OPEN");
67 if ("false".equals(s)) {
68 return getState("CLOSED");
71 return UnDefType.UNDEF;
74 START(null, "start", OnOffType.class, false);
76 private final Logger logger = LoggerFactory.getLogger(FridgeChannelSelector.class);
78 private final String mieleID;
79 private final String channelID;
80 private final Class<? extends Type> typeClass;
81 private final boolean isProperty;
83 FridgeChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
84 this.mieleID = propertyID;
85 this.channelID = channelID;
86 this.typeClass = typeClass;
87 this.isProperty = isProperty;
91 public String toString() {
96 public String getMieleID() {
101 public String getChannelID() {
106 public boolean isProperty() {
111 public boolean isExtendedState() {
116 public State getState(String s, DeviceMetaData dmd) {
118 String localizedValue = getMieleEnum(s, dmd);
119 if (localizedValue == null) {
120 localizedValue = dmd.LocalizedValue;
122 if (localizedValue == null) {
126 return getState(localizedValue);
132 public State getState(String s) {
134 Method valueOf = typeClass.getMethod("valueOf", String.class);
135 State state = (State) valueOf.invoke(typeClass, s);
139 } catch (Exception e) {
140 logger.error("An exception occurred while converting '{}' into a State", s);
146 public State getTemperatureState(String s) {
148 return ExtendedDeviceStateUtil.getTemperatureState(s);
149 } catch (NumberFormatException e) {
150 logger.warn("An exception occurred while converting '{}' into a State", s);
151 return UnDefType.UNDEF;
155 public String getMieleEnum(String s, DeviceMetaData dmd) {
156 if (dmd.MieleEnum != null) {
157 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
158 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
159 return enumEntry.getKey();