]> git.basschouten.com Git - openhab-addons.git/blob
4b7e8ef68e2f16352cdb53298cc801bed138842e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.homematic.internal.converter;
14
15 import static org.hamcrest.CoreMatchers.*;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
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;
26
27 /**
28  * Tests for
29  * {@link org.openhab.binding.homematic.internal.converter.type.AbstractTypeConverter#convertFromBinding(HmDatapoint)}.
30  *
31  * @author Michael Reitler - Initial Contribution
32  *
33  */
34 public class ConvertFromBindingTest extends BaseConverterTest {
35
36     @Test
37     public void testDecimalTypeConverter() throws ConverterException {
38         State convertedState;
39         TypeConverter<?> decimalConverter = ConverterFactory.createConverter("Number");
40
41         // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
42         floatDp.setUnit("%");
43
44         floatDp.setValue(99.9);
45         convertedState = decimalConverter.convertFromBinding(floatDp);
46         assertThat(convertedState, instanceOf(DecimalType.class));
47         assertThat(((DecimalType) convertedState).doubleValue(), is(99.9));
48
49         floatDp.setValue(77.77777778);
50         convertedState = decimalConverter.convertFromBinding(floatDp);
51         assertThat(convertedState, instanceOf(DecimalType.class));
52         assertThat(((DecimalType) convertedState).doubleValue(), is(77.777778));
53
54         integerDp.setValue(99.0);
55         convertedState = decimalConverter.convertFromBinding(integerDp);
56         assertThat(convertedState, instanceOf(DecimalType.class));
57         assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
58
59         integerDp.setValue(99.9);
60         convertedState = decimalConverter.convertFromBinding(integerDp);
61         assertThat(convertedState, instanceOf(DecimalType.class));
62         assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
63     }
64
65     @SuppressWarnings("null")
66     @Test
67     public void testQuantityTypeConverter() throws ConverterException {
68         State convertedState;
69         TypeConverter<?> temperatureConverter = ConverterFactory.createConverter("Number:Temperature");
70         TypeConverter<?> frequencyConverter = ConverterFactory.createConverter("Number:Frequency");
71         TypeConverter<?> timeConverter = ConverterFactory.createConverter("Number:Time");
72
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));
80
81         floatQuantityDp.setUnit("°C");
82         assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(SIUnits.CELSIUS.getDimension())));
83         assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
84
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));
92
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));
101     }
102 }