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