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, MieleTranslationProvider translationProvider) {
46 return DeviceUtil.getStateTextState(s, dmd, translationProvider);
49 STATE("", STATE_CHANNEL_ID, DecimalType.class, false),
50 PLATES("plateNumbers", "plates", DecimalType.class, true),
51 PLATE1_POWER("plate1PowerStep", "plate1power", DecimalType.class, false),
52 PLATE1_HEAT("plate1RemainingHeat", "plate1heat", DecimalType.class, false) {
54 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
55 // If there is remaining heat, the device metadata contains some informative string which can not be
56 // converted into a DecimalType. We therefore ignore the metadata and return the device property value as a
61 PLATE1_TIME("plate1RemainingTime", "plate1time", StringType.class, false),
62 PLATE2_POWER("plate2PowerStep", "plate2power", DecimalType.class, false),
63 PLATE2_HEAT("plate2RemainingHeat", "plate2heat", DecimalType.class, false) {
65 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
69 PLATE2_TIME("plate2RemainingTime", "plate2time", StringType.class, false),
70 PLATE3_POWER("plate3PowerStep", "plate3power", DecimalType.class, false),
71 PLATE3_HEAT("plate3RemainingHeat", "plate3heat", DecimalType.class, false) {
73 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
77 PLATE3_TIME("plate3RemainingTime", "plate3time", StringType.class, false),
78 PLATE4_POWER("plate4PowerStep", "plate4power", DecimalType.class, false),
79 PLATE4_HEAT("plate4RemainingHeat", "plate4heat", DecimalType.class, false) {
81 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
85 PLATE4_TIME("plate4RemainingTime", "plate4time", StringType.class, false),
86 PLATE5_POWER("plate5PowerStep", "plate5power", DecimalType.class, false),
87 PLATE5_HEAT("plate5RemainingHeat", "plate5heat", DecimalType.class, false) {
89 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
93 PLATE5_TIME("plate5RemainingTime", "plate5time", StringType.class, false),
94 PLATE6_POWER("plate6PowerStep", "plate6power", DecimalType.class, false),
95 PLATE6_HEAT("plate6RemainingHeat", "plate6heat", DecimalType.class, false) {
97 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
101 PLATE6_TIME("plate6RemainingTime", "plate6time", StringType.class, false);
103 private final Logger logger = LoggerFactory.getLogger(HobChannelSelector.class);
105 private final String mieleID;
106 private final String channelID;
107 private final Class<? extends Type> typeClass;
108 private final boolean isProperty;
110 HobChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
111 this.mieleID = propertyID;
112 this.channelID = channelID;
113 this.typeClass = typeClass;
114 this.isProperty = isProperty;
118 public String toString() {
123 public String getMieleID() {
128 public String getChannelID() {
133 public boolean isProperty() {
138 public boolean isExtendedState() {
143 public State getState(String s, @Nullable DeviceMetaData dmd, MieleTranslationProvider translationProvider) {
144 return this.getState(s, dmd);
148 public State getState(String s, @Nullable DeviceMetaData dmd) {
150 String localizedValue = dmd.getMieleEnum(s);
151 if (localizedValue == null) {
152 localizedValue = dmd.LocalizedValue;
154 if (localizedValue == null) {
158 return getState(localizedValue);
164 public State getState(String s) {
166 Method valueOf = typeClass.getMethod("valueOf", String.class);
167 State state = (State) valueOf.invoke(typeClass, s);
171 } catch (Exception e) {
172 logger.warn("An exception occurred while converting '{}' into a State", s);
175 return UnDefType.UNDEF;