2 * Copyright (c) 2010-2024 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.insteon.internal.device.feature;
15 import java.util.Arrays;
16 import java.util.List;
18 import java.util.function.Function;
19 import java.util.stream.Collectors;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.insteon.internal.utils.BinaryUtils;
25 * The {@link FeatureEnums} represents feature enums
27 * @author Jeremy Setton - Initial contribution
30 public class FeatureEnums {
31 public static enum ButtonEvent {
40 public static ButtonEvent valueOf(int cmd1, int cmd2) throws IllegalArgumentException {
43 return ButtonEvent.PRESSED_ON;
45 return ButtonEvent.DOUBLE_PRESSED_ON;
47 return ButtonEvent.PRESSED_OFF;
49 return ButtonEvent.DOUBLE_PRESSED_OFF;
51 return cmd2 == 0x01 ? ButtonEvent.HELD_UP : ButtonEvent.HELD_DOWN;
53 return ButtonEvent.RELEASED;
55 throw new IllegalArgumentException("unexpected button event");
60 public static enum IMButtonEvent {
65 public static IMButtonEvent valueOf(int cmd) throws IllegalArgumentException {
68 return IMButtonEvent.PRESSED;
70 return IMButtonEvent.HELD;
72 return IMButtonEvent.RELEASED;
74 throw new IllegalArgumentException("unexpected im button event");
79 public static enum FanLincFanSpeed {
87 private FanLincFanSpeed(int value) {
91 public int getValue() {
95 public static FanLincFanSpeed valueOf(int value) throws IllegalArgumentException {
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;
105 throw new IllegalArgumentException("unexpected fanlinc fan speed");
109 public static List<String> names() {
110 return Arrays.stream(values()).map(String::valueOf).toList();
114 public static enum KeypadButtonConfig {
121 private KeypadButtonConfig(int value, int count) {
126 public int getValue() {
130 public int getCount() {
134 public static KeypadButtonConfig from(boolean is8Button) {
135 return is8Button ? KeypadButtonConfig.BUTTON_8 : KeypadButtonConfig.BUTTON_6;
139 public static enum KeypadButtonToggleMode {
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;
150 return KeypadButtonToggleMode.ALWAYS_OFF;
155 public static enum IOLincRelayMode {
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;
172 // return momentary a, otherwise
173 return IOLincRelayMode.MOMENTARY_A;
178 public static enum MicroModuleOpMode {
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;
191 // return dual momentary, otherwise
192 return MicroModuleOpMode.DUAL_MOMENTARY;
197 public static enum SirenAlertType {
201 private static final Map<Integer, SirenAlertType> VALUE_MAP = Arrays.stream(values())
202 .collect(Collectors.toUnmodifiableMap(type -> type.value, Function.identity()));
206 private SirenAlertType(int value) {
210 public int getValue() {
214 public static SirenAlertType valueOf(int value) throws IllegalArgumentException {
215 SirenAlertType type = VALUE_MAP.get(value);
217 throw new IllegalArgumentException("unexpected siren alert type");
223 public static enum ThermostatFanMode {
225 ALWAYS_ON(0x07, 0x01);
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()));
235 private ThermostatFanMode(int value, int status) {
237 this.status = status;
240 public int getValue() {
244 public static ThermostatFanMode valueOf(int value) throws IllegalArgumentException {
245 ThermostatFanMode mode = VALUE_MAP.get(value);
247 throw new IllegalArgumentException("unexpected thermostat fan mode");
252 public static ThermostatFanMode fromStatus(int status) throws IllegalArgumentException {
253 ThermostatFanMode mode = STATUS_MAP.get(status);
255 throw new IllegalArgumentException("unexpected thermostat fan status");
260 public static List<String> names() {
261 return VALUE_MAP.values().stream().map(String::valueOf).toList();
265 public static enum ThermostatSystemMode {
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()));
280 private ThermostatSystemMode(int value, int status) {
282 this.status = status;
285 public int getValue() {
289 public static ThermostatSystemMode valueOf(int value) throws IllegalArgumentException {
290 ThermostatSystemMode mode = VALUE_MAP.get(value);
292 throw new IllegalArgumentException("unexpected thermostat system mode");
297 public static ThermostatSystemMode fromStatus(int status) throws IllegalArgumentException {
298 ThermostatSystemMode mode = STATUS_MAP.get(status);
300 throw new IllegalArgumentException("unexpected thermostat system status");
305 public static List<String> names() {
306 return VALUE_MAP.values().stream().map(String::valueOf).toList();
310 public static enum VenstarSystemMode {
315 PROGRAM_HEAT(0x0A, 0x04),
316 PROGRAM_COOL(0x0B, 0x05),
317 PROGRAM_AUTO(0x0C, 0x06);
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()));
327 private VenstarSystemMode(int value, int status) {
329 this.status = status;
332 public int getValue() {
336 public static VenstarSystemMode valueOf(int value) throws IllegalArgumentException {
337 VenstarSystemMode mode = VALUE_MAP.get(value);
339 throw new IllegalArgumentException("unexpected venstar system mode");
344 public static VenstarSystemMode fromStatus(int status) throws IllegalArgumentException {
345 VenstarSystemMode mode = STATUS_MAP.get(status);
347 throw new IllegalArgumentException("unexpected venstar system status");
352 public static List<String> names() {
353 return VALUE_MAP.values().stream().map(String::valueOf).toList();
357 public static enum ThermostatSystemState {
365 public static enum ThermostatTemperatureScale {
369 public static ThermostatTemperatureScale from(boolean isCelsius) {
370 return isCelsius ? ThermostatTemperatureScale.CELSIUS : ThermostatTemperatureScale.FAHRENHEIT;
374 public static enum ThermostatTimeFormat {
378 private static final Map<String, ThermostatTimeFormat> LABEL_MAP = Arrays.stream(values())
379 .collect(Collectors.toUnmodifiableMap(format -> format.label, Function.identity()));
381 private String label;
383 private ThermostatTimeFormat(String label) {
388 public String toString() {
392 public static ThermostatTimeFormat from(boolean is24Hr) {
393 return is24Hr ? ThermostatTimeFormat.HR_24 : ThermostatTimeFormat.HR_12;
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");