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