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.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.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
26 import org.openhab.core.types.UnDefType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.gson.JsonElement;
33 * The {@link ApplianceChannelSelector} for ventilation hoods
35 * @author Karel Goderis - Initial contribution
36 * @author Jacob Laursen - Added raw channels
38 public enum HoodChannelSelector implements ApplianceChannelSelector {
40 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
41 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
42 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
43 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
44 VENTILATION("ventilationPower", "ventilation", DecimalType.class, false),
45 LIGHT("lightingStatus", "light", OnOffType.class, false) {
48 public State getState(String s, DeviceMetaData dmd) {
49 if ("true".equals(s)) {
50 return getState("ON");
53 if ("false".equals(s)) {
54 return getState("OFF");
57 return UnDefType.UNDEF;
60 STOP(null, "stop", OnOffType.class, false);
62 private final Logger logger = LoggerFactory.getLogger(HoodChannelSelector.class);
64 private final String mieleID;
65 private final String channelID;
66 private final Class<? extends Type> typeClass;
67 private final boolean isProperty;
69 HoodChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
70 this.mieleID = propertyID;
71 this.channelID = channelID;
72 this.typeClass = typeClass;
73 this.isProperty = isProperty;
77 public String toString() {
82 public String getMieleID() {
87 public String getChannelID() {
92 public boolean isProperty() {
97 public boolean isExtendedState() {
102 public State getState(String s, DeviceMetaData dmd) {
104 String localizedValue = getMieleEnum(s, dmd);
105 if (localizedValue == null) {
106 localizedValue = dmd.LocalizedValue;
108 if (localizedValue == null) {
112 return getState(localizedValue);
118 public State getState(String s) {
120 Method valueOf = typeClass.getMethod("valueOf", String.class);
121 State state = (State) valueOf.invoke(typeClass, s);
125 } catch (Exception e) {
126 logger.error("An exception occurred while converting '{}' into a State", s);
132 public String getMieleEnum(String s, DeviceMetaData dmd) {
133 if (dmd.MieleEnum != null) {
134 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
135 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
136 return enumEntry.getKey();