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 static final String CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD = "removalGracePeriod";
59 private final Logger logger = LoggerFactory.getLogger(HueBridgeMDNSDiscoveryParticipant.class);
61 private long removalGracePeriod = 0L;
63 private boolean isAutoDiscoveryEnabled = true;
66 protected void activate(ComponentContext componentContext) {
67 activateOrModifyService(componentContext);
71 protected void modified(ComponentContext componentContext) {
72 activateOrModifyService(componentContext);
75 private void activateOrModifyService(ComponentContext componentContext) {
76 Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
77 String autoDiscoveryPropertyValue = (String) properties
78 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
79 if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
80 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
82 String removalGracePeriodPropertyValue = (String) properties.get(CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD);
83 if (removalGracePeriodPropertyValue != null && !removalGracePeriodPropertyValue.isBlank()) {
85 removalGracePeriod = Long.parseLong(removalGracePeriodPropertyValue);
86 } catch (NumberFormatException e) {
87 logger.warn("Configuration property '{}' has invalid value: {}", CONFIG_PROPERTY_REMOVAL_GRACE_PERIOD,
88 removalGracePeriodPropertyValue);
94 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
95 return HueBridgeHandler.SUPPORTED_THING_TYPES;
99 public String getServiceType() {
104 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
105 if (isAutoDiscoveryEnabled) {
106 ThingUID uid = getThingUID(service);
108 String host = service.getHostAddresses()[0];
109 String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
110 String friendlyName = String.format(DISCOVERY_LABEL_PATTERN, host);
111 return DiscoveryResultBuilder.create(uid) //
112 .withProperties(Map.of( //
114 Thing.PROPERTY_MODEL_ID, service.getPropertyString(MDNS_PROPERTY_MODEL_ID), //
115 Thing.PROPERTY_SERIAL_NUMBER, id.toLowerCase())) //
116 .withLabel(friendlyName) //
117 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER) //
126 public @Nullable ThingUID getThingUID(ServiceInfo service) {
127 String id = service.getPropertyString(MDNS_PROPERTY_BRIDGE_ID);
128 if (id != null && !id.isBlank()) {
129 return new ThingUID(THING_TYPE_BRIDGE, id.toLowerCase());
135 public long getRemovalGracePeriodSeconds(ServiceInfo service) {
136 return removalGracePeriod;