2 * Copyright (c) 2010-2021 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.avmfritz.internal.discovery;
15 import static org.openhab.binding.avmfritz.internal.AVMFritzBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_VENDOR;
18 import java.util.Dictionary;
19 import java.util.HashMap;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.jupnp.model.meta.DeviceDetails;
26 import org.jupnp.model.meta.ModelDetails;
27 import org.jupnp.model.meta.RemoteDevice;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.config.discovery.upnp.UpnpDiscoveryParticipant;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.osgi.service.component.ComponentContext;
34 import org.osgi.service.component.annotations.Activate;
35 import org.osgi.service.component.annotations.Component;
36 import org.osgi.service.component.annotations.Modified;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link AVMFritzUpnpDiscoveryParticipant} is responsible for discovering new and removed FRITZ!Box devices. It
42 * uses the central {@link UpnpDiscoveryService}.
44 * @author Robert Bausdorf - Initial contribution
45 * @author Christoph Weitkamp - Added support for groups
46 * @author Christoph Weitkamp - Use "discovery.avmfritz:background=false" to disable discovery service
48 @Component(configurationPid = "discovery.avmfritz")
50 public class AVMFritzUpnpDiscoveryParticipant implements UpnpDiscoveryParticipant {
52 private final Logger logger = LoggerFactory.getLogger(AVMFritzUpnpDiscoveryParticipant.class);
54 private boolean isAutoDiscoveryEnabled = true;
57 protected void activate(ComponentContext componentContext) {
58 activateOrModifyService(componentContext);
62 protected void modified(ComponentContext componentContext) {
63 activateOrModifyService(componentContext);
66 private void activateOrModifyService(ComponentContext componentContext) {
67 Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
68 String autoDiscoveryPropertyValue = (String) properties.get("background");
69 if (autoDiscoveryPropertyValue != null && autoDiscoveryPropertyValue.length() != 0) {
70 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
75 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
76 return SUPPORTED_BRIDGE_THING_TYPES_UIDS;
80 public @Nullable DiscoveryResult createResult(RemoteDevice device) {
81 if (isAutoDiscoveryEnabled) {
82 ThingUID uid = getThingUID(device);
84 logger.debug("discovered: {} ({}) at {}", device.getDisplayString(),
85 device.getDetails().getFriendlyName(), device.getIdentity().getDescriptorURL().getHost());
87 Map<String, Object> properties = new HashMap<>();
88 properties.put(CONFIG_IP_ADDRESS, device.getIdentity().getDescriptorURL().getHost());
89 properties.put(PROPERTY_VENDOR, device.getDetails().getManufacturerDetails().getManufacturer());
91 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
92 .withLabel(device.getDetails().getFriendlyName()).withRepresentationProperty(CONFIG_IP_ADDRESS)
102 public @Nullable ThingUID getThingUID(RemoteDevice device) {
103 // newer FRITZ!OS versions return several upnp services (e.g. Mediaserver)
104 if (device.getType().getType().equals(BRIDGE_FRITZBOX)) {
105 DeviceDetails details = device.getDetails();
106 if (details != null) {
107 ModelDetails modelDetails = details.getModelDetails();
108 if (modelDetails != null) {
109 String modelName = modelDetails.getModelName();
110 if (modelName != null) {
111 // It would be better to use udn but in my case FB is discovered twice
112 // .getIdentity().getUdn().getIdentifierString()
113 String id = device.getIdentity().getDescriptorURL().getHost().replaceAll(INVALID_PATTERN, "_");
114 if (modelName.startsWith(BOX_MODEL_NAME)) {
115 logger.debug("discovered on {}", device.getIdentity().getDiscoveredOnLocalAddress());
116 return new ThingUID(BRIDGE_THING_TYPE, id);
117 } else if (modelName.startsWith(POWERLINE_MODEL_NAME)) {
118 logger.debug("discovered on {}", device.getIdentity().getDiscoveredOnLocalAddress());
119 return new ThingUID(PL546E_STANDALONE_THING_TYPE, id);