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.ExtendedDeviceStateUtil;
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 BRAND_ID("brandId", "brandId", StringType.class, true),
47 COMPANY_ID("companyId", "companyId", StringType.class, true),
48 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
49 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
50 FREEZERSTATE("freezerState", "freezerstate", StringType.class, false),
51 FRIDGESTATE("fridgeState", "fridgestate", StringType.class, false),
52 SUPERCOOL(null, SUPERCOOL_CHANNEL_ID, OnOffType.class, false),
53 SUPERFREEZE(null, SUPERFREEZE_CHANNEL_ID, OnOffType.class, false),
54 FREEZERCURRENTTEMP("freezerCurrentTemperature", "freezercurrent", QuantityType.class, false) {
56 public State getState(String s, DeviceMetaData dmd) {
57 return getTemperatureState(s);
60 FREEZERTARGETTEMP("freezerTargetTemperature", "freezertarget", QuantityType.class, false) {
62 public State getState(String s, DeviceMetaData dmd) {
63 return getTemperatureState(s);
66 FRIDGECURRENTTEMP("fridgeCurrentTemperature", "fridgecurrent", QuantityType.class, false) {
68 public State getState(String s, DeviceMetaData dmd) {
69 return getTemperatureState(s);
72 FRIDGETARGETTEMP("fridgeTargetTemperature", "fridgetarget", QuantityType.class, false) {
74 public State getState(String s, DeviceMetaData dmd) {
75 return getTemperatureState(s);
78 DOOR("signalDoor", "door", OpenClosedType.class, false) {
81 public State getState(String s, DeviceMetaData dmd) {
82 if ("true".equals(s)) {
83 return getState("OPEN");
86 if ("false".equals(s)) {
87 return getState("CLOSED");
90 return UnDefType.UNDEF;
93 START(null, "start", OnOffType.class, false);
95 private final Logger logger = LoggerFactory.getLogger(FridgeFreezerChannelSelector.class);
97 private final String mieleID;
98 private final String channelID;
99 private final Class<? extends Type> typeClass;
100 private final boolean isProperty;
102 FridgeFreezerChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
103 boolean isProperty) {
104 this.mieleID = propertyID;
105 this.channelID = channelID;
106 this.typeClass = typeClass;
107 this.isProperty = isProperty;
111 public String toString() {
116 public String getMieleID() {
121 public String getChannelID() {
126 public boolean isProperty() {
131 public boolean isExtendedState() {
136 public State getState(String s, DeviceMetaData dmd) {
138 String localizedValue = getMieleEnum(s, dmd);
139 if (localizedValue == null) {
140 localizedValue = dmd.LocalizedValue;
142 if (localizedValue == null) {
146 return getState(localizedValue);
152 public State getState(String s) {
154 Method valueOf = typeClass.getMethod("valueOf", String.class);
155 State state = (State) valueOf.invoke(typeClass, s);
159 } catch (Exception e) {
160 logger.error("An exception occurred while converting '{}' into a State", s);
166 public State getTemperatureState(String s) {
168 return ExtendedDeviceStateUtil.getTemperatureState(s);
169 } catch (NumberFormatException e) {
170 logger.warn("An exception occurred while converting '{}' into a State", s);
171 return UnDefType.UNDEF;
175 public String getMieleEnum(String s, DeviceMetaData dmd) {
176 if (dmd.MieleEnum != null) {
177 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
178 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
179 return enumEntry.getKey();