]> git.basschouten.com Git - openhab-addons.git/blob
f84eebc91941db4f227c280b1943d5c7067fe505
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.miele.internal.handler;
14
15 import static org.openhab.binding.miele.internal.MieleBindingConstants.*;
16
17 import java.lang.reflect.Method;
18
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;
32
33 /**
34  * The {@link ApplianceChannelSelector} for ventilation hoods
35  *
36  * @author Karel Goderis - Initial contribution
37  * @author Jacob Laursen - Added raw channels
38  */
39 @NonNullByDefault
40 public enum HoodChannelSelector implements ApplianceChannelSelector {
41
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) {
45         @Override
46         public State getState(String s, @Nullable DeviceMetaData dmd,
47                 @Nullable MieleTranslationProvider translationProvider) {
48             return DeviceUtil.getStateTextState(s, dmd, translationProvider);
49         }
50     },
51     STATE("", STATE_CHANNEL_ID, DecimalType.class, false),
52     VENTILATION("ventilationPower", "ventilation", DecimalType.class, false),
53     LIGHT("lightingStatus", "light", OnOffType.class, false) {
54         @Override
55
56         public State getState(String s, @Nullable DeviceMetaData dmd,
57                 @Nullable MieleTranslationProvider translationProvider) {
58             if ("true".equals(s)) {
59                 return getState("ON");
60             }
61
62             if ("false".equals(s)) {
63                 return getState("OFF");
64             }
65
66             return UnDefType.UNDEF;
67         }
68     },
69     STOP("", "stop", OnOffType.class, false);
70
71     private final Logger logger = LoggerFactory.getLogger(HoodChannelSelector.class);
72
73     private final String mieleID;
74     private final String channelID;
75     private final Class<? extends Type> typeClass;
76     private final boolean isProperty;
77
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;
83     }
84
85     @Override
86     public String toString() {
87         return mieleID;
88     }
89
90     @Override
91     public String getMieleID() {
92         return mieleID;
93     }
94
95     @Override
96     public String getChannelID() {
97         return channelID;
98     }
99
100     @Override
101     public boolean isProperty() {
102         return isProperty;
103     }
104
105     @Override
106     public boolean isExtendedState() {
107         return false;
108     }
109
110     @Override
111     public State getState(String s, @Nullable DeviceMetaData dmd,
112             @Nullable MieleTranslationProvider translationProvider) {
113         return this.getState(s, dmd);
114     }
115
116     @Override
117     public State getState(String s, @Nullable DeviceMetaData dmd) {
118         if (dmd != null) {
119             String localizedValue = dmd.getMieleEnum(s);
120             if (localizedValue == null) {
121                 localizedValue = dmd.LocalizedValue;
122             }
123             if (localizedValue == null) {
124                 localizedValue = s;
125             }
126
127             return getState(localizedValue);
128         } else {
129             return getState(s);
130         }
131     }
132
133     public State getState(String s) {
134         try {
135             Method valueOf = typeClass.getMethod("valueOf", String.class);
136             State state = (State) valueOf.invoke(typeClass, s);
137             if (state != null) {
138                 return state;
139             }
140         } catch (Exception e) {
141             logger.error("An exception occurred while converting '{}' into a State", s);
142         }
143
144         return UnDefType.UNDEF;
145     }
146 }