]> git.basschouten.com Git - openhab-addons.git/blob
de172805550b1266fd47548e1b561383c545c68b
[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.binding.mielecloud.internal.webservice.api.json;
14
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.List;
18 import java.util.stream.Collectors;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22
23 /**
24  * Represents the state of a light on a Miele device.
25  *
26  * @author Roland Edelhoff - Initial contribution
27  * @author Björn Lange - Added NOT_SUPPORTED entry
28  */
29 @NonNullByDefault
30 public enum Light {
31     /**
32      * {Light} for unknown states.
33      */
34     UNKNOWN(),
35
36     ENABLE(1),
37
38     DISABLE(2),
39
40     NOT_SUPPORTED(0, 255);
41
42     private List<Integer> ids;
43
44     Light(int... ids) {
45         this.ids = Collections.unmodifiableList(Arrays.stream(ids).boxed().collect(Collectors.toList()));
46     }
47
48     /**
49      * Gets the {@link Light} state matching the given ID.
50      * 
51      * @param id The ID.
52      * @return The matching {@link Light} or {@code UNKNOWN} if no ID matches.
53      */
54     public static Light fromId(@Nullable Integer id) {
55         for (Light light : Light.values()) {
56             if (light.ids.contains(id)) {
57                 return light;
58             }
59         }
60
61         return Light.UNKNOWN;
62     }
63
64     /**
65      * Formats this instance for interaction with the Miele webservice.
66      */
67     public String format() {
68         if (ids.isEmpty()) {
69             return "";
70         } else {
71             return Integer.toString(ids.get(0));
72         }
73     }
74 }