2 * Copyright (c) 2010-2022 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.mqtt.homie.internal.discovery;
15 import java.nio.charset.StandardCharsets;
16 import java.util.Collections;
17 import java.util.HashMap;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.mqtt.discovery.AbstractMQTTDiscovery;
23 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
24 import org.openhab.binding.mqtt.homie.generic.internal.MqttBindingConstants;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.io.transport.mqtt.MqttBrokerConnection;
28 import org.openhab.core.thing.ThingUID;
29 import org.osgi.service.component.annotations.Activate;
30 import org.osgi.service.component.annotations.Component;
31 import org.osgi.service.component.annotations.Reference;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * The {@link Homie300Discovery} is responsible for discovering device nodes that follow the
37 * Homie 3.x convention (https://github.com/homieiot/convention).
39 * @author David Graeff - Initial contribution
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.mqtthomie")
43 public class Homie300Discovery extends AbstractMQTTDiscovery {
44 private final Logger logger = LoggerFactory.getLogger(Homie300Discovery.class);
46 protected final MQTTTopicDiscoveryService discoveryService;
49 public Homie300Discovery(@Reference MQTTTopicDiscoveryService discoveryService) {
50 super(Collections.singleton(MqttBindingConstants.HOMIE300_MQTT_THING), 3, true, "+/+/$homie");
51 this.discoveryService = discoveryService;
55 protected MQTTTopicDiscoveryService getDiscoveryService() {
56 return discoveryService;
60 * @param topic A topic like "homie/mydevice/$homie"
61 * @return Returns the "mydevice" part of the example
63 public static @Nullable String extractDeviceID(String topic) {
64 String[] strings = topic.split("/");
65 if (strings.length > 2) {
72 * Returns true if the version is something like "3.x" or "4.x".
74 public static boolean checkVersion(byte[] payload) {
75 return payload.length > 0 && (payload[0] == '3' || payload[0] == '4');
79 public void receivedMessage(ThingUID connectionBridge, MqttBrokerConnection connection, String topic,
83 if (!checkVersion(payload)) {
84 logger.trace("Found homie device. But version {} is out of range.",
85 new String(payload, StandardCharsets.UTF_8));
88 final String deviceID = extractDeviceID(topic);
89 if (deviceID == null) {
90 logger.trace("Found homie device. But deviceID {} is invalid.", deviceID);
94 publishDevice(connectionBridge, connection, deviceID, topic, deviceID);
97 void publishDevice(ThingUID connectionBridge, MqttBrokerConnection connection, String deviceID, String topic,
99 Map<String, Object> properties = new HashMap<>();
100 properties.put("deviceid", deviceID);
101 properties.put("basetopic", topic.substring(0, topic.indexOf("/")));
103 thingDiscovered(DiscoveryResultBuilder
104 .create(new ThingUID(MqttBindingConstants.HOMIE300_MQTT_THING, connectionBridge, deviceID))
105 .withBridge(connectionBridge).withProperties(properties).withRepresentationProperty("deviceid")
106 .withLabel(name).build());
110 public void topicVanished(ThingUID connectionBridge, MqttBrokerConnection connection, String topic) {
111 String deviceID = extractDeviceID(topic);
112 if (deviceID == null) {
115 thingRemoved(new ThingUID(MqttBindingConstants.HOMIE300_MQTT_THING, connectionBridge, deviceID));