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