]> git.basschouten.com Git - openhab-addons.git/blob
bdedbf2a3f01368a8ec917e5f22767c532e4bf02
[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     BRAND_ID("brandId", "brandId", StringType.class, true),
43     COMPANY_ID("companyId", "companyId", StringType.class, true),
44     STATE_TEXT(STATE_PROPERTY_NAME, STATE_TEXT_CHANNEL_ID, StringType.class, false),
45     STATE(null, STATE_CHANNEL_ID, DecimalType.class, false),
46     VENTILATION("ventilationPower", "ventilation", DecimalType.class, false),
47     LIGHT("lightingStatus", "light", OnOffType.class, false) {
48         @Override
49
50         public State getState(String s, DeviceMetaData dmd) {
51             if ("true".equals(s)) {
52                 return getState("ON");
53             }
54
55             if ("false".equals(s)) {
56                 return getState("OFF");
57             }
58
59             return UnDefType.UNDEF;
60         }
61     },
62     STOP(null, "stop", OnOffType.class, false);
63
64     private final Logger logger = LoggerFactory.getLogger(HoodChannelSelector.class);
65
66     private final String mieleID;
67     private final String channelID;
68     private final Class<? extends Type> typeClass;
69     private final boolean isProperty;
70
71     HoodChannelSelector(String propertyID, String channelID, Class<? extends Type> typeClass, boolean isProperty) {
72         this.mieleID = propertyID;
73         this.channelID = channelID;
74         this.typeClass = typeClass;
75         this.isProperty = isProperty;
76     }
77
78     @Override
79     public String toString() {
80         return mieleID;
81     }
82
83     @Override
84     public String getMieleID() {
85         return mieleID;
86     }
87
88     @Override
89     public String getChannelID() {
90         return channelID;
91     }
92
93     @Override
94     public boolean isProperty() {
95         return isProperty;
96     }
97
98     @Override
99     public boolean isExtendedState() {
100         return false;
101     }
102
103     @Override
104     public State getState(String s, DeviceMetaData dmd) {
105         if (dmd != null) {
106             String localizedValue = getMieleEnum(s, dmd);
107             if (localizedValue == null) {
108                 localizedValue = dmd.LocalizedValue;
109             }
110             if (localizedValue == null) {
111                 localizedValue = s;
112             }
113
114             return getState(localizedValue);
115         } else {
116             return getState(s);
117         }
118     }
119
120     public State getState(String s) {
121         try {
122             Method valueOf = typeClass.getMethod("valueOf", String.class);
123             State state = (State) valueOf.invoke(typeClass, s);
124             if (state != null) {
125                 return state;
126             }
127         } catch (Exception e) {
128             logger.error("An exception occurred while converting '{}' into a State", s);
129         }
130
131         return null;
132     }
133
134     public String getMieleEnum(String s, DeviceMetaData dmd) {
135         if (dmd.MieleEnum != null) {
136             for (Entry<String, JsonElement> enumEntry : dmd.MieleEnum.entrySet()) {
137                 if (enumEntry.getValue().getAsString().trim().equals(s.trim())) {
138                     return enumEntry.getKey();
139                 }
140             }
141         }
142
143         return null;
144     }
145 }