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 java.math.BigDecimal;
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.DecimalType;
20 import org.openhab.core.types.Type;
23 * Converts between a Homematic datapoint value and an openHAB DecimalType.
25 * @author Gerhard Riegler - Initial contribution
27 public class DecimalTypeConverter extends AbstractTypeConverter<DecimalType> {
29 protected boolean toBindingValidation(HmDatapoint dp, Class<? extends Type> typeClass) {
30 return dp.isNumberType() && typeClass.isAssignableFrom(DecimalType.class);
34 protected Object toBinding(DecimalType type, HmDatapoint dp) throws ConverterException {
35 if (dp.isIntegerType()) {
36 return type.intValue();
38 return round(type.doubleValue()).doubleValue();
42 protected boolean fromBindingValidation(HmDatapoint dp) {
43 return dp.isNumberType() && dp.getValue() instanceof Number;
47 protected DecimalType fromBinding(HmDatapoint dp) throws ConverterException {
48 Number number = ((Number) dp.getValue()).doubleValue();
49 if (dp.isIntegerType()) {
50 return new DecimalType(new BigDecimal(number.intValue()));
52 return new DecimalType(round(number.doubleValue()));