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.OnOffType;
26 import org.openhab.core.library.types.StringType;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.Type;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * The {@link ApplianceChannelSelector} for ventilation hoods
36 * @author Karel Goderis - Initial contribution
37 * @author Jacob Laursen - Added raw channels
40 public enum HoodChannelSelector implements ApplianceChannelSelector {
42 PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
43 DEVICE_TYPE("mieleDeviceType", "deviceType", StringType.class, true),
44 STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false) {
46 public State getState(String s, @Nullable DeviceMetaData dmd,
47 @Nullable MieleTranslationProvider translationProvider) {
48 return DeviceUtil.getStateTextState(s, dmd, translationProvider);
51 STATE("", STATE_CHANNEL_ID, DecimalType.class, false),
52 VENTILATION("ventilationPower", "ventilation", DecimalType.class, false),
53 LIGHT("lightingStatus", "light", OnOffType.class, false) {
56 public State getState(String s, @Nullable DeviceMetaData dmd,
57 @Nullable MieleTranslationProvider translationProvider) {
58 if ("true".equals(s)) {
59 return getState("ON");
62 if ("false".equals(s)) {
63 return getState("OFF");
66 return UnDefType.UNDEF;
69 STOP("", "stop", OnOffType.class, false);
71 private final Logger logger = LoggerFactory.getLogger(HoodChannelSelector.class);
73 private final String mieleID;
74 private final String channelID;
75 private final Class<? extends Type> typeClass;
76 private final boolean isProperty;
78 HoodChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
79 this.mieleID = propertyID;
80 this.channelID = channelID;
81 this.typeClass = typeClass;
82 this.isProperty = isProperty;
86 public String toString() {
91 public String getMieleID() {
96 public String getChannelID() {
101 public boolean isProperty() {
106 public boolean isExtendedState() {
111 public State getState(String s, @Nullable DeviceMetaData dmd,
112 @Nullable MieleTranslationProvider translationProvider) {
113 return this.getState(s, dmd);
117 public State getState(String s, @Nullable DeviceMetaData dmd) {
119 String localizedValue = dmd.getMieleEnum(s);
120 if (localizedValue == null) {
121 localizedValue = dmd.LocalizedValue;
123 if (localizedValue == null) {
127 return getState(localizedValue);
133 public State getState(String s) {
135 Method valueOf = typeClass.getMethod("valueOf", String.class);
136 State state = (State) valueOf.invoke(typeClass, s);
140 } catch (Exception e) {
141 logger.error("An exception occurred while converting '{}' into a State", s);
144 return UnDefType.UNDEF;