]> git.basschouten.com Git - openhab-addons.git/blob
9abc3a465ac3eae8127367d78824a5139e7987a4
[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.converter.type;
14
15 import static org.openhab.binding.homematic.internal.misc.HomematicConstants.DATAPOINT_NAME_SENSOR;
16
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;
21
22 /**
23  * Converts between a Homematic datapoint value and an openHAB OpenClosedType.
24  *
25  * @author Gerhard Riegler - Initial contribution
26  */
27 public class OpenClosedTypeConverter extends AbstractTypeConverter<OpenClosedType> {
28     @Override
29     protected boolean toBindingValidation(HmDatapoint dp, Class<? extends Type> typeClass) {
30         return dp.isBooleanType() && typeClass.isAssignableFrom(OpenClosedType.class);
31     }
32
33     @Override
34     protected Object toBinding(OpenClosedType type, HmDatapoint dp) throws ConverterException {
35         return (type == OpenClosedType.CLOSED ? Boolean.FALSE : Boolean.TRUE) != isInvert(dp);
36     }
37
38     @Override
39     protected boolean fromBindingValidation(HmDatapoint dp) {
40         return dp.isBooleanType() && dp.getValue() instanceof Boolean;
41     }
42
43     @Override
44     protected OpenClosedType fromBinding(HmDatapoint dp) throws ConverterException {
45         return Boolean.FALSE.equals(dp.getValue()) != isInvert(dp) ? OpenClosedType.CLOSED : OpenClosedType.OPEN;
46     }
47
48     /**
49      * Invert only values which are not from a sensor or a state from some devices.
50      */
51     private boolean isInvert(HmDatapoint dp) {
52         return !DATAPOINT_NAME_SENSOR.equals(dp.getName()) && !isStateInvertDatapoint(dp);
53     }
54 }