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.Collections;
20 import java.util.Map.Entry;
22 import org.openhab.binding.miele.internal.handler.MieleBridgeHandler.DeviceMetaData;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.OpenClosedType;
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 coffee machines
38 * @author Stephan Esch - Initial contribution
39 * @author Jacob Laursen - Added raw channels
41 public enum CoffeeMachineChannelSelector implements ApplianceChannelSelector {
43 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
44 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
45 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
46 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
47 PROGRAM_TEXT(PROGRAM_ID_PROPERTY_NAME, PROGRAM_TEXT_CHANNEL_ID, StringType.class, false) {
49 public State getState(String s, DeviceMetaData dmd) {
50 State state = getTextState(s, dmd, programs, MISSING_PROGRAM_TEXT_PREFIX);
54 return super.getState(s, dmd);
57 PROGRAM(null, PROGRAM_CHANNEL_ID, DecimalType.class, false),
58 PROGRAMTYPE("programType", "type", StringType.class, false),
59 PROGRAM_PHASE_TEXT(PHASE_PROPERTY_NAME, PHASE_TEXT_CHANNEL_ID, StringType.class, false) {
61 public State getState(String s, DeviceMetaData dmd) {
62 State state = getTextState(s, dmd, phases, MISSING_PHASE_TEXT_PREFIX);
66 return super.getState(s, dmd);
69 PROGRAM_PHASE(RAW_PHASE_PROPERTY_NAME, PHASE_CHANNEL_ID, DecimalType.class, false),
70 // lightingStatus signalFailure signalInfo
71 DOOR("signalDoor", "door", OpenClosedType.class, false) {
73 public State getState(String s, DeviceMetaData dmd) {
74 if ("true".equals(s)) {
75 return getState("OPEN");
78 if ("false".equals(s)) {
79 return getState("CLOSED");
82 return UnDefType.UNDEF;
85 SWITCH(null, "switch", OnOffType.class, false);
87 private final Logger logger = LoggerFactory.getLogger(CoffeeMachineChannelSelector.class);
89 private static final Map<String, String> programs = Collections.<String, String> emptyMap();
91 private static final Map<String, String> phases = Collections.<String, String> emptyMap();
93 private final String mieleID;
94 private final String channelID;
95 private final Class<? extends Type> typeClass;
96 private final boolean isProperty;
98 CoffeeMachineChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass,
100 this.mieleID = propertyID;
101 this.channelID = channelID;
102 this.typeClass = typeClass;
103 this.isProperty = isProperty;
107 public String toString() {
112 public String getMieleID() {
117 public String getChannelID() {
122 public boolean isProperty() {
127 public boolean isExtendedState() {
132 public State getState(String s, DeviceMetaData dmd) {
134 String localizedValue = getMieleEnum(s, dmd);
135 if (localizedValue == null) {
136 localizedValue = dmd.LocalizedValue;
138 if (localizedValue == null) {
142 return getState(localizedValue);
148 public State getState(String s) {
150 Method valueOf = typeClass.getMethod("valueOf", String.class);
151 State state = (State) valueOf.invoke(typeClass, s);
155 } catch (Exception e) {
156 logger.error("An exception occurred while converting '{}' into a State", s);
162 public State getTextState(String s, DeviceMetaData dmd, Map<String, String> valueMap, String prefix) {
164 return UnDefType.UNDEF;
167 if (dmd == null || dmd.LocalizedValue == null || dmd.LocalizedValue.startsWith(prefix)) {
168 String text = valueMap.get(s);
170 return getState(text);
172 if (dmd == null || dmd.LocalizedValue == null) {
173 return getState(prefix + s);
180 public String getMieleEnum(String s, DeviceMetaData dmd) {
181 if (dmd.MieleEnum != null) {
182 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
183 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
184 return enumEntry.getKey();