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;
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
18 import org.junit.jupiter.api.Test;
19 import org.openhab.binding.homematic.internal.converter.type.AbstractTypeConverter;
20 import org.openhab.binding.homematic.internal.model.HmDatapoint;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.ImperialUnits;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.types.State;
27 import tech.units.indriya.unit.UnitDimension;
30 * Tests for {@link AbstractTypeConverter#convertFromBinding(HmDatapoint)}.
32 * @author Michael Reitler - Initial Contribution
35 public class ConvertFromBindingTest extends BaseConverterTest {
38 public void testDecimalTypeConverter() throws ConverterException {
40 TypeConverter<?> decimalConverter = ConverterFactory.createConverter("Number");
42 // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
45 floatDp.setValue(99.9);
46 convertedState = decimalConverter.convertFromBinding(floatDp);
47 assertThat(convertedState, instanceOf(DecimalType.class));
48 assertThat(((DecimalType) convertedState).doubleValue(), is(99.9));
50 floatDp.setValue(77.77777778);
51 convertedState = decimalConverter.convertFromBinding(floatDp);
52 assertThat(convertedState, instanceOf(DecimalType.class));
53 assertThat(((DecimalType) convertedState).doubleValue(), is(77.777778));
55 integerDp.setValue(99.0);
56 convertedState = decimalConverter.convertFromBinding(integerDp);
57 assertThat(convertedState, instanceOf(DecimalType.class));
58 assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
60 integerDp.setValue(99.9);
61 convertedState = decimalConverter.convertFromBinding(integerDp);
62 assertThat(convertedState, instanceOf(DecimalType.class));
63 assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
66 @SuppressWarnings("null")
68 public void testQuantityTypeConverter() throws ConverterException {
70 TypeConverter<?> temperatureConverter = ConverterFactory.createConverter("Number:Temperature");
71 TypeConverter<?> frequencyConverter = ConverterFactory.createConverter("Number:Frequency");
72 TypeConverter<?> timeConverter = ConverterFactory.createConverter("Number:Time");
74 floatQuantityDp.setValue(10.5);
75 floatQuantityDp.setUnit("°C");
76 convertedState = temperatureConverter.convertFromBinding(floatQuantityDp);
77 assertThat(convertedState, instanceOf(QuantityType.class));
78 assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.TEMPERATURE));
79 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
80 assertThat(((QuantityType<?>) convertedState).toUnit(ImperialUnits.FAHRENHEIT).doubleValue(), is(50.9));
82 floatQuantityDp.setUnit("°C");
83 assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.TEMPERATURE));
84 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
86 integerQuantityDp.setValue(50000);
87 integerQuantityDp.setUnit("mHz");
88 convertedState = frequencyConverter.convertFromBinding(integerQuantityDp);
89 assertThat(convertedState, instanceOf(QuantityType.class));
90 assertThat(((QuantityType<?>) convertedState).getDimension(),
91 is(UnitDimension.NONE.divide(UnitDimension.TIME)));
92 assertThat(((QuantityType<?>) convertedState).intValue(), is(50000));
93 assertThat(((QuantityType<?>) convertedState).toUnit(Units.HERTZ).intValue(), is(50));
95 floatQuantityDp.setValue(0.7);
96 floatQuantityDp.setUnit("100%");
97 convertedState = timeConverter.convertFromBinding(floatQuantityDp);
98 assertThat(convertedState, instanceOf(QuantityType.class));
99 assertThat(((QuantityType<?>) convertedState).getDimension(), is(UnitDimension.NONE));
100 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(70.0));
101 assertThat(((QuantityType<?>) convertedState).getUnit(), is(Units.PERCENT));
102 assertThat(((QuantityType<?>) convertedState).toUnit(Units.ONE).doubleValue(), is(0.7));