2 * Copyright (c) 2010-2022 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;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.miele.internal.DeviceUtil;
22 import org.openhab.binding.miele.internal.MieleTranslationProvider;
23 import org.openhab.binding.miele.internal.api.dto.DeviceMetaData;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.Type;
28 import org.openhab.core.types.UnDefType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link ApplianceChannelSelector} for hobs
35 * @author Karel Goderis - Initial contribution
36 * @author Jacob Laursen - Added raw channels
39 public enum HobChannelSelector implements ApplianceChannelSelector {
41 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
42 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
43 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false) {
45 public State getState(String s, @Nullable DeviceMetaData dmd,
46 @Nullable MieleTranslationProvider translationProvider) {
47 return DeviceUtil.getStateTextState(s, dmd, translationProvider);
50 STATE("", STATE_CHANNEL_ID, DecimalType.class, false),
51 PLATES("plateNumbers", "plates", DecimalType.class, true),
52 PLATE1_POWER("plate1PowerStep", "plate1power", DecimalType.class, false),
53 PLATE1_HEAT("plate1RemainingHeat", "plate1heat", DecimalType.class, false) {
55 public State getState(String s, @Nullable DeviceMetaData dmd,
56 @Nullable MieleTranslationProvider translationProvider) {
57 // If there is remaining heat, the device metadata contains some informative string which can not be
58 // converted into a DecimalType. We therefore ignore the metadata and return the device property value as a
63 PLATE1_TIME("plate1RemainingTime", "plate1time", StringType.class, false),
64 PLATE2_POWER("plate2PowerStep", "plate2power", DecimalType.class, false),
65 PLATE2_HEAT("plate2RemainingHeat", "plate2heat", DecimalType.class, false) {
67 public State getState(String s, @Nullable DeviceMetaData dmd,
68 @Nullable MieleTranslationProvider translationProvider) {
72 PLATE2_TIME("plate2RemainingTime", "plate2time", StringType.class, false),
73 PLATE3_POWER("plate3PowerStep", "plate3power", DecimalType.class, false),
74 PLATE3_HEAT("plate3RemainingHeat", "plate3heat", DecimalType.class, false) {
76 public State getState(String s, @Nullable DeviceMetaData dmd,
77 @Nullable MieleTranslationProvider translationProvider) {
81 PLATE3_TIME("plate3RemainingTime", "plate3time", StringType.class, false),
82 PLATE4_POWER("plate4PowerStep", "plate4power", DecimalType.class, false),
83 PLATE4_HEAT("plate4RemainingHeat", "plate4heat", DecimalType.class, false) {
85 public State getState(String s, @Nullable DeviceMetaData dmd,
86 @Nullable MieleTranslationProvider translationProvider) {
90 PLATE4_TIME("plate4RemainingTime", "plate4time", StringType.class, false),
91 PLATE5_POWER("plate5PowerStep", "plate5power", DecimalType.class, false),
92 PLATE5_HEAT("plate5RemainingHeat", "plate5heat", DecimalType.class, false) {
94 public State getState(String s, @Nullable DeviceMetaData dmd,
95 @Nullable MieleTranslationProvider translationProvider) {
99 PLATE5_TIME("plate5RemainingTime", "plate5time", StringType.class, false),
100 PLATE6_POWER("plate6PowerStep", "plate6power", DecimalType.class, false),
101 PLATE6_HEAT("plate6RemainingHeat", "plate6heat", DecimalType.class, false) {
103 public State getState(String s, @Nullable DeviceMetaData dmd,
104 @Nullable MieleTranslationProvider translationProvider) {
108 PLATE6_TIME("plate6RemainingTime", "plate6time", StringType.class, false);
110 private final Logger logger = LoggerFactory.getLogger(HobChannelSelector.class);
112 private final String mieleID;
113 private final String channelID;
114 private final Class<? extends Type> typeClass;
115 private final boolean isProperty;
117 HobChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
118 this.mieleID = propertyID;
119 this.channelID = channelID;
120 this.typeClass = typeClass;
121 this.isProperty = isProperty;
125 public String toString() {
130 public String getMieleID() {
135 public String getChannelID() {
140 public boolean isProperty() {
145 public boolean isExtendedState() {
150 public State getState(String s, @Nullable DeviceMetaData dmd,
151 @Nullable MieleTranslationProvider translationProvider) {
152 return this.getState(s, dmd);
156 public State getState(String s, @Nullable DeviceMetaData dmd) {
158 String localizedValue = dmd.getMieleEnum(s);
159 if (localizedValue == null) {
160 localizedValue = dmd.LocalizedValue;
162 if (localizedValue == null) {
166 return getState(localizedValue);
172 public State getState(String s) {
174 Method valueOf = typeClass.getMethod("valueOf", String.class);
175 State state = (State) valueOf.invoke(typeClass, s);
179 } catch (Exception e) {
180 logger.error("An exception occurred while converting '{}' into a State", s);
183 return UnDefType.UNDEF;