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
14 package org.openhab.binding.mqtt.espmilighthub.internal.discovery;
16 import static org.openhab.binding.mqtt.MqttBindingConstants.BINDING_ID;
17 import static org.openhab.binding.mqtt.espmilighthub.internal.EspMilightHubBindingConstants.*;
19 import java.util.HashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.binding.mqtt.discovery.AbstractMQTTDiscovery;
24 import org.openhab.binding.mqtt.discovery.MQTTTopicDiscoveryService;
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.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Reference;
35 * The {@link EspMilightHubDiscoveryService} is responsible for finding globes
36 * and setting them up for the handlers.
38 * @author Matthew Skinner - Initial contribution
41 @Component(service = DiscoveryService.class, configurationPid = "discovery.mqttespmilighthub")
43 public class EspMilightHubDiscoveryService extends AbstractMQTTDiscovery {
44 protected final MQTTTopicDiscoveryService discoveryService;
47 public EspMilightHubDiscoveryService(@Reference MQTTTopicDiscoveryService discoveryService) {
48 super(SUPPORTED_THING_TYPES, 3, true, STATES_BASE_TOPIC + "#");
49 this.discoveryService = discoveryService;
53 protected MQTTTopicDiscoveryService getDiscoveryService() {
54 return discoveryService;
58 public void receivedMessage(ThingUID connectionBridge, MqttBrokerConnection connection, String topic,
61 if (topic.startsWith(STATES_BASE_TOPIC)) {
62 String cutTopic = topic.replace(STATES_BASE_TOPIC, "");
63 int index = cutTopic.indexOf("/");
64 if (index != -1) // -1 means "not found"
66 String remoteCode = (cutTopic.substring(0, index)); // Store the remote code for use later
67 cutTopic = topic.replace(STATES_BASE_TOPIC + remoteCode + "/", "");
68 index = cutTopic.indexOf("/");
70 String globeType = (cutTopic.substring(0, index));
71 String remoteGroupID = (cutTopic.substring(index + 1, index + 2));
72 // openHAB's framework has better code for handling groups then the firmware does
73 if (!"0".equals(remoteGroupID)) {// Users can manually add group 0 things if they wish
74 publishDevice(connectionBridge, connection, topic, remoteCode, globeType, remoteGroupID);
81 void publishDevice(ThingUID connectionBridge, MqttBrokerConnection connection, String topic, String remoteCode,
82 String globeType, String remoteGroupID) {
83 Map<String, Object> properties = new HashMap<>();
84 properties.put("deviceid", remoteCode + remoteGroupID);
85 properties.put("basetopic", STATES_BASE_TOPIC + remoteCode + "/" + globeType + "/" + remoteGroupID);
86 thingDiscovered(DiscoveryResultBuilder
87 .create(new ThingUID(new ThingTypeUID(BINDING_ID, globeType), connectionBridge,
88 remoteCode + remoteGroupID))
89 .withProperties(properties).withRepresentationProperty("deviceid").withBridge(connectionBridge)
90 .withLabel("Milight " + globeType).build());
94 public void topicVanished(ThingUID connectionBridge, MqttBrokerConnection connection, String topic) {