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 BRAND_ID("brandId", "brandId", StringType.class, true),
43 COMPANY_ID("companyId", "companyId", StringType.class, true),
44 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
45 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
46 VENTILATION("ventilationPower", "ventilation", DecimalType.class, false),
47 LIGHT("lightingStatus", "light", OnOffType.class, false) {
50 public State getState(String s, DeviceMetaData dmd) {
51 if ("true".equals(s)) {
52 return getState("ON");
55 if ("false".equals(s)) {
56 return getState("OFF");
59 return UnDefType.UNDEF;
62 STOP(null, "stop", OnOffType.class, false);
64 private final Logger logger = LoggerFactory.getLogger(HoodChannelSelector.class);
66 private final String mieleID;
67 private final String channelID;
68 private final Class<? extends Type> typeClass;
69 private final boolean isProperty;
71 HoodChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
72 this.mieleID = propertyID;
73 this.channelID = channelID;
74 this.typeClass = typeClass;
75 this.isProperty = isProperty;
79 public String toString() {
84 public String getMieleID() {
89 public String getChannelID() {
94 public boolean isProperty() {
99 public boolean isExtendedState() {
104 public State getState(String s, DeviceMetaData dmd) {
106 String localizedValue = getMieleEnum(s, dmd);
107 if (localizedValue == null) {
108 localizedValue = dmd.LocalizedValue;
110 if (localizedValue == null) {
114 return getState(localizedValue);
120 public State getState(String s) {
122 Method valueOf = typeClass.getMethod("valueOf", String.class);
123 State state = (State) valueOf.invoke(typeClass, s);
127 } catch (Exception e) {
128 logger.error("An exception occurred while converting '{}' into a State", s);
134 public String getMieleEnum(String s, DeviceMetaData dmd) {
135 if (dmd.MieleEnum != null) {
136 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
137 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
138 return enumEntry.getKey();