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
13 package org.openhab.binding.hdpowerview.internal.discovery;
15 import static org.openhab.binding.hdpowerview.internal.HDPowerViewBindingConstants.*;
17 import java.util.Collections;
20 import javax.jmdns.ServiceInfo;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.hdpowerview.internal.config.HDPowerViewHubConfiguration;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.service.component.annotations.Component;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Discovers HD PowerView hubs by means of mDNS
37 * @author Andrew Fiddian-Green - Initial contribution
41 public class HDPowerViewHubDiscoveryParticipant implements MDNSDiscoveryParticipant {
43 private static final String LABEL_KEY = "discovery.hub.label";
45 private final Logger logger = LoggerFactory.getLogger(HDPowerViewHubDiscoveryParticipant.class);
48 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
49 return Collections.singleton(THING_TYPE_HUB);
53 public String getServiceType() {
54 return "_powerview._tcp.local.";
58 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
59 for (String host : service.getHostAddresses()) {
60 if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
61 ThingUID thingUID = new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));
62 DiscoveryResult hub = DiscoveryResultBuilder.create(thingUID)
63 .withProperty(HDPowerViewHubConfiguration.HOST, host)
64 .withRepresentationProperty(HDPowerViewHubConfiguration.HOST)
65 .withLabel(String.format("@text/%s [\"%s\"]", LABEL_KEY, host)).build();
66 logger.debug("mDNS discovered Gen 1/2 hub on host '{}'", host);
74 public @Nullable ThingUID getThingUID(ServiceInfo service) {
75 for (String host : service.getHostAddresses()) {
76 if (VALID_IP_V4_ADDRESS.matcher(host).matches()) {
77 return new ThingUID(THING_TYPE_HUB, host.replace('.', '_'));