2 * Copyright (c) 2010-2020 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.OnOffType;
20 import org.openhab.core.library.types.OpenClosedType;
21 import org.openhab.core.library.types.StringType;
22 import org.openhab.core.types.State;
23 import org.openhab.core.types.Type;
24 import org.openhab.core.types.UnDefType;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.gson.JsonElement;
31 * The {@link ApplianceChannelSelector} for coffee machines
33 * @author Stephan Esch - Initial contribution
35 public enum CoffeeMachineChannelSelector implements ApplianceChannelSelector {
37 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
38 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
39 BRAND_ID("brandId", "brandId", StringType.class, true),
40 COMPANY_ID("companyId", "companyId", StringType.class, true),
41 STATE("state", "state", StringType.class, false),
42 PROGRAMID("programId", "program", StringType.class, false),
43 PROGRAMTYPE("programType", "type", StringType.class, false),
44 PROGRAMPHASE("phase", "phase", StringType.class, false),
45 // lightingStatus signalFailure signalInfo
46 DOOR("signalDoor", "door", OpenClosedType.class, false) {
48 public State getState(String s, DeviceMetaData dmd) {
49 if ("true".equals(s)) {
50 return getState("OPEN");
53 if ("false".equals(s)) {
54 return getState("CLOSED");
57 return UnDefType.UNDEF;
60 SWITCH(null, "switch", OnOffType.class, false);
62 private final Logger logger = LoggerFactory.getLogger(CoffeeMachineChannelSelector.class);
64 private final String mieleID;
65 private final String channelID;
66 private final Class<? extends Type> typeClass;
67 private final boolean isProperty;
69 CoffeeMachineChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
71 this.mieleID = propertyID;
72 this.channelID = channelID;
73 this.typeClass = typeClass;
74 this.isProperty = isProperty;
78 public String toString() {
83 public String getMieleID() {
88 public String getChannelID() {
93 public Class<? extends Type> getTypeClass() {
98 public boolean isProperty() {
103 public State getState(String s, DeviceMetaData dmd) {
105 String localizedValue = getMieleEnum(s, dmd);
106 if (localizedValue == null) {
107 localizedValue = dmd.LocalizedValue;
109 if (localizedValue == null) {
113 return getState(localizedValue);
119 public State getState(String s) {
121 Method valueOf = typeClass.getMethod("valueOf", String.class);
122 State state = (State) valueOf.invoke(typeClass, s);
126 } catch (Exception e) {
127 logger.error("An exception occurred while converting '{}' into a State", s);
133 public String getMieleEnum(String s, DeviceMetaData dmd) {
134 if (dmd.MieleEnum != null) {
135 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
136 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
137 return enumEntry.getKey();