]> git.basschouten.com Git - openhab-addons.git/blob
a970aeec629f8823695fdd74cb9db56e511f0009
[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.imperihome.internal.model.device;
14
15 import java.util.Map;
16 import java.util.stream.Collectors;
17
18 import org.openhab.core.items.Item;
19 import org.openhab.core.library.types.DecimalType;
20 import org.openhab.core.types.State;
21 import org.openhab.io.imperihome.internal.model.param.DeviceParam;
22 import org.openhab.io.imperihome.internal.model.param.ParamType;
23
24 /**
25  * MultiSwitch device, mimics behavior of an OH Switch with a mapping.
26  *
27  * @author Pepijn de Geus - Initial contribution
28  */
29 public class MultiSwitchDevice extends AbstractDevice {
30
31     private String itemValue = "";
32
33     public MultiSwitchDevice(Item item) {
34         super(DeviceType.MULTI_SWITCH, item);
35     }
36
37     @Override
38     public void updateParams() {
39         super.updateParams();
40
41         Map<String, String> mapping = getMapping();
42         if (mapping == null || mapping.isEmpty()) {
43             logger.error("MultiSwitch device {} contains no mapping", this);
44             return;
45         }
46
47         DeviceParam choicesParam = new DeviceParam(ParamType.CHOICES,
48                 mapping.values().stream().collect(Collectors.joining(",")));
49         addParam(choicesParam);
50
51         // Find current value text
52         String currentValue = "";
53         if (mapping.containsKey(itemValue)) {
54             currentValue = mapping.get(itemValue);
55         }
56
57         DeviceParam valueParam = new DeviceParam(ParamType.MULTISWITCH_VALUE, currentValue);
58         addParam(valueParam);
59     }
60
61     @Override
62     public void stateUpdated(Item item, State newState) {
63         super.stateUpdated(item, newState);
64
65         State state = item.getStateAs(DecimalType.class);
66         if (state instanceof DecimalType) {
67             itemValue = String.valueOf(((DecimalType) state).intValue());
68         }
69     }
70 }