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.StringType;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.Type;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.gson.JsonElement;
31 * The {@link ApplianceChannelSelector} for hobs
33 * @author Karel Goderis - Initial contribution
34 * @author Jacob Laursen - Added raw channels
36 public enum HobChannelSelector implements ApplianceChannelSelector {
38 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
39 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
40 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
41 STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
42 PLATES("plateNumbers", "plates", DecimalType.class, true),
43 PLATE1_POWER("plate1PowerStep", "plate1power", DecimalType.class, false),
44 PLATE1_HEAT("plate1RemainingHeat", "plate1heat", DecimalType.class, false) {
46 public State getState(String s, DeviceMetaData dmd) {
47 // If there is remaining heat, the device metadata contains some informative string which can not be
48 // converted into a DecimalType. We therefore ignore the metadata and return the device property value as a
53 PLATE1_TIME("plate1RemainingTime", "plate1time", StringType.class, false),
54 PLATE2_POWER("plate2PowerStep", "plate2power", DecimalType.class, false),
55 PLATE2_HEAT("plate2RemainingHeat", "plate2heat", DecimalType.class, false) {
57 public State getState(String s, DeviceMetaData dmd) {
61 PLATE2_TIME("plate2RemainingTime", "plate2time", StringType.class, false),
62 PLATE3_POWER("plate3PowerStep", "plate3power", DecimalType.class, false),
63 PLATE3_HEAT("plate3RemainingHeat", "plate3heat", DecimalType.class, false) {
65 public State getState(String s, DeviceMetaData dmd) {
69 PLATE3_TIME("plate3RemainingTime", "plate3time", StringType.class, false),
70 PLATE4_POWER("plate4PowerStep", "plate4power", DecimalType.class, false),
71 PLATE4_HEAT("plate4RemainingHeat", "plate4heat", DecimalType.class, false) {
73 public State getState(String s, DeviceMetaData dmd) {
77 PLATE4_TIME("plate4RemainingTime", "plate4time", StringType.class, false),
78 PLATE5_POWER("plate5PowerStep", "plate5power", DecimalType.class, false),
79 PLATE5_HEAT("plate5RemainingHeat", "plate5heat", DecimalType.class, false) {
81 public State getState(String s, DeviceMetaData dmd) {
85 PLATE5_TIME("plate5RemainingTime", "plate5time", StringType.class, false),
86 PLATE6_POWER("plate6PowerStep", "plate6power", DecimalType.class, false),
87 PLATE6_HEAT("plate6RemainingHeat", "plate6heat", DecimalType.class, false) {
89 public State getState(String s, DeviceMetaData dmd) {
93 PLATE6_TIME("plate6RemainingTime", "plate6time", StringType.class, false);
95 private final Logger logger = LoggerFactory.getLogger(HobChannelSelector.class);
97 private final String mieleID;
98 private final String channelID;
99 private final Class<? extends Type> typeClass;
100 private final boolean isProperty;
102 HobChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
103 this.mieleID = propertyID;
104 this.channelID = channelID;
105 this.typeClass = typeClass;
106 this.isProperty = isProperty;
110 public String toString() {
115 public String getMieleID() {
120 public String getChannelID() {
125 public boolean isProperty() {
130 public boolean isExtendedState() {
135 public State getState(String s, DeviceMetaData dmd) {
137 String localizedValue = getMieleEnum(s, dmd);
138 if (localizedValue == null) {
139 localizedValue = dmd.LocalizedValue;
141 if (localizedValue == null) {
145 return getState(localizedValue);
151 public State getState(String s) {
153 Method valueOf = typeClass.getMethod("valueOf", String.class);
154 State state = (State) valueOf.invoke(typeClass, s);
158 } catch (Exception e) {
159 logger.error("An exception occurred while converting '{}' into a State", s);
165 public String getMieleEnum(String s, DeviceMetaData dmd) {
166 if (dmd.MieleEnum != null) {
167 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
168 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
169 return enumEntry.getKey();