]> git.basschouten.com Git - openhab-addons.git/blob
50876df94f6b3b79b8773ca73e05f33360b124f8
[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.anel.internal;
14
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
17
18 import java.util.HashMap;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.junit.jupiter.api.Test;
24 import org.openhab.binding.anel.internal.state.AnelState;
25 import org.openhab.binding.anel.internal.state.AnelStateUpdater;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.types.State;
31
32 /**
33  * This class tests {@link AnelStateUpdater}.
34  *
35  * @author Patrick Koenemann - Initial contribution
36  */
37 @NonNullByDefault
38 public class AnelStateUpdaterTest implements IAnelTestStatus, IAnelConstants {
39
40     private final AnelStateUpdater stateUpdater = new AnelStateUpdater();
41
42     @Test
43     public void noStateChange() {
44         // given
45         final AnelState oldState = AnelState.of(STATUS_HUT_V5);
46         final AnelState newState = AnelState.of(STATUS_HUT_V5.replace(":80:", ":81:")); // port is irrelevant
47         // when
48         Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
49         // then
50         assertThat(updates.entrySet(), is(empty()));
51     }
52
53     @Test
54     public void fromNullStateUpdatesHome() {
55         // given
56         final AnelState newState = AnelState.of(STATUS_HOME_V46);
57         // when
58         Map<String, State> updates = stateUpdater.getChannelUpdates(null, newState);
59         // then
60         final Map<String, State> expected = new HashMap<>();
61         expected.put(CHANNEL_NAME, new StringType("NET-CONTROL"));
62         for (int i = 1; i <= 8; i++) {
63             expected.put(CHANNEL_RELAY_NAME.get(i - 1), new StringType("Nr. " + i));
64             expected.put(CHANNEL_RELAY_STATE.get(i - 1), OnOffType.from(i % 2 == 1));
65             expected.put(CHANNEL_RELAY_LOCKED.get(i - 1), OnOffType.from(i > 3));
66         }
67         assertThat(updates, equalTo(expected));
68     }
69
70     @Test
71     public void fromNullStateUpdatesHutPowerSensor() {
72         // given
73         final AnelState newState = AnelState.of(STATUS_HUT_V61_POW_SENSOR);
74         // when
75         Map<String, State> updates = stateUpdater.getChannelUpdates(null, newState);
76         // then
77         assertThat(updates.size(), is(5 + 8 * 6));
78         assertThat(updates.get(CHANNEL_NAME), equalTo(new StringType("NET-CONTROL")));
79         assertTemperature(updates.get(CHANNEL_TEMPERATURE), 27.7);
80
81         assertThat(updates.get(CHANNEL_SENSOR_BRIGHTNESS), equalTo(new DecimalType("7")));
82         assertThat(updates.get(CHANNEL_SENSOR_HUMIDITY), equalTo(new DecimalType("40.7")));
83         assertTemperature(updates.get(CHANNEL_SENSOR_TEMPERATURE), 20.61);
84
85         for (int i = 1; i <= 8; i++) {
86             assertThat(updates.get(CHANNEL_RELAY_NAME.get(i - 1)), equalTo(new StringType("Nr. " + i)));
87             assertThat(updates.get(CHANNEL_RELAY_STATE.get(i - 1)), equalTo(OnOffType.from(i <= 3 || i >= 7)));
88             assertThat(updates.get(CHANNEL_RELAY_LOCKED.get(i - 1)), equalTo(OnOffType.OFF));
89         }
90         for (int i = 1; i <= 8; i++) {
91             assertThat(updates.get(CHANNEL_IO_NAME.get(i - 1)), equalTo(new StringType("IO-" + i)));
92             assertThat(updates.get(CHANNEL_IO_STATE.get(i - 1)), equalTo(OnOffType.OFF));
93             assertThat(updates.get(CHANNEL_IO_MODE.get(i - 1)), equalTo(OnOffType.OFF));
94         }
95     }
96
97     @Test
98     public void singleRelayStateChange() {
99         // given
100         final AnelState oldState = AnelState.of(STATUS_HUT_V61_POW_SENSOR);
101         final AnelState newState = AnelState.of(STATUS_HUT_V61_POW_SENSOR.replace("Nr. 4,0", "Nr. 4,1"));
102         // when
103         Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
104         // then
105         final Map<String, State> expected = new HashMap<>();
106         expected.put(CHANNEL_RELAY_STATE.get(3), OnOffType.ON);
107         assertThat(updates, equalTo(expected));
108     }
109
110     @Test
111     public void temperatureChange() {
112         // given
113         final AnelState oldState = AnelState.of(STATUS_HUT_V65);
114         final AnelState newState = AnelState.of(STATUS_HUT_V65.replaceFirst(":27\\.0(.)C:", ":27.1°C:"));
115         // when
116         Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
117         // then
118         assertThat(updates.size(), is(1));
119         assertTemperature(updates.get(CHANNEL_TEMPERATURE), 27.1);
120     }
121
122     @Test
123     public void singleSensorStatesChange() {
124         // given
125         final AnelState oldState = AnelState.of(STATUS_HUT_V61_SENSOR);
126         final AnelState newState = AnelState.of(STATUS_HUT_V61_SENSOR.replace(":s:20.61:40.7:7.0:", ":s:20.6:40:7.1:"));
127         // when
128         Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
129         // then
130         assertThat(updates.size(), is(3));
131         assertThat(updates.get(CHANNEL_SENSOR_BRIGHTNESS), equalTo(new DecimalType("7.1")));
132         assertThat(updates.get(CHANNEL_SENSOR_HUMIDITY), equalTo(new DecimalType("40")));
133         assertTemperature(updates.get(CHANNEL_SENSOR_TEMPERATURE), 20.6);
134     }
135
136     private void assertTemperature(@Nullable State state, double value) {
137         assertThat(state, isA(QuantityType.class));
138         if (state instanceof QuantityType<?>) {
139             assertThat(((QuantityType<?>) state).doubleValue(), closeTo(value, 0.0001d));
140         }
141     }
142 }