]> git.basschouten.com Git - openhab-addons.git/blob
c4191e786d5e6adf475345cee0d377d0eee3dba2
[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 java.io.IOException;
18
19 import org.openhab.binding.homematic.internal.misc.HomematicClientException;
20 import org.openhab.binding.homematic.internal.misc.MiscUtils;
21 import org.openhab.binding.homematic.internal.model.HmChannel;
22 import org.openhab.binding.homematic.internal.model.HmDatapoint;
23 import org.openhab.binding.homematic.internal.model.HmDatapointConfig;
24 import org.openhab.binding.homematic.internal.model.HmDatapointInfo;
25 import org.openhab.binding.homematic.internal.model.HmDevice;
26 import org.openhab.binding.homematic.internal.model.HmInterface;
27 import org.openhab.binding.homematic.internal.model.HmValueType;
28
29 /**
30  * A virtual Switch datapoint to start and stop the install mode.
31  *
32  * @author Gerhard Riegler - Initial contribution
33  */
34 public class InstallModeVirtualDatapoint extends AbstractVirtualDatapointHandler {
35     @Override
36     public String getName() {
37         return VIRTUAL_DATAPOINT_NAME_INSTALL_MODE;
38     }
39
40     @Override
41     public void initialize(HmDevice device) {
42         if (device.isGatewayExtras()) {
43             addDatapoint(device, 0, getName(), HmValueType.BOOL, Boolean.FALSE, false);
44         }
45     }
46
47     @Override
48     public boolean canHandleCommand(HmDatapoint dp, Object value) {
49         return getName().equals(dp.getName());
50     }
51
52     @Override
53     public void handleCommand(VirtualGateway gateway, HmDatapoint dp, HmDatapointConfig dpConfig, Object value)
54             throws IOException, HomematicClientException {
55         dp.setValue(value);
56         boolean enable = MiscUtils.isTrueValue(value);
57         int duration = getDuration(dp.getChannel());
58         if (enable) {
59             gateway.disableDatapoint(dp, duration);
60         }
61         HmInterface hmInterface = dp.getChannel().getDevice().getHmInterface();
62         gateway.getRpcClient(hmInterface).setInstallMode(hmInterface, enable, duration);
63     }
64
65     /**
66      * Returns the virtual datapoint value for install mode duration.
67      */
68     private Integer getDuration(HmChannel channel) {
69         HmDatapoint dpDuration = channel
70                 .getDatapoint(HmDatapointInfo.createValuesInfo(channel, VIRTUAL_DATAPOINT_NAME_INSTALL_MODE_DURATION));
71         return dpDuration == null || dpDuration.getValue() == null || dpDuration.getType() != HmValueType.INTEGER ? 60
72                 : ((Number) dpDuration.getValue()).intValue();
73     }
74 }