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.handler.MieleBridgeHandler.DeviceMetaData;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.OpenClosedType;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.types.State;
26 import org.openhab.core.types.Type;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.gson.JsonElement;
34 * The {@link ApplianceChannelSelector} for fridges
36 * @author Karel Goderis - Initial contribution
38 public enum FridgeChannelSelector implements ApplianceChannelSelector {
40 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
41 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
42 BRAND_ID("brandId", "brandId", StringType.class, true),
43 COMPANY_ID("companyId", "companyId", StringType.class, true),
44 STATE("state", "state", StringType.class, false),
45 SUPERCOOL(null, SUPERCOOL_CHANNEL_ID, OnOffType.class, false),
46 FRIDGECURRENTTEMP("currentTemperature", "current", DecimalType.class, false) {
48 public State getState(String s, DeviceMetaData dmd) {
52 FRIDGETARGETTEMP("targetTemperature", "target", DecimalType.class, false) {
54 public State getState(String s, DeviceMetaData dmd) {
58 DOOR("signalDoor", "door", OpenClosedType.class, false) {
61 public State getState(String s, DeviceMetaData dmd) {
62 if ("true".equals(s)) {
63 return getState("OPEN");
66 if ("false".equals(s)) {
67 return getState("CLOSED");
70 return UnDefType.UNDEF;
73 START(null, "start", OnOffType.class, false);
75 private final Logger logger = LoggerFactory.getLogger(FridgeChannelSelector.class);
77 private final String mieleID;
78 private final String channelID;
79 private final Class<? extends Type> typeClass;
80 private final boolean isProperty;
82 FridgeChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
83 this.mieleID = propertyID;
84 this.channelID = channelID;
85 this.typeClass = typeClass;
86 this.isProperty = isProperty;
90 public String toString() {
95 public String getMieleID() {
100 public String getChannelID() {
105 public boolean isProperty() {
110 public boolean isExtendedState() {
115 public State getState(String s, DeviceMetaData dmd) {
117 String localizedValue = getMieleEnum(s, dmd);
118 if (localizedValue == null) {
119 localizedValue = dmd.LocalizedValue;
121 if (localizedValue == null) {
125 return getState(localizedValue);
131 public State getState(String s) {
133 Method valueOf = typeClass.getMethod("valueOf", String.class);
134 State state = (State) valueOf.invoke(typeClass, s);
138 } catch (Exception e) {
139 logger.error("An exception occurred while converting '{}' into a State", s);
145 public String getMieleEnum(String s, DeviceMetaData dmd) {
146 if (dmd.MieleEnum != null) {
147 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
148 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
149 return enumEntry.getKey();