]> git.basschouten.com Git - openhab-addons.git/blob
514dccf03099a55305fdc38ab65b3f18098c7830
[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.OnOffType;
20 import org.openhab.core.types.Type;
21
22 /**
23  * Converts between a Homematic datapoint value and an openHAB OnOffType.
24  *
25  * @author Gerhard Riegler - Initial contribution
26  */
27 public class OnOffTypeConverter extends AbstractTypeConverter<OnOffType> {
28     @Override
29     protected boolean toBindingValidation(HmDatapoint dp, Class<? extends Type> typeClass) {
30         return dp.isBooleanType() && typeClass.isAssignableFrom(OnOffType.class);
31     }
32
33     @Override
34     protected Object toBinding(OnOffType type, HmDatapoint dp) throws ConverterException {
35         return (type == OnOffType.OFF ? 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 OnOffType fromBinding(HmDatapoint dp) throws ConverterException {
45         return Boolean.FALSE.equals(dp.getValue()) != isInvert(dp) ? OnOffType.OFF : OnOffType.ON;
46     }
47
48     /**
49      * If the item is a sensor or a state from some devices, then OnOff must be inverted.
50      */
51     private boolean isInvert(HmDatapoint dp) {
52         return DATAPOINT_NAME_SENSOR.equals(dp.getName()) || isStateInvertDatapoint(dp);
53     }
54 }