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.model.HmDatapoint;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.PercentType;
22 import org.openhab.core.library.types.QuantityType;
23 import org.openhab.core.library.unit.ImperialUnits;
24 import org.openhab.core.library.unit.SIUnits;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
31 * {@link org.openhab.binding.homematic.internal.converter.type.AbstractTypeConverter#convertFromBinding(HmDatapoint)}.
33 * @author Michael Reitler - Initial Contribution
36 public class ConvertFromBindingTest extends BaseConverterTest {
39 public void testDecimalTypeConverter() throws ConverterException {
41 TypeConverter<?> decimalConverter = ConverterFactory.createConverter("Number");
43 // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
46 floatDp.setValue(99.9);
47 convertedState = decimalConverter.convertFromBinding(floatDp);
48 assertThat(convertedState, instanceOf(DecimalType.class));
49 assertThat(((DecimalType) convertedState).doubleValue(), is(99.9));
51 floatDp.setValue(77.77777778);
52 convertedState = decimalConverter.convertFromBinding(floatDp);
53 assertThat(convertedState, instanceOf(DecimalType.class));
54 assertThat(((DecimalType) convertedState).doubleValue(), is(77.777778));
56 integerDp.setValue(99.0);
57 convertedState = decimalConverter.convertFromBinding(integerDp);
58 assertThat(convertedState, instanceOf(DecimalType.class));
59 assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
61 integerDp.setValue(99.9);
62 convertedState = decimalConverter.convertFromBinding(integerDp);
63 assertThat(convertedState, instanceOf(DecimalType.class));
64 assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
67 @SuppressWarnings("null")
69 public void testQuantityTypeConverter() throws ConverterException {
71 TypeConverter<?> temperatureConverter = ConverterFactory.createConverter("Number:Temperature");
72 TypeConverter<?> frequencyConverter = ConverterFactory.createConverter("Number:Frequency");
73 TypeConverter<?> timeConverter = ConverterFactory.createConverter("Number:Time");
75 floatQuantityDp.setValue(10.5);
76 floatQuantityDp.setUnit("°C");
77 convertedState = temperatureConverter.convertFromBinding(floatQuantityDp);
78 assertThat(convertedState, instanceOf(QuantityType.class));
79 assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(SIUnits.CELSIUS.getDimension())));
80 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
81 assertThat(((QuantityType<?>) convertedState).toUnit(ImperialUnits.FAHRENHEIT).doubleValue(), is(50.9));
83 floatQuantityDp.setUnit("°C");
84 assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(SIUnits.CELSIUS.getDimension())));
85 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
87 integerQuantityDp.setValue(50000);
88 integerQuantityDp.setUnit("mHz");
89 convertedState = frequencyConverter.convertFromBinding(integerQuantityDp);
90 assertThat(convertedState, instanceOf(QuantityType.class));
91 assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(Units.HERTZ.getDimension())));
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(equalTo(Units.ONE.getDimension())));
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));
106 public void testPercentTypeConverter() throws ConverterException {
107 State convertedState;
108 TypeConverter<?> percentTypeConverter = ConverterFactory.createConverter("Dimmer");
110 // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
111 integerDp.setUnit("%");
113 integerDp.setValue(99.9);
114 integerDp.setMaxValue(100);
115 convertedState = percentTypeConverter.convertFromBinding(integerDp);
116 assertThat(convertedState, instanceOf(PercentType.class));
117 assertThat(((PercentType) convertedState).doubleValue(), is(99.0));
119 integerDp.setValue(77.77777778);
120 convertedState = percentTypeConverter.convertFromBinding(integerDp);
121 assertThat(convertedState, instanceOf(PercentType.class));
122 assertThat(((PercentType) convertedState).doubleValue(), is(77.0));
124 integerDp.setValue("");
125 convertedState = percentTypeConverter.convertFromBinding(integerDp);
126 assertThat(convertedState, instanceOf(UnDefType.class));
127 assertThat(((UnDefType) convertedState), is(UnDefType.NULL));