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;
16 import static org.openhab.binding.miele.internal.MieleBindingConstants.SUPERFREEZE_CHANNEL_ID;
18 import java.lang.reflect.Method;
19 import java.util.Map.Entry;
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.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 with
36 * a freezer compartment
38 * @author Karel Goderis - Initial contribution
40 public enum FridgeFreezerChannelSelector implements ApplianceChannelSelector {
42 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
43 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
44 BRAND_ID("brandId", "brandId", StringType.class, true),
45 COMPANY_ID("companyId", "companyId", StringType.class, true),
46 STATE("state", "state", StringType.class, false),
47 FREEZERSTATE("freezerState", "freezerstate", StringType.class, false),
48 FRIDGESTATE("fridgeState", "fridgestate", StringType.class, false),
49 SUPERCOOL(null, SUPERCOOL_CHANNEL_ID, OnOffType.class, false),
50 SUPERFREEZE(null, SUPERFREEZE_CHANNEL_ID, OnOffType.class, false),
51 FREEZERCURRENTTEMP("freezerCurrentTemperature", "freezercurrent", DecimalType.class, false) {
53 public State getState(String s, DeviceMetaData dmd) {
57 FREEZERTARGETTEMP("freezerTargetTemperature", "freezertarget", DecimalType.class, false) {
59 public State getState(String s, DeviceMetaData dmd) {
63 FRIDGECURRENTTEMP("fridgeCurrentTemperature", "fridgecurrent", DecimalType.class, false) {
65 public State getState(String s, DeviceMetaData dmd) {
69 FRIDGETARGETTEMP("fridgeTargetTemperature", "fridgetarget", DecimalType.class, false) {
71 public State getState(String s, DeviceMetaData dmd) {
75 DOOR("signalDoor", "door", OpenClosedType.class, false) {
78 public State getState(String s, DeviceMetaData dmd) {
79 if ("true".equals(s)) {
80 return getState("OPEN");
83 if ("false".equals(s)) {
84 return getState("CLOSED");
87 return UnDefType.UNDEF;
90 START(null, "start", OnOffType.class, false);
92 private final Logger logger = LoggerFactory.getLogger(FridgeFreezerChannelSelector.class);
94 private final String mieleID;
95 private final String channelID;
96 private final Class<? extends Type> typeClass;
97 private final boolean isProperty;
99 FridgeFreezerChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
100 boolean isProperty) {
101 this.mieleID = propertyID;
102 this.channelID = channelID;
103 this.typeClass = typeClass;
104 this.isProperty = isProperty;
108 public String toString() {
113 public String getMieleID() {
118 public String getChannelID() {
123 public boolean isProperty() {
128 public boolean isExtendedState() {
133 public State getState(String s, DeviceMetaData dmd) {
135 String localizedValue = getMieleEnum(s, dmd);
136 if (localizedValue == null) {
137 localizedValue = dmd.LocalizedValue;
139 if (localizedValue == null) {
143 return getState(localizedValue);
149 public State getState(String s) {
151 Method valueOf = typeClass.getMethod("valueOf", String.class);
152 State state = (State) valueOf.invoke(typeClass, s);
156 } catch (Exception e) {
157 logger.error("An exception occurred while converting '{}' into a State", s);
163 public String getMieleEnum(String s, DeviceMetaData dmd) {
164 if (dmd.MieleEnum != null) {
165 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
166 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
167 return enumEntry.getKey();