]> git.basschouten.com Git - openhab-addons.git/blob
4ea7b25f155d7e75d48babaf08e4da6e30051e8e
[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.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                         "  \"availability_topic\": \"zigbee2mqtt/bridge/state\", " +
45                         "  \"availability_template\": \"{{value_json.state}}\", " +
46                         "  \"device\": { " +
47                         "    \"identifiers\": [ " +
48                         "      \"zigbee2mqtt_0x0000000000000000\" " +
49                         "    ], " +
50                         "    \"manufacturer\": \"Sensors inc\", " +
51                         "    \"model\": \"Sensor\", " +
52                         "    \"name\": \"Sensor\", " +
53                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
54                         "  }, " +
55                         "  \"name\": \"sensor1\", " +
56                         "  \"expire_after\": \"1\", " +
57                         "  \"force_update\": \"true\", " +
58                         "  \"unit_of_measurement\": \"W\", " +
59                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
60                         "  \"unique_id\": \"sn1\" " +
61                         "}");
62         // @formatter:on
63
64         assertThat(component.channels.size(), is(1));
65         assertThat(component.getName(), is("sensor1"));
66         assertThat(component.getGroupUID().getId(), is("sn1"));
67
68         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
69                 NumberValue.class);
70
71         publishMessage("zigbee2mqtt/bridge/state", "{ \"state\": \"online\" }");
72         assertThat(haThing.getStatus(), is(ThingStatus.ONLINE));
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 W"));
79
80         waitForAssert(() -> assertState(component, Sensor.SENSOR_CHANNEL_ID, UnDefType.UNDEF), 5000, 200);
81
82         publishMessage("zigbee2mqtt/bridge/state", "{ \"state\": \"offline\" }");
83         assertThat(haThing.getStatus(), is(ThingStatus.OFFLINE));
84     }
85
86     @Test
87     public void testMeasurementStateClass() throws InterruptedException {
88         // @formatter:off
89         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
90                 "{ " +
91                         "  \"device\": { " +
92                         "    \"identifiers\": [ " +
93                         "      \"zigbee2mqtt_0x0000000000000000\" " +
94                         "    ], " +
95                         "    \"manufacturer\": \"Sensors inc\", " +
96                         "    \"model\": \"Sensor\", " +
97                         "    \"name\": \"Sensor\", " +
98                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
99                         "  }, " +
100                         "  \"name\": \"sensor1\", " +
101                         "  \"expire_after\": \"1\", " +
102                         "  \"force_update\": \"true\", " +
103                         "  \"state_class\": \"measurement\", " +
104                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
105                         "  \"unique_id\": \"sn1\" " +
106                         "}");
107         // @formatter:on
108
109         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1",
110                 NumberValue.class);
111     }
112
113     @Test
114     public void testNonNumericSensor() throws InterruptedException {
115         // @formatter:off
116         var component = discoverComponent(configTopicToMqtt(CONFIG_TOPIC),
117                 "{ " +
118                         "  \"device\": { " +
119                         "    \"identifiers\": [ " +
120                         "      \"zigbee2mqtt_0x0000000000000000\" " +
121                         "    ], " +
122                         "    \"manufacturer\": \"Sensors inc\", " +
123                         "    \"model\": \"Sensor\", " +
124                         "    \"name\": \"Sensor\", " +
125                         "    \"sw_version\": \"Zigbee2MQTT 1.18.2\" " +
126                         "  }, " +
127                         "  \"name\": \"sensor1\", " +
128                         "  \"expire_after\": \"1\", " +
129                         "  \"force_update\": \"true\", " +
130                         "  \"state_topic\": \"zigbee2mqtt/sensor/state\", " +
131                         "  \"unique_id\": \"sn1\" " +
132                         "}");
133         // @formatter:on
134
135         assertChannel(component, Sensor.SENSOR_CHANNEL_ID, "zigbee2mqtt/sensor/state", "", "sensor1", TextValue.class);
136     }
137
138     @Override
139     protected Set<String> getConfigTopics() {
140         return Set.of(CONFIG_TOPIC);
141     }
142 }