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.anel.internal;
15 import static org.hamcrest.MatcherAssert.assertThat;
16 import static org.hamcrest.Matchers.*;
18 import java.util.HashMap;
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;
33 * This class tests {@link AnelStateUpdater}.
35 * @author Patrick Koenemann - Initial contribution
38 public class AnelStateUpdaterTest implements IAnelTestStatus, IAnelConstants {
40 private final AnelStateUpdater stateUpdater = new AnelStateUpdater();
43 public void noStateChange() {
45 final AnelState oldState = AnelState.of(STATUS_HUT_V5);
46 final AnelState newState = AnelState.of(STATUS_HUT_V5.replace(":80:", ":81:")); // port is irrelevant
48 Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
50 assertThat(updates.entrySet(), is(empty()));
54 public void fromNullStateUpdatesHome() {
56 final AnelState newState = AnelState.of(STATUS_HOME_V46);
58 Map<String, State> updates = stateUpdater.getChannelUpdates(null, newState);
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));
67 assertThat(updates, equalTo(expected));
71 public void fromNullStateUpdatesHutPowerSensor() {
73 final AnelState newState = AnelState.of(STATUS_HUT_V61_POW_SENSOR);
75 Map<String, State> updates = stateUpdater.getChannelUpdates(null, newState);
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);
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);
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));
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));
98 public void singleRelayStateChange() {
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"));
103 Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
105 final Map<String, State> expected = new HashMap<>();
106 expected.put(CHANNEL_RELAY_STATE.get(3), OnOffType.ON);
107 assertThat(updates, equalTo(expected));
111 public void temperatureChange() {
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:"));
116 Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
118 assertThat(updates.size(), is(1));
119 assertTemperature(updates.get(CHANNEL_TEMPERATURE), 27.1);
123 public void singleSensorStatesChange() {
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:"));
128 Map<String, State> updates = stateUpdater.getChannelUpdates(oldState, newState);
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);
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));