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 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 with
33 * a freezer compartment
35 * @author Karel Goderis - Initial contribution
37 public enum FridgeFreezerChannelSelector implements ApplianceChannelSelector {
39 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
40 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
41 BRAND_ID("brandId", "brandId", StringType.class, true),
42 COMPANY_ID("companyId", "companyId", StringType.class, true),
43 STATE("state", "state", StringType.class, false),
44 FREEZERSTATE("freezerState", "freezerstate", StringType.class, false),
45 FRIDGESTATE("fridgeState", "fridgestate", StringType.class, false),
46 SUPERCOOL(null, "supercool", OnOffType.class, false),
47 SUPERFREEZE(null, "superfreeze", OnOffType.class, false),
48 FREEZERCURRENTTEMP("freezerCurrentTemperature", "freezercurrent", DecimalType.class, false) {
50 public State getState(String s, DeviceMetaData dmd) {
54 FREEZERTARGETTEMP("freezerTargetTemperature", "freezertarget", DecimalType.class, false) {
56 public State getState(String s, DeviceMetaData dmd) {
60 FRIDGECURRENTTEMP("fridgeCurrentTemperature", "fridgecurrent", DecimalType.class, false) {
62 public State getState(String s, DeviceMetaData dmd) {
66 FRIDGETARGETTEMP("fridgeTargetTemperature", "fridgetarget", DecimalType.class, false) {
68 public State getState(String s, DeviceMetaData dmd) {
72 DOOR("signalDoor", "door", OpenClosedType.class, false) {
75 public State getState(String s, DeviceMetaData dmd) {
76 if ("true".equals(s)) {
77 return getState("OPEN");
80 if ("false".equals(s)) {
81 return getState("CLOSED");
84 return UnDefType.UNDEF;
87 START(null, "start", OnOffType.class, false);
89 private final Logger logger = LoggerFactory.getLogger(FridgeFreezerChannelSelector.class);
91 private final String mieleID;
92 private final String channelID;
93 private final Class<? extends Type> typeClass;
94 private final boolean isProperty;
96 FridgeFreezerChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
98 this.mieleID = propertyID;
99 this.channelID = channelID;
100 this.typeClass = typeClass;
101 this.isProperty = isProperty;
105 public String toString() {
110 public String getMieleID() {
115 public String getChannelID() {
120 public Class<? extends Type> getTypeClass() {
125 public boolean isProperty() {
130 public State getState(String s, DeviceMetaData dmd) {
132 String localizedValue = getMieleEnum(s, dmd);
133 if (localizedValue == null) {
134 localizedValue = dmd.LocalizedValue;
136 if (localizedValue == null) {
140 return getState(localizedValue);
146 public State getState(String s) {
148 Method valueOf = typeClass.getMethod("valueOf", String.class);
149 State state = (State) valueOf.invoke(typeClass, s);
153 } catch (Exception e) {
154 logger.error("An exception occurred while converting '{}' into a State", s);
160 public String getMieleEnum(String s, DeviceMetaData dmd) {
161 if (dmd.MieleEnum != null) {
162 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
163 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
164 return enumEntry.getKey();