2 * Copyright (c) 2010-2023 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.*;
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;
24 * A virtual datapoint that converts the ENUM state of the HMIP-SWDO device to a contact.
26 * @author Gerhard Riegler - Initial contribution
28 public class StateContactVirtualDatapointHandler extends AbstractVirtualDatapointHandler {
30 public String getName() {
31 return VIRTUAL_DATAPOINT_NAME_STATE_CONTACT;
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);
49 public boolean canHandleEvent(HmDatapoint dp) {
50 return isApplicable(dp.getChannel().getDevice()) && DATAPOINT_NAME_STATE.equals(dp.getName());
54 public void handleEvent(VirtualGateway gateway, HmDatapoint dp) {
55 Object value = convertState(dp.getValue());
56 HmDatapoint vdp = getVirtualDatapoint(dp.getChannel());
60 private boolean isApplicable(HmDevice device) {
61 return device.getType().toUpperCase().startsWith("HMIP-SWDO");
64 private Boolean convertState(Object value) {
65 if (!(value instanceof Integer)) {
68 if ((int) value == 0) {
70 } else if ((int) value == 1) {