]> git.basschouten.com Git - openhab-addons.git/blob
a92237980edd2447cd2226565a739bdf2e2830c2
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.mqtt.homeassistant.internal.component;
14
15 import static org.hamcrest.CoreMatchers.is;
16 import static org.hamcrest.MatcherAssert.assertThat;
17
18 import java.util.Set;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.junit.jupiter.api.Test;
22 import org.openhab.binding.mqtt.generic.values.NumberValue;
23 import org.openhab.binding.mqtt.generic.values.TextValue;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.UnDefType;
27
28 /**
29  * Tests for {@link Sensor}
30  *
31  * @author Anton Kharuzhy - Initial contribution
32  */
33 @SuppressWarnings("ConstantConditions")
34 @NonNullByDefault
35 public class SensorTests extends AbstractComponentTests {
36     public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
37
38     @Test
39     public void test() throws InterruptedException {
40         // @formatter:off
41         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
42                 "{ " +
43                         "  \"availability\": [ " +
44                         "    { " +
45                         "      \"topic\": \"zigbee2mqtt/bridge/state\" " +
46                         "    } " +
47                         "  ], " +
48                         "  \"device\": { " +
49                         "    \"identifiers\": [ " +
50                         "      \"zigbee2mqtt_0x0000000000000000\" " +
51                         "    ], " +
52                         "    \"manufacturer\": \"Sensors inc\", " +
53                         "    \"model\": \"Sensor\", " +
54                         "    \"name\": \"Sensor\", " +
55                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
56                         "  }, " +
57                         "  \"name\": \"sensor1\", " +
58                         "  \"expire_after\": \"1\", " +
59                         "  \"force_update\": \"true\", " +
60                         "  \"unit_of_measurement\": \"W\", " +
61                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
62                         "  \"unique_id\": \"sn1\" " +
63                         "}");
64         // @formatter:on
65
66         assertThat(component.channels.size(), is(1));
67         assertThat(component.getName(), is("sensor1"));
68         assertThat(component.getGroupUID().getId(), is("sn1"));
69
70         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
71                 NumberValue.class);
72
73         publishMessage("zigbee2mqtt/sensor/state", "10");
74         assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(10, Units.WATT));
75         publishMessage("zigbee2mqtt/sensor/state", "20");
76         assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(20, Units.WATT));
77         assertThat(component.getChannel(Sensor.SENSOR_CHANNEL_ID).getState().getCache().createStateDescription(true)
78                 .build().getPattern(), is("%s %unit%"));
79
80         waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 10000, 200);
81     }
82
83     @Test
84     public void testMeasurementStateClass() throws InterruptedException {
85         // @formatter:off
86         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
87                 "{ " +
88                         "  \"device\": { " +
89                         "    \"identifiers\": [ " +
90                         "      \"zigbee2mqtt_0x0000000000000000\" " +
91                         "    ], " +
92                         "    \"manufacturer\": \"Sensors inc\", " +
93                         "    \"model\": \"Sensor\", " +
94                         "    \"name\": \"Sensor\", " +
95                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
96                         "  }, " +
97                         "  \"name\": \"sensor1\", " +
98                         "  \"expire_after\": \"1\", " +
99                         "  \"force_update\": \"true\", " +
100                         "  \"state_class\": \"measurement\", " +
101                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
102                         "  \"unique_id\": \"sn1\" " +
103                         "}");
104         // @formatter:on
105
106         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
107                 NumberValue.class);
108     }
109
110     @Test
111     public void testNonNumericSensor() throws InterruptedException {
112         // @formatter:off
113         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
114                 "{ " +
115                         "  \"device\": { " +
116                         "    \"identifiers\": [ " +
117                         "      \"zigbee2mqtt_0x0000000000000000\" " +
118                         "    ], " +
119                         "    \"manufacturer\": \"Sensors inc\", " +
120                         "    \"model\": \"Sensor\", " +
121                         "    \"name\": \"Sensor\", " +
122                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
123                         "  }, " +
124                         "  \"name\": \"sensor1\", " +
125                         "  \"expire_after\": \"1\", " +
126                         "  \"force_update\": \"true\", " +
127                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
128                         "  \"unique_id\": \"sn1\" " +
129                         "}");
130         // @formatter:on
131
132         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1", TextValue.class);
133     }
134
135     @Override
136     protected Set<String> getConfigTopics() {
137         return Set.of(CONFIG_TOPIC);
138     }
139 }