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.hue.internal.discovery;
15 import static org.openhab.binding.hue.internal.HueBindingConstants.*;
17 import java.util.Dictionary;
21 import javax.jmdns.ServiceInfo;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.hue.internal.handler.HueBridgeHandler;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.config.discovery.mdns.MDNSDiscoveryParticipant;
30 import org.openhab.core.config.discovery.mdns.internal.MDNSDiscoveryService;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.osgi.service.component.ComponentContext;
35 import org.osgi.service.component.annotations.Activate;
36 import org.osgi.service.component.annotations.Component;
37 import org.osgi.service.component.annotations.Modified;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link HueBridgeMDNSDiscoveryParticipant} is responsible for discovering new and removed Hue Bridges. It uses the
43 * central {@link MDNSDiscoveryService}.
45 * @author Kai Kreuzer - Initial contribution
46 * @author Thomas Höfer - Added representation
47 * @author Christoph Weitkamp - Change discovery protocol to mDNS
49 @Component(configurationPid = "discovery.hue")
51 public class HueBridgeMDNSDiscoveryParticipant implements MDNSDiscoveryParticipant {
53 private static final String SERVICE_TYPE = "_hue._tcp.local.";
54 private static final String MDNS_PROPERTY_BRIDGE_ID = "bridgeid";
55 private static final String MDNS_PROPERTY_MODEL_ID = "modelid";
57 private final Logger logger = LoggerFactory.getLogger(HueBridgeMDNSDiscoveryParticipant.class);
59 private long removalGracePeriod = 0L;
61 private boolean isAutoDiscoveryEnabled = true;
64 protected void activate(ComponentContext componentContext) {
65 activateOrModifyService(componentContext);
69 protected void modified(ComponentContext componentContext) {
70 activateOrModifyService(componentContext);
73 private void activateOrModifyService(ComponentContext componentContext) {
74 Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
75 String autoDiscoveryPropertyValue = (String) properties
76 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
77 if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
78 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
80 String removalGracePeriodPropertyValue = (String) properties.get(REMOVAL_GRACE_PERIOD);
81 if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
83 removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
84 } catch (NumberFormatException e) {
85 logger.warn("Configuration property '{}' has invalid value: {}", REMOVAL_GRACE_PERIOD,
86 removalGracePeriodPropertyValue);
92 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
93 return HueBridgeHandler.SUPPORTED_THING_TYPES;
97 public String getServiceType() {
102 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
103 if (isAutoDiscoveryEnabled) {
104 ThingUID uid = getThingUID(service);
106 String host = service.getHostAddresses()[0];
107 String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
108 String friendlyName = String.format(DISCOVERY_LABEL_PATTERN, host);
109 return DiscoveryResultBuilder.create(uid) //
110 .withProperties(Map.of( //
112 Thing.PROPERTY_MODEL_ID, service.getPropertyString(MDNS_PROPERTY_MODEL_ID), //
113 Thing.PROPERTY_SERIAL_NUMBER, id.toLowerCase())) //
114 .withLabel(friendlyName) //
115 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER) //
124 public @Nullable ThingUID getThingUID(ServiceInfo service) {
125 String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
126 if (id != null && !id.isBlank()) {
127 return new ThingUID(THING_TYPE_BRIDGE, id.toLowerCase());
133 public long getRemovalGracePeriodSeconds(ServiceInfo service) {
134 return removalGracePeriod;