]> git.basschouten.com Git - openhab-addons.git/blob
2dfc759f9a64ecd7fb12f017f267226b4a54b5cc
[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.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;
28
29 /**
30  * Tests for
31  * {@link org.openhab.binding.homematic.internal.converter.type.AbstractTypeConverter#convertFromBinding(HmDatapoint)}.
32  *
33  * @author Michael Reitler - Initial Contribution
34  *
35  */
36 public class ConvertFromBindingTest extends BaseConverterTest {
37
38     @Test
39     public void testDecimalTypeConverter() throws ConverterException {
40         State convertedState;
41         TypeConverter<?> decimalConverter = ConverterFactory.createConverter("Number");
42
43         // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
44         floatDp.setUnit("%");
45
46         floatDp.setValue(99.9);
47         convertedState = decimalConverter.convertFromBinding(floatDp);
48         assertThat(convertedState, instanceOf(DecimalType.class));
49         assertThat(((DecimalType) convertedState).doubleValue(), is(99.9));
50
51         floatDp.setValue(77.77777778);
52         convertedState = decimalConverter.convertFromBinding(floatDp);
53         assertThat(convertedState, instanceOf(DecimalType.class));
54         assertThat(((DecimalType) convertedState).doubleValue(), is(77.777778));
55
56         integerDp.setValue(99.0);
57         convertedState = decimalConverter.convertFromBinding(integerDp);
58         assertThat(convertedState, instanceOf(DecimalType.class));
59         assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
60
61         integerDp.setValue(99.9);
62         convertedState = decimalConverter.convertFromBinding(integerDp);
63         assertThat(convertedState, instanceOf(DecimalType.class));
64         assertThat(((DecimalType) convertedState).doubleValue(), is(99.0));
65     }
66
67     @SuppressWarnings("null")
68     @Test
69     public void testQuantityTypeConverter() throws ConverterException {
70         State convertedState;
71         TypeConverter<?> temperatureConverter = ConverterFactory.createConverter("Number:Temperature");
72         TypeConverter<?> frequencyConverter = ConverterFactory.createConverter("Number:Frequency");
73         TypeConverter<?> timeConverter = ConverterFactory.createConverter("Number:Time");
74
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));
82
83         floatQuantityDp.setUnit("°C");
84         assertThat(((QuantityType<?>) convertedState).getDimension(), is(equalTo(SIUnits.CELSIUS.getDimension())));
85         assertThat(((QuantityType<?>) convertedState).doubleValue(), is(10.5));
86
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));
94
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));
103     }
104
105     @Test
106     public void testPercentTypeConverter() throws ConverterException {
107         State convertedState;
108         TypeConverter<?> percentTypeConverter = ConverterFactory.createConverter("Dimmer");
109
110         // the binding is backwards compatible, so clients may still use DecimalType, even if a unit is used
111         integerDp.setUnit("%");
112
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));
118
119         integerDp.setValue(77.77777778);
120         convertedState = percentTypeConverter.convertFromBinding(integerDp);
121         assertThat(convertedState, instanceOf(PercentType.class));
122         assertThat(((PercentType) convertedState).doubleValue(), is(77.0));
123
124         integerDp.setValue("");
125         convertedState = percentTypeConverter.convertFromBinding(integerDp);
126         assertThat(convertedState, instanceOf(UnDefType.class));
127         assertThat(((UnDefType) convertedState), is(UnDefType.NULL));
128     }
129 }