]> git.basschouten.com Git - openhab-addons.git/blob
93a09cb0c210e3185cf1f6db6077a131b6d95d4e
[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.tplinksmarthome.internal;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.function.Function;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.core.thing.ThingTypeUID;
25
26 /**
27  * ThingType enum with all supported TP-Link Smart Home devices.
28  *
29  * @author Hilbrand Bouwkamp - Initial contribution
30  *
31  */
32 @NonNullByDefault
33 public enum TPLinkSmartHomeThingType {
34
35     // Bulb Thing Type UIDs
36     KB100("kb100", DeviceType.BULB),
37     KB130("kb130", DeviceType.BULB),
38     LB100("lb100", DeviceType.BULB),
39     LB110("lb110", DeviceType.BULB),
40     LB120("lb120", DeviceType.BULB),
41     LB130("lb130", DeviceType.BULB),
42     LB200("lb200", DeviceType.BULB),
43     LB230("lb230", DeviceType.BULB),
44     KL50("kl50", DeviceType.BULB),
45     KL60("kl60", DeviceType.BULB),
46     KL110("kl110", DeviceType.BULB),
47     KL120("kl120", DeviceType.BULB),
48     KL130("kl130", DeviceType.BULB),
49
50     // Plug Thing Type UIDs
51     HS100("hs100", DeviceType.PLUG),
52     HS103("hs103", DeviceType.PLUG),
53     HS105("hs105", DeviceType.PLUG),
54     HS110("hs110", DeviceType.PLUG),
55     KP100("kp100", DeviceType.PLUG),
56     KP105("kp105", DeviceType.PLUG),
57     KP115("kp115", DeviceType.PLUG),
58
59     // Switch Thing Type UIDs
60     HS200("hs200", DeviceType.SWITCH),
61     HS210("hs210", DeviceType.SWITCH),
62
63     // Dimmer Thing Type UIDs
64     HS220("hs220", DeviceType.DIMMER),
65
66     // Power Strip Thing Type UIDs.
67     HS107("hs107", DeviceType.STRIP, 2),
68     HS300("hs300", DeviceType.STRIP, 6),
69     KP200("kp200", DeviceType.STRIP, 2),
70     KP303("kp303", DeviceType.STRIP, 3),
71     KP400("kp400", DeviceType.STRIP, 2),
72
73     // Range Extender Thing Type UIDs
74     RE270K("re270", DeviceType.RANGE_EXTENDER),
75     RE370K("re370", DeviceType.RANGE_EXTENDER);
76
77     /**
78      * All supported Smart Home devices in a list.
79      */
80     public static final List<TPLinkSmartHomeThingType> SUPPORTED_THING_TYPES_LIST = Arrays
81             .asList(TPLinkSmartHomeThingType.values());
82
83     /**
84      * All {@link ThingTypeUID}s of all supported Smart Home devices.
85      */
86     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = SUPPORTED_THING_TYPES_LIST.stream()
87             .map(TPLinkSmartHomeThingType::thingTypeUID).collect(Collectors.toSet());
88
89     /**
90      * A map of all {@link TPLinkSmartHomeThingType} mapped to {@link ThingTypeUID}.
91      */
92     public static final Map<ThingTypeUID, TPLinkSmartHomeThingType> THING_TYPE_MAP = SUPPORTED_THING_TYPES_LIST.stream()
93             .collect(Collectors.toMap(TPLinkSmartHomeThingType::thingTypeUID, Function.identity()));
94     private static final List<TPLinkSmartHomeThingType> BULB_WITH_TEMPERATURE_COLOR_1 = Stream.of(LB120, KL120)
95             .collect(Collectors.toList());
96     private static final List<TPLinkSmartHomeThingType> BULB_WITH_TEMPERATURE_COLOR_2 = Stream
97             .of(KB130, KL130, LB130, LB230).collect(Collectors.toList());
98
99     private final ThingTypeUID thingTypeUID;
100     private final DeviceType type;
101     private final int sockets;
102
103     TPLinkSmartHomeThingType(final String name, final DeviceType type) {
104         this(name, type, 0);
105     }
106
107     TPLinkSmartHomeThingType(final String name, final DeviceType type, int sockets) {
108         thingTypeUID = new ThingTypeUID(TPLinkSmartHomeBindingConstants.BINDING_ID, name);
109         this.type = type;
110         this.sockets = sockets;
111     }
112
113     /**
114      * @return Returns the type of the device.
115      */
116     public DeviceType getDeviceType() {
117         return type;
118     }
119
120     /**
121      * @return The {@link ThingTypeUID} of this device.
122      */
123     public ThingTypeUID thingTypeUID() {
124         return thingTypeUID;
125     }
126
127     /**
128      * @return Returns the number of sockets. Only for Strip devices.
129      */
130     public int getSockets() {
131         return sockets;
132     }
133
134     /**
135      * Returns true if the given {@link ThingTypeUID} matches a device that is a bulb with color temperature ranges 1
136      * (2700 to 6500k).
137      *
138      * @param thingTypeUID if the check
139      * @return true if it's a bulb device with color temperature range 1
140      */
141     public static boolean isBulbDeviceWithTemperatureColor1(ThingTypeUID thingTypeUID) {
142         return isDevice(thingTypeUID, BULB_WITH_TEMPERATURE_COLOR_1);
143     }
144
145     /**
146      * Returns true if the given {@link ThingTypeUID} matches a device that is a bulb with color temperature ranges 2
147      * (2500 to 9000k).
148      *
149      * @param thingTypeUID if the check
150      * @return true if it's a bulb device with color temperature range 2
151      */
152     public static boolean isBulbDeviceWithTemperatureColor2(ThingTypeUID thingTypeUID) {
153         return isDevice(thingTypeUID, BULB_WITH_TEMPERATURE_COLOR_2);
154     }
155
156     private static boolean isDevice(ThingTypeUID thingTypeUID, List<TPLinkSmartHomeThingType> thingTypes) {
157         return thingTypes.stream().anyMatch(t -> t.is(thingTypeUID));
158     }
159
160     /**
161      * Returns true if the given {@link ThingTypeUID} matches the {@link ThingTypeUID} in this enum.
162      *
163      * @param otherThingTypeUID to check
164      * @return true if matches
165      */
166     public boolean is(ThingTypeUID otherThingTypeUID) {
167         return thingTypeUID.equals(otherThingTypeUID);
168     }
169
170     /**
171      * Enum indicating the type of the device.
172      */
173     public enum DeviceType {
174         /**
175          * Light Bulb device.
176          */
177         BULB,
178         /**
179          * Dimmer device.
180          */
181         DIMMER,
182         /**
183          * Plug device.
184          */
185         PLUG,
186         /**
187          * Wi-Fi range extender device with plug.
188          */
189         RANGE_EXTENDER,
190         /**
191          * Power strip device.
192          */
193         STRIP,
194         /**
195          * Switch device.
196          */
197         SWITCH
198     }
199 }