]> git.basschouten.com Git - openhab-addons.git/blob
c2abf74b39c07c0fe8c2fd938daf1f9774eed5b4
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.thing.ThingStatus;
27 import org.openhab.core.types.UnDefType;
28
29 /**
30  * Tests for {@link Sensor}
31  *
32  * @author Anton Kharuzhy - Initial contribution
33  */
34 @NonNullByDefault
35 public class SensorTests extends AbstractComponentTests {
36     public static final String CONFIG_TOPIC = "sensor/0x0000000000000000_sensor_zigbee2mqtt";
37
38     @SuppressWarnings("null")
39     @Test
40     public void test() throws InterruptedException {
41         // @formatter:off
42         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
43                 """
44                 { \
45                   "availability_topic": "zigbee2mqtt/bridge/state", \
46                   "availability_template": "{{value_json.state}}", \
47                   "device": { \
48                     "identifiers": [ \
49                       "zigbee2mqtt_0x0000000000000000" \
50                     ], \
51                     "manufacturer": "Sensors inc", \
52                     "model": "Sensor", \
53                     "name": "Sensor", \
54                     "sw_version": "Zigbee2MQTT 1.18.2" \
55                   }, \
56                   "name": "sensor1", \
57                   "expire_after": "1", \
58                   "force_update": "true", \
59                   "unit_of_measurement": "W", \
60                   "state_topic": "zigbee2mqtt/sensor/state", \
61                   "unique_id": "sn1" \
62                 }\
63                 """);
64         // @formatter:on
65
66         assertThat(component.channels.size(), is(1));
67         assertThat(component.getName(), is("sensor1"));
68         assertThat(component.getGroupId(), is("sn1"));
69
70         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
71                 NumberValue.class);
72
73         publishMessage("zigbee2mqtt/bridge/state", "{ \"state\": \"online\" }");
74         assertThat(haThing.getStatus(), is(ThingStatus.ONLINE));
75         publishMessage("zigbee2mqtt/sensor/state", "10");
76         assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(10, Units.WATT));
77         publishMessage("zigbee2mqtt/sensor/state", "20");
78         assertState(component, Sensor.SENSOR_CHANNEL_ID, new QuantityType<>(20, Units.WATT));
79         assertThat(component.getChannel(Sensor.SENSOR_CHANNEL_ID).getState().getCache().createStateDescription(true)
80                 .build().getPattern(), is("%.0f %unit%"));
81
82         waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 5000, 200);
83
84         publishMessage("zigbee2mqtt/bridge/state", "{ \"state\": \"offline\" }");
85         assertThat(haThing.getStatus(), is(ThingStatus.OFFLINE));
86     }
87
88     @Test
89     public void testMeasurementStateClass() throws InterruptedException {
90         // @formatter:off
91         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
92                 """
93                 { \
94                   "device": { \
95                     "identifiers": [ \
96                       "zigbee2mqtt_0x0000000000000000" \
97                     ], \
98                     "manufacturer": "Sensors inc", \
99                     "model": "Sensor", \
100                     "name": "Sensor", \
101                     "sw_version": "Zigbee2MQTT 1.18.2" \
102                   }, \
103                   "name": "sensor1", \
104                   "expire_after": "1", \
105                   "force_update": "true", \
106                   "state_class": "measurement", \
107                   "state_topic": "zigbee2mqtt/sensor/state", \
108                   "unique_id": "sn1" \
109                 }\
110                 """);
111         // @formatter:on
112
113         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
114                 NumberValue.class);
115     }
116
117     @Test
118     public void testNonNumericSensor() throws InterruptedException {
119         // @formatter:off
120         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
121                 """
122                 { \
123                   "device": { \
124                     "identifiers": [ \
125                       "zigbee2mqtt_0x0000000000000000" \
126                     ], \
127                     "manufacturer": "Sensors inc", \
128                     "model": "Sensor", \
129                     "name": "Sensor", \
130                     "sw_version": "Zigbee2MQTT 1.18.2" \
131                   }, \
132                   "name": "sensor1", \
133                   "expire_after": "1", \
134                   "force_update": "true", \
135                   "state_topic": "zigbee2mqtt/sensor/state", \
136                   "unique_id": "sn1" \
137                 }\
138                 """);
139         // @formatter:on
140
141         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1", TextValue.class);
142     }
143
144     @Override
145     protected Set<String> getConfigTopics() {
146         return Set.of(CONFIG_TOPIC);
147     }
148 }