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.DecimalType;
20 import org.openhab.core.library.types.StringType;
21 import org.openhab.core.types.State;
22 import org.openhab.core.types.Type;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import com.google.gson.JsonElement;
29 * The {@link ApplianceChannelSelector} for hobs
31 * @author Karel Goderis - Initial contribution
33 public enum HobChannelSelector implements ApplianceChannelSelector {
35 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
36 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
37 BRAND_ID("brandId", "brandId", StringType.class, true),
38 COMPANY_ID("companyId", "companyId", StringType.class, true),
39 STATE("state", "state", StringType.class, false),
40 PLATES("plateNumbers", "plates", DecimalType.class, true),
41 PLATE1_POWER("plate1PowerStep", "plate1power", DecimalType.class, false),
42 PLATE1_HEAT("plate1RemainingHeat", "plate1heat", DecimalType.class, false) {
44 public State getState(String s, DeviceMetaData dmd) {
45 // If there is remaining heat, the device metadata contains some informative string which can not be
46 // converted into a DecimalType. We therefore ignore the metadata and return the device property value as a
51 PLATE1_TIME("plate1RemainingTime", "plate1time", StringType.class, false),
52 PLATE2_POWER("plate2PowerStep", "plate2power", DecimalType.class, false),
53 PLATE2_HEAT("plate2RemainingHeat", "plate2heat", DecimalType.class, false) {
55 public State getState(String s, DeviceMetaData dmd) {
59 PLATE2_TIME("plate2RemainingTime", "plate2time", StringType.class, false),
60 PLATE3_POWER("plate3PowerStep", "plate3power", DecimalType.class, false),
61 PLATE3_HEAT("plate3RemainingHeat", "plate3heat", DecimalType.class, false) {
63 public State getState(String s, DeviceMetaData dmd) {
67 PLATE3_TIME("plate3RemainingTime", "plate3time", StringType.class, false),
68 PLATE4_POWER("plate4PowerStep", "plate4power", DecimalType.class, false),
69 PLATE4_HEAT("plate4RemainingHeat", "plate4heat", DecimalType.class, false) {
71 public State getState(String s, DeviceMetaData dmd) {
75 PLATE4_TIME("plate4RemainingTime", "plate4time", StringType.class, false),
76 PLATE5_POWER("plate5PowerStep", "plate5power", DecimalType.class, false),
77 PLATE5_HEAT("plate5RemainingHeat", "plate5heat", DecimalType.class, false) {
79 public State getState(String s, DeviceMetaData dmd) {
83 PLATE5_TIME("plate5RemainingTime", "plate5time", StringType.class, false),
84 PLATE6_POWER("plate6PowerStep", "plate6power", DecimalType.class, false),
85 PLATE6_HEAT("plate6RemainingHeat", "plate6heat", DecimalType.class, false) {
87 public State getState(String s, DeviceMetaData dmd) {
91 PLATE6_TIME("plate6RemainingTime", "plate6time", StringType.class, false);
93 private final Logger logger = LoggerFactory.getLogger(HobChannelSelector.class);
95 private final String mieleID;
96 private final String channelID;
97 private final Class<? extends Type> typeClass;
98 private final boolean isProperty;
100 HobChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
101 this.mieleID = propertyID;
102 this.channelID = channelID;
103 this.typeClass = typeClass;
104 this.isProperty = isProperty;
108 public String toString() {
113 public String getMieleID() {
118 public String getChannelID() {
123 public Class<? extends Type> getTypeClass() {
128 public boolean isProperty() {
133 public State getState(String s, DeviceMetaData dmd) {
135 String localizedValue = getMieleEnum(s, dmd);
136 if (localizedValue == null) {
137 localizedValue = dmd.LocalizedValue;
139 if (localizedValue == null) {
143 return getState(localizedValue);
149 public State getState(String s) {
151 Method valueOf = typeClass.getMethod("valueOf", String.class);
152 State state = (State) valueOf.invoke(typeClass, s);
156 } catch (Exception e) {
157 logger.error("An exception occurred while converting '{}' into a State", s);
163 public String getMieleEnum(String s, DeviceMetaData dmd) {
164 if (dmd.MieleEnum != null) {
165 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
166 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
167 return enumEntry.getKey();