]> git.basschouten.com Git - openhab-addons.git/blob
93a68e7fb4ec8033acf34d0add40016b8ba81dc4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.insteon.internal.device.feature;
14
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.function.Function;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.insteon.internal.utils.BinaryUtils;
23
24 /**
25  * The {@link FeatureEnums} represents feature enums
26  *
27  * @author Jeremy Setton - Initial contribution
28  */
29 @NonNullByDefault
30 public class FeatureEnums {
31     public static enum ButtonEvent {
32         PRESSED_ON,
33         PRESSED_OFF,
34         DOUBLE_PRESSED_ON,
35         DOUBLE_PRESSED_OFF,
36         HELD_UP,
37         HELD_DOWN,
38         RELEASED;
39
40         public static ButtonEvent valueOf(int cmd1, int cmd2) throws IllegalArgumentException {
41             switch (cmd1) {
42                 case 0x11:
43                     return ButtonEvent.PRESSED_ON;
44                 case 0x12:
45                     return ButtonEvent.DOUBLE_PRESSED_ON;
46                 case 0x13:
47                     return ButtonEvent.PRESSED_OFF;
48                 case 0x14:
49                     return ButtonEvent.DOUBLE_PRESSED_OFF;
50                 case 0x17:
51                     return cmd2 == 0x01 ? ButtonEvent.HELD_UP : ButtonEvent.HELD_DOWN;
52                 case 0x18:
53                     return ButtonEvent.RELEASED;
54                 default:
55                     throw new IllegalArgumentException("unexpected button event");
56             }
57         }
58     }
59
60     public static enum IMButtonEvent {
61         PRESSED,
62         HELD,
63         RELEASED;
64
65         public static IMButtonEvent valueOf(int cmd) throws IllegalArgumentException {
66             switch (cmd) {
67                 case 0x02:
68                     return IMButtonEvent.PRESSED;
69                 case 0x03:
70                     return IMButtonEvent.HELD;
71                 case 0x04:
72                     return IMButtonEvent.RELEASED;
73                 default:
74                     throw new IllegalArgumentException("unexpected im button event");
75             }
76         }
77     }
78
79     public static enum FanLincFanSpeed {
80         OFF(0x00),
81         LOW(0x55),
82         MEDIUM(0xAA),
83         HIGH(0xFF);
84
85         private int value;
86
87         private FanLincFanSpeed(int value) {
88             this.value = value;
89         }
90
91         public int getValue() {
92             return value;
93         }
94
95         public static FanLincFanSpeed valueOf(int value) throws IllegalArgumentException {
96             if (value == 0x00) {
97                 return FanLincFanSpeed.OFF;
98             } else if (value >= 0x01 && value <= 0x7F) {
99                 return FanLincFanSpeed.LOW;
100             } else if (value >= 0x80 && value <= 0xFE) {
101                 return FanLincFanSpeed.MEDIUM;
102             } else if (value == 0xFF) {
103                 return FanLincFanSpeed.HIGH;
104             } else {
105                 throw new IllegalArgumentException("unexpected fanlinc fan speed");
106             }
107         }
108
109         public static List<String> names() {
110             return Arrays.stream(values()).map(String::valueOf).toList();
111         }
112     }
113
114     public static enum KeypadButtonConfig {
115         BUTTON_6(0x07, 6),
116         BUTTON_8(0x06, 8);
117
118         private int value;
119         private int count;
120
121         private KeypadButtonConfig(int value, int count) {
122             this.value = value;
123             this.count = count;
124         }
125
126         public int getValue() {
127             return value;
128         }
129
130         public int getCount() {
131             return count;
132         }
133
134         public static KeypadButtonConfig from(boolean is8Button) {
135             return is8Button ? KeypadButtonConfig.BUTTON_8 : KeypadButtonConfig.BUTTON_6;
136         }
137     }
138
139     public static enum KeypadButtonToggleMode {
140         TOGGLE,
141         ALWAYS_ON,
142         ALWAYS_OFF;
143
144         public static KeypadButtonToggleMode valueOf(int value, int bit) {
145             if (!BinaryUtils.isBitSet(value >> 8, bit)) {
146                 return KeypadButtonToggleMode.TOGGLE;
147             } else if (BinaryUtils.isBitSet(value & 0xFF, bit)) {
148                 return KeypadButtonToggleMode.ALWAYS_ON;
149             } else {
150                 return KeypadButtonToggleMode.ALWAYS_OFF;
151             }
152         }
153     }
154
155     public static enum IOLincRelayMode {
156         LATCHING,
157         MOMENTARY_A,
158         MOMENTARY_B,
159         MOMENTARY_C;
160
161         public static IOLincRelayMode valueOf(int value) {
162             if (!BinaryUtils.isBitSet(value, 3)) {
163                 // return latching, when momentary mode op flag (3) is off
164                 return IOLincRelayMode.LATCHING;
165             } else if (BinaryUtils.isBitSet(value, 7)) {
166                 // return momentary c, when momentary sensor follow op flag (7) is on
167                 return IOLincRelayMode.MOMENTARY_C;
168             } else if (BinaryUtils.isBitSet(value, 4)) {
169                 // return momentary b, when momentary trigger on/off op flag (4) is on
170                 return IOLincRelayMode.MOMENTARY_B;
171             } else {
172                 // return momentary a, otherwise
173                 return IOLincRelayMode.MOMENTARY_A;
174             }
175         }
176     }
177
178     public static enum MicroModuleOpMode {
179         LATCHING,
180         SINGLE_MOMENTARY,
181         DUAL_MOMENTARY;
182
183         public static MicroModuleOpMode valueOf(int value) {
184             if (!BinaryUtils.isBitSet(value, 1)) {
185                 // return latching, when momentary line op flag (1) is off
186                 return MicroModuleOpMode.LATCHING;
187             } else if (!BinaryUtils.isBitSet(value, 0)) {
188                 // return single momentary, when dual line op flag (0) is off
189                 return MicroModuleOpMode.SINGLE_MOMENTARY;
190             } else {
191                 // return dual momentary, otherwise
192                 return MicroModuleOpMode.DUAL_MOMENTARY;
193             }
194         }
195     }
196
197     public static enum SirenAlertType {
198         CHIME(0x00),
199         LOUD_SIREN(0x01);
200
201         private static final Map<Integer, SirenAlertType> VALUE_MAP = Arrays.stream(values())
202                 .collect(Collectors.toUnmodifiableMap(type -> type.value, Function.identity()));
203
204         private int value;
205
206         private SirenAlertType(int value) {
207             this.value = value;
208         }
209
210         public int getValue() {
211             return value;
212         }
213
214         public static SirenAlertType valueOf(int value) throws IllegalArgumentException {
215             SirenAlertType type = VALUE_MAP.get(value);
216             if (type == null) {
217                 throw new IllegalArgumentException("unexpected siren alert type");
218             }
219             return type;
220         }
221     }
222
223     public static enum ThermostatFanMode {
224         AUTO(0x08, 0x00),
225         ALWAYS_ON(0x07, 0x01);
226
227         private static final Map<Integer, ThermostatFanMode> VALUE_MAP = Arrays.stream(values())
228                 .collect(Collectors.toUnmodifiableMap(mode -> mode.value, Function.identity()));
229         private static final Map<Integer, ThermostatFanMode> STATUS_MAP = Arrays.stream(values())
230                 .collect(Collectors.toUnmodifiableMap(mode -> mode.status, Function.identity()));
231
232         private int value;
233         private int status;
234
235         private ThermostatFanMode(int value, int status) {
236             this.value = value;
237             this.status = status;
238         }
239
240         public int getValue() {
241             return value;
242         }
243
244         public static ThermostatFanMode valueOf(int value) throws IllegalArgumentException {
245             ThermostatFanMode mode = VALUE_MAP.get(value);
246             if (mode == null) {
247                 throw new IllegalArgumentException("unexpected thermostat fan mode");
248             }
249             return mode;
250         }
251
252         public static ThermostatFanMode fromStatus(int status) throws IllegalArgumentException {
253             ThermostatFanMode mode = STATUS_MAP.get(status);
254             if (mode == null) {
255                 throw new IllegalArgumentException("unexpected thermostat fan status");
256             }
257             return mode;
258         }
259
260         public static List<String> names() {
261             return VALUE_MAP.values().stream().map(String::valueOf).toList();
262         }
263     }
264
265     public static enum ThermostatSystemMode {
266         OFF(0x09, 0x00),
267         AUTO(0x06, 0x01),
268         HEAT(0x04, 0x02),
269         COOL(0x05, 0x03),
270         PROGRAM(0x0A, 0x04);
271
272         private static final Map<Integer, ThermostatSystemMode> VALUE_MAP = Arrays.stream(values())
273                 .collect(Collectors.toUnmodifiableMap(mode -> mode.value, Function.identity()));
274         private static final Map<Integer, ThermostatSystemMode> STATUS_MAP = Arrays.stream(values())
275                 .collect(Collectors.toUnmodifiableMap(mode -> mode.status, Function.identity()));
276
277         private int value;
278         private int status;
279
280         private ThermostatSystemMode(int value, int status) {
281             this.value = value;
282             this.status = status;
283         }
284
285         public int getValue() {
286             return value;
287         }
288
289         public static ThermostatSystemMode valueOf(int value) throws IllegalArgumentException {
290             ThermostatSystemMode mode = VALUE_MAP.get(value);
291             if (mode == null) {
292                 throw new IllegalArgumentException("unexpected thermostat system mode");
293             }
294             return mode;
295         }
296
297         public static ThermostatSystemMode fromStatus(int status) throws IllegalArgumentException {
298             ThermostatSystemMode mode = STATUS_MAP.get(status);
299             if (mode == null) {
300                 throw new IllegalArgumentException("unexpected thermostat system status");
301             }
302             return mode;
303         }
304
305         public static List<String> names() {
306             return VALUE_MAP.values().stream().map(String::valueOf).toList();
307         }
308     }
309
310     public static enum VenstarSystemMode {
311         OFF(0x09, 0x00),
312         AUTO(0x06, 0x01),
313         HEAT(0x04, 0x02),
314         COOL(0x05, 0x03),
315         PROGRAM_HEAT(0x0A, 0x04),
316         PROGRAM_COOL(0x0B, 0x05),
317         PROGRAM_AUTO(0x0C, 0x06);
318
319         private static final Map<Integer, VenstarSystemMode> VALUE_MAP = Arrays.stream(values())
320                 .collect(Collectors.toUnmodifiableMap(mode -> mode.value, Function.identity()));
321         private static final Map<Integer, VenstarSystemMode> STATUS_MAP = Arrays.stream(values())
322                 .collect(Collectors.toUnmodifiableMap(mode -> mode.status, Function.identity()));
323
324         private int value;
325         private int status;
326
327         private VenstarSystemMode(int value, int status) {
328             this.value = value;
329             this.status = status;
330         }
331
332         public int getValue() {
333             return value;
334         }
335
336         public static VenstarSystemMode valueOf(int value) throws IllegalArgumentException {
337             VenstarSystemMode mode = VALUE_MAP.get(value);
338             if (mode == null) {
339                 throw new IllegalArgumentException("unexpected venstar system mode");
340             }
341             return mode;
342         }
343
344         public static VenstarSystemMode fromStatus(int status) throws IllegalArgumentException {
345             VenstarSystemMode mode = STATUS_MAP.get(status);
346             if (mode == null) {
347                 throw new IllegalArgumentException("unexpected venstar system status");
348             }
349             return mode;
350         }
351
352         public static List<String> names() {
353             return VALUE_MAP.values().stream().map(String::valueOf).toList();
354         }
355     }
356
357     public static enum ThermostatSystemState {
358         OFF,
359         HEATING,
360         COOLING,
361         HUMIDIFYING,
362         DEHUMIDIFYING;
363     }
364
365     public static enum ThermostatTemperatureScale {
366         CELSIUS,
367         FAHRENHEIT;
368
369         public static ThermostatTemperatureScale from(boolean isCelsius) {
370             return isCelsius ? ThermostatTemperatureScale.CELSIUS : ThermostatTemperatureScale.FAHRENHEIT;
371         }
372     }
373
374     public static enum ThermostatTimeFormat {
375         HR_12("12H"),
376         HR_24("24H");
377
378         private static final Map<String, ThermostatTimeFormat> LABEL_MAP = Arrays.stream(values())
379                 .collect(Collectors.toUnmodifiableMap(format -> format.label, Function.identity()));
380
381         private String label;
382
383         private ThermostatTimeFormat(String label) {
384             this.label = label;
385         }
386
387         @Override
388         public String toString() {
389             return label;
390         }
391
392         public static ThermostatTimeFormat from(boolean is24Hr) {
393             return is24Hr ? ThermostatTimeFormat.HR_24 : ThermostatTimeFormat.HR_12;
394         }
395
396         public static ThermostatTimeFormat from(String label) throws IllegalArgumentException {
397             ThermostatTimeFormat format = LABEL_MAP.get(label);
398             if (format == null) {
399                 throw new IllegalArgumentException("unexpected thermostat time format");
400             }
401             return format;
402         }
403     }
404 }