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 BRAND_ID("brandId", "brandId", StringType.class, true),
41 COMPANY_ID("companyId", "companyId", 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 PLATES("plateNumbers", "plates", DecimalType.class, true),
45 PLATE1_POWER("plate1PowerStep", "plate1power", DecimalType.class, false),
46 PLATE1_HEAT("plate1RemainingHeat", "plate1heat", DecimalType.class, false) {
48 public State getState(String s, DeviceMetaData dmd) {
49 // If there is remaining heat, the device metadata contains some informative string which can not be
50 // converted into a DecimalType. We therefore ignore the metadata and return the device property value as a
55 PLATE1_TIME("plate1RemainingTime", "plate1time", StringType.class, false),
56 PLATE2_POWER("plate2PowerStep", "plate2power", DecimalType.class, false),
57 PLATE2_HEAT("plate2RemainingHeat", "plate2heat", DecimalType.class, false) {
59 public State getState(String s, DeviceMetaData dmd) {
63 PLATE2_TIME("plate2RemainingTime", "plate2time", StringType.class, false),
64 PLATE3_POWER("plate3PowerStep", "plate3power", DecimalType.class, false),
65 PLATE3_HEAT("plate3RemainingHeat", "plate3heat", DecimalType.class, false) {
67 public State getState(String s, DeviceMetaData dmd) {
71 PLATE3_TIME("plate3RemainingTime", "plate3time", StringType.class, false),
72 PLATE4_POWER("plate4PowerStep", "plate4power", DecimalType.class, false),
73 PLATE4_HEAT("plate4RemainingHeat", "plate4heat", DecimalType.class, false) {
75 public State getState(String s, DeviceMetaData dmd) {
79 PLATE4_TIME("plate4RemainingTime", "plate4time", StringType.class, false),
80 PLATE5_POWER("plate5PowerStep", "plate5power", DecimalType.class, false),
81 PLATE5_HEAT("plate5RemainingHeat", "plate5heat", DecimalType.class, false) {
83 public State getState(String s, DeviceMetaData dmd) {
87 PLATE5_TIME("plate5RemainingTime", "plate5time", StringType.class, false),
88 PLATE6_POWER("plate6PowerStep", "plate6power", DecimalType.class, false),
89 PLATE6_HEAT("plate6RemainingHeat", "plate6heat", DecimalType.class, false) {
91 public State getState(String s, DeviceMetaData dmd) {
95 PLATE6_TIME("plate6RemainingTime", "plate6time", StringType.class, false);
97 private final Logger logger = LoggerFactory.getLogger(HobChannelSelector.class);
99 private final String mieleID;
100 private final String channelID;
101 private final Class<? extends Type> typeClass;
102 private final boolean isProperty;
104 HobChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
105 this.mieleID = propertyID;
106 this.channelID = channelID;
107 this.typeClass = typeClass;
108 this.isProperty = isProperty;
112 public String toString() {
117 public String getMieleID() {
122 public String getChannelID() {
127 public boolean isProperty() {
132 public boolean isExtendedState() {
137 public State getState(String s, DeviceMetaData dmd) {
139 String localizedValue = getMieleEnum(s, dmd);
140 if (localizedValue == null) {
141 localizedValue = dmd.LocalizedValue;
143 if (localizedValue == null) {
147 return getState(localizedValue);
153 public State getState(String s) {
155 Method valueOf = typeClass.getMethod("valueOf", String.class);
156 State state = (State) valueOf.invoke(typeClass, s);
160 } catch (Exception e) {
161 logger.error("An exception occurred while converting '{}' into a State", s);
167 public String getMieleEnum(String s, DeviceMetaData dmd) {
168 if (dmd.MieleEnum != null) {
169 for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
170 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
171 return enumEntry.getKey();