]> git.basschouten.com Git - openhab-addons.git/blob
623c9830d335cb799c746fbd9c7d8d3716f53063
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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 import java.util.Map.Entry;
19
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.OnOffType;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.types.State;
25 import org.openhab.core.types.Type;
26 import org.openhab.core.types.UnDefType;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.google.gson.JsonElement;
31
32 /**
33  * The {@link ApplianceChannelSelector} for ventilation hoods
34  *
35  * @author Karel Goderis - Initial contribution
36  * @author Jacob Laursen - Added raw channels
37  */
38 public enum HoodChannelSelector implements ApplianceChannelSelector {
39
40     PRODUCT_TYPE("productTypeId", "productType", StringType.class, true),
41     DEVICE_TYPE("mieleDeviceType", "deviceType", 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     VENTILATION("ventilationPower", "ventilation", DecimalType.class, false),
45     LIGHT("lightingStatus", "light", OnOffType.class, false) {
46         @Override
47
48         public State getState(String s, DeviceMetaData dmd) {
49             if ("true".equals(s)) {
50                 return getState("ON");
51             }
52
53             if ("false".equals(s)) {
54                 return getState("OFF");
55             }
56
57             return UnDefType.UNDEF;
58         }
59     },
60     STOP(null, "stop", OnOffType.class, false);
61
62     private final Logger logger = LoggerFactory.getLogger(HoodChannelSelector.class);
63
64     private final String mieleID;
65     private final String channelID;
66     private final Class<? extends Type> typeClass;
67     private final boolean isProperty;
68
69     HoodChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
70         this.mieleID = propertyID;
71         this.channelID = channelID;
72         this.typeClass = typeClass;
73         this.isProperty = isProperty;
74     }
75
76     @Override
77     public String toString() {
78         return mieleID;
79     }
80
81     @Override
82     public String getMieleID() {
83         return mieleID;
84     }
85
86     @Override
87     public String getChannelID() {
88         return channelID;
89     }
90
91     @Override
92     public boolean isProperty() {
93         return isProperty;
94     }
95
96     @Override
97     public boolean isExtendedState() {
98         return false;
99     }
100
101     @Override
102     public State getState(String s, DeviceMetaData dmd) {
103         if (dmd != null) {
104             String localizedValue = getMieleEnum(s, dmd);
105             if (localizedValue == null) {
106                 localizedValue = dmd.LocalizedValue;
107             }
108             if (localizedValue == null) {
109                 localizedValue = s;
110             }
111
112             return getState(localizedValue);
113         } else {
114             return getState(s);
115         }
116     }
117
118     public State getState(String s) {
119         try {
120             Method valueOf = typeClass.getMethod("valueOf", String.class);
121             State state = (State) valueOf.invoke(typeClass, s);
122             if (state != null) {
123                 return state;
124             }
125         } catch (Exception e) {
126             logger.error("An exception occurred while converting '{}' into a State", s);
127         }
128
129         return null;
130     }
131
132     public String getMieleEnum(String s, DeviceMetaData dmd) {
133         if (dmd.MieleEnum != null) {
134             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
135                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
136                     return enumEntry.getKey();
137                 }
138             }
139         }
140
141         return null;
142     }
143 }