]> git.basschouten.com Git - openhab-addons.git/blob
970bb99dac7c7b054c5d680862ba797919f8773b
[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.homematic.internal.communicator.virtual;
14
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.*;
16
17 import org.openhab.binding.homematic.internal.model.HmChannel;
18 import org.openhab.binding.homematic.internal.model.HmDatapoint;
19 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
20 import org.openhab.binding.homematic.internal.model.HmDevice;
21 import org.openhab.binding.homematic.internal.model.HmValueType;
22
23 /**
24  * A virtual datapoint that converts the ENUM state of the HMIP-SWD_ devices to a contact.
25  *
26  * @author Gerhard Riegler - Initial contribution
27  */
28 public class StateContactVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
29     @Override
30     public String getName() {
31         return VIRTUAL_DATAPOINT_NAME_STATE_CONTACT;
32     }
33
34     @Override
35     public void initialize(HmDevice device) {
36         if (isApplicable(device)) {
37             HmChannel channelOne = device.getChannel(1);
38             if (channelOne != null) {
39                 HmDatapointInfo dpStateInfo = HmDatapointInfo.createValuesInfo(channelOne, DATAPOINT_NAME_STATE);
40                 HmDatapoint dpState = channelOne.getDatapoint(dpStateInfo);
41                 if (dpState != null) {
42                     addDatapoint(device, 1, getName(), HmValueType.BOOL, convertState(dpState.getValue()), true);
43                 }
44             }
45         }
46     }
47
48     @Override
49     public boolean canHandleEvent(HmDatapoint dp) {
50         return isApplicable(dp.getChannel().getDevice()) && DATAPOINT_NAME_STATE.equals(dp.getName());
51     }
52
53     @Override
54     public void handleEvent(VirtualGateway gateway, HmDatapoint dp) {
55         Object value = convertState(dp.getValue());
56         HmDatapoint vdp = getVirtualDatapoint(dp.getChannel());
57         vdp.setValue(value);
58     }
59
60     private boolean isApplicable(HmDevice device) {
61         return device.getType().toUpperCase().startsWith("HMIP-SWD");
62     }
63
64     private Boolean convertState(Object value) {
65         if (!(value instanceof Integer)) {
66             return null;
67         }
68         if ((int) value == 0) {
69             return true;
70         } else if ((int) value == 1) {
71             return false;
72         } else {
73             return null;
74         }
75     }
76 }