]> git.basschouten.com Git - openhab-addons.git/blob
4a7ef256ea137888bb52ff02a0e52a2331c0ee57
[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.io.neeo.internal.models;
14
15 import java.util.Objects;
16
17 import org.apache.commons.lang.StringUtils;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.core.thing.type.ChannelType;
21
22 /**
23  * Defines the various NEEO capability types
24  *
25  * @author Tim Roberts - Initial Contribution
26  */
27 @NonNullByDefault
28 public enum NeeoCapabilityType {
29     /** Represents the NEEO BUTTON capability */
30     BUTTON("button"),
31     /** Represents the NEEO SWITCH capability */
32     SWITCH("switch"),
33     /** Represents the NEEO SLIDER capability */
34     SLIDER("slider"),
35     /** Represents the NEEO SENSOR capability */
36     SENSOR("sensor"),
37     /** Represents the NEEO TEXT LABEL capability */
38     TEXTLABEL("textlabel"),
39     /** Represents the NEEO IMAGE URL capability */
40     IMAGEURL("imageurl"),
41     /** Represents the NEEO directory capability */
42     DIRECTORY("directory"),
43
44     /** Represents the NEEO CUSTOM SENSOR capability */
45     SENSOR_CUSTOM("custom"),
46     /** Represents the NEEO RANGE SENSOR capability */
47     SENSOR_RANGE("range"),
48     /** Represents the NEEO BINARY SENSOR capability */
49     SENSOR_BINARY("binary"),
50     /** Represents the NEEO POWER SENSOR capability */
51     SENSOR_POWER("power"),
52     /** Represents no capability (and should be excluded) */
53     EXCLUDE("");
54
55     /** The text value of the enum */
56     private final String text;
57
58     /**
59      * Constructs the NeeoCapabilityType using the specified text
60      *
61      * @param text the text
62      */
63     private NeeoCapabilityType(final String text) {
64         Objects.requireNonNull(text, "text is required");
65         this.text = text;
66     }
67
68     /**
69      * Parses the text into a NeeoCapabilityType enum (ignoring case)
70      *
71      * @param text the text to parse
72      * @return the NeeoCapabilityType type
73      */
74     public static NeeoCapabilityType parse(final String text) {
75         if (StringUtils.isEmpty(text)) {
76             return EXCLUDE;
77         }
78         for (NeeoCapabilityType enm : NeeoCapabilityType.values()) {
79             if (StringUtils.equalsIgnoreCase(text, enm.text)) {
80                 return enm;
81             }
82         }
83
84         return EXCLUDE;
85     }
86
87     /**
88      * Guess the {@link NeeoCapabilityType} for the given {@link ChannelType}
89      *
90      * @param channelType the possibly null channel type
91      * @return the best guess {@link NeeoCapabilityType}
92      */
93     public static NeeoCapabilityType guessType(@Nullable ChannelType channelType) {
94         if (channelType == null || StringUtils.isEmpty(channelType.getItemType())) {
95             return NeeoCapabilityType.EXCLUDE;
96         }
97
98         switch (channelType.getItemType().toLowerCase()) {
99             case "switch":
100             case "contact":
101             case "rollershutter":
102                 return NeeoCapabilityType.SWITCH;
103             case "datetime":
104             case "number":
105             case "point":
106             case "string":
107                 return NeeoCapabilityType.TEXTLABEL;
108             case "dimmer":
109             case "color":
110                 return NeeoCapabilityType.SLIDER;
111             default:
112                 return NeeoCapabilityType.EXCLUDE;
113         }
114     }
115
116     @Override
117     public String toString() {
118         return text;
119     }
120 }