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.converter.type;
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.DATAPOINT_NAME_SENSOR;
17 import org.openhab.binding.homematic.internal.converter.ConverterException;
18 import org.openhab.binding.homematic.internal.model.HmDatapoint;
19 import org.openhab.core.library.types.OpenClosedType;
20 import org.openhab.core.types.Type;
23 * Converts between a Homematic datapoint value and an openHAB OpenClosedType.
25 * @author Gerhard Riegler - Initial contribution
27 public class OpenClosedTypeConverter extends AbstractTypeConverter<OpenClosedType> {
29 protected boolean toBindingValidation(HmDatapoint dp, Class<? extends Type> typeClass) {
30 return dp.isBooleanType() && typeClass.isAssignableFrom(OpenClosedType.class);
34 protected Object toBinding(OpenClosedType type, HmDatapoint dp) throws ConverterException {
35 return (type == OpenClosedType.CLOSED ? Boolean.FALSE : Boolean.TRUE) != isInvert(dp);
39 protected boolean fromBindingValidation(HmDatapoint dp) {
40 return dp.isBooleanType() && dp.getValue() instanceof Boolean;
44 protected OpenClosedType fromBinding(HmDatapoint dp) throws ConverterException {
45 return Boolean.FALSE.equals(dp.getValue()) != isInvert(dp) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
49 * Invert only values which are not from a sensor or a state from some devices.
51 private boolean isInvert(HmDatapoint dp) {
52 return !DATAPOINT_NAME_SENSOR.equals(dp.getName()) && !isStateInvertDatapoint(dp);