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.QuantityType;
22 import org.openhab.core.library.unit.ImperialUnits;
23 import org.openhab.core.library.unit.SIUnits;
24 import org.openhab.core.library.unit.Units;
25 import org.openhab.core.types.State;
29 * {@link org.openhab.binding.homematic.internal.converter.type.AbstractTypeConverter#convertFromBinding(HmDatapoint)}.
31 * @author Michael Reitler - Initial Contribution
34 public class ConvertFromBindingTest extends BaseConverterTest {
37 public void testDecimalTypeConverter() throws ConverterException {
39 TypeConverter<?> decimalConverter = ConverterFactory.createConverter("Number");
41 // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
44 floatDp.setValue(99.9);
45 convertedState = decimalConverter.convertFromBinding(floatDp);
46 assertThat(convertedState, instanceOf(DecimalType.class));
47 assertThat(((DecimalType) convertedState).doubleValue(), is(99.9));
49 floatDp.setValue(77.77777778);
50 convertedState = decimalConverter.convertFromBinding(floatDp);
51 assertThat(convertedState, instanceOf(DecimalType.class));
52 assertThat(((DecimalType) convertedState).doubleValue(), is(77.777778));
54 integerDp.setValue(99.0);
55 convertedState = decimalConverter.convertFromBinding(integerDp);
56 assertThat(convertedState, instanceOf(DecimalType.class));
57 assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
59 integerDp.setValue(99.9);
60 convertedState = decimalConverter.convertFromBinding(integerDp);
61 assertThat(convertedState, instanceOf(DecimalType.class));
62 assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
65 @SuppressWarnings("null")
67 public void testQuantityTypeConverter() throws ConverterException {
69 TypeConverter<?> temperatureConverter = ConverterFactory.createConverter("Number:Temperature");
70 TypeConverter<?> frequencyConverter = ConverterFactory.createConverter("Number:Frequency");
71 TypeConverter<?> timeConverter = ConverterFactory.createConverter("Number:Time");
73 floatQuantityDp.setValue(10.5);
74 floatQuantityDp.setUnit("°C");
75 convertedState = temperatureConverter.convertFromBinding(floatQuantityDp);
76 assertThat(convertedState, instanceOf(QuantityType.class));
77 assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(SIUnits.CELSIUS.getDimension())));
78 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
79 assertThat(((QuantityType<?>) convertedState).toUnit(ImperialUnits.FAHRENHEIT).doubleValue(), is(50.9));
81 floatQuantityDp.setUnit("°C");
82 assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(SIUnits.CELSIUS.getDimension())));
83 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
85 integerQuantityDp.setValue(50000);
86 integerQuantityDp.setUnit("mHz");
87 convertedState = frequencyConverter.convertFromBinding(integerQuantityDp);
88 assertThat(convertedState, instanceOf(QuantityType.class));
89 assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(Units.HERTZ.getDimension())));
90 assertThat(((QuantityType<?>) convertedState).intValue(), is(50000));
91 assertThat(((QuantityType<?>) convertedState).toUnit(Units.HERTZ).intValue(), is(50));
93 floatQuantityDp.setValue(0.7);
94 floatQuantityDp.setUnit("100%");
95 convertedState = timeConverter.convertFromBinding(floatQuantityDp);
96 assertThat(convertedState, instanceOf(QuantityType.class));
97 assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(Units.ONE.getDimension())));
98 assertThat(((QuantityType<?>) convertedState).doubleValue(), is(70.0));
99 assertThat(((QuantityType<?>) convertedState).getUnit(), is(Units.PERCENT));
100 assertThat(((QuantityType<?>) convertedState).toUnit(Units.ONE).doubleValue(), is(0.7));