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.OnOffType;
20 import org.openhab.core.types.Type;
23 * Converts between a Homematic datapoint value and an openHAB OnOffType.
25 * @author Gerhard Riegler - Initial contribution
27 public class OnOffTypeConverter extends AbstractTypeConverter<OnOffType> {
29 protected boolean toBindingValidation(HmDatapoint dp, Class<? extends Type> typeClass) {
30 return dp.isBooleanType() && typeClass.isAssignableFrom(OnOffType.class);
34 protected Object toBinding(OnOffType type, HmDatapoint dp) throws ConverterException {
35 return (type == OnOffType.OFF ? Boolean.FALSE : Boolean.TRUE) != isInvert(dp);
39 protected boolean fromBindingValidation(HmDatapoint dp) {
40 return dp.isBooleanType() && dp.getValue() instanceof Boolean;
44 protected OnOffType fromBinding(HmDatapoint dp) throws ConverterException {
45 return OnOffType.from(Boolean.FALSE.equals(dp.getValue()) == isInvert(dp));
49 * If the item is a sensor or a state from some devices, then OnOff must be inverted.
51 private boolean isInvert(HmDatapoint dp) {
52 return DATAPOINT_NAME_SENSOR.equals(dp.getName()) || isStateInvertDatapoint(dp);