2 * Copyright (c) 2010-2021 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.homematic.internal.communicator.virtual;
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.VIRTUAL_DATAPOINT_NAME_BUTTON;
17 import org.apache.commons.lang.StringUtils;
18 import org.openhab.binding.homematic.internal.misc.MiscUtils;
19 import org.openhab.binding.homematic.internal.model.HmChannel;
20 import org.openhab.binding.homematic.internal.model.HmDatapoint;
21 import org.openhab.binding.homematic.internal.model.HmDevice;
22 import org.openhab.binding.homematic.internal.model.HmValueType;
23 import org.openhab.core.thing.CommonTriggerEvents;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * A virtual String datapoint which adds a BUTTON datapoint. It will forward key events to the
29 * system channel {@link DefaultSystemChannelTypeProvider#SYSTEM_BUTTON}.
31 * @author Michael Reitler - Initial contribution
33 public class ButtonVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
34 private final Logger logger = LoggerFactory.getLogger(ButtonVirtualDatapointHandler.class);
37 public String getName() {
38 return VIRTUAL_DATAPOINT_NAME_BUTTON;
42 public void initialize(HmDevice device) {
43 for (HmChannel channel : device.getChannels()) {
44 if (channel.hasPressDatapoint()) {
45 HmDatapoint dp = addDatapoint(device, channel.getNumber(), getName(), HmValueType.STRING, null, false);
47 dp.setOptions(new String[] { CommonTriggerEvents.SHORT_PRESSED, CommonTriggerEvents.LONG_PRESSED,
48 CommonTriggerEvents.DOUBLE_PRESSED });
54 public boolean canHandleEvent(HmDatapoint dp) {
55 return dp.isPressDatapoint();
59 public void handleEvent(VirtualGateway gateway, HmDatapoint dp) {
60 HmDatapoint vdp = getVirtualDatapoint(dp.getChannel());
61 if (MiscUtils.isTrueValue(dp.getValue())) {
62 String pressType = StringUtils.substringAfter(dp.getName(), "_");
65 if (dp.getValue() == null || !dp.getValue().equals(dp.getPreviousValue())) {
66 vdp.setValue(CommonTriggerEvents.SHORT_PRESSED);
68 // two (or more) PRESS_SHORT events were received
69 // within AbstractHomematicGateway#DEFAULT_DISABLE_DELAY seconds
70 vdp.setValue(CommonTriggerEvents.DOUBLE_PRESSED);
74 vdp.setValue(CommonTriggerEvents.LONG_PRESSED);
82 logger.warn("Unexpected vaule '{}' for PRESS virtual datapoint", pressType);