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