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.tapocontrol.internal.api;
15 import static org.openhab.binding.tapocontrol.internal.constants.TapoThingConstants.*;
17 import java.util.Dictionary;
20 import javax.jmdns.ServiceInfo;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryService;
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.ComponentContext;
30 import org.osgi.service.component.annotations.Activate;
31 import org.osgi.service.component.annotations.Component;
32 import org.osgi.service.component.annotations.Modified;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * Handler class for TAPO Smart Home thing discovery over mDNS
39 * @author Christian Wild - Initial contribution
41 @Component(configurationPid = "discovery.tapocontrol")
43 public class TapoMDNS implements MDNSDiscoveryParticipant {
44 private final Logger logger = LoggerFactory.getLogger(TapoMDNS.class);
45 private boolean isAutoDiscoveryEnabled = true;
48 protected void activate(ComponentContext componentContext) {
49 activateOrModifyService(componentContext);
53 protected void modified(ComponentContext componentContext) {
54 activateOrModifyService(componentContext);
57 private void activateOrModifyService(ComponentContext componentContext) {
58 Dictionary<String, @Nullable Object> properties = componentContext.getProperties();
59 String autoDiscoveryPropertyValue = (String) properties
60 .get(DiscoveryService.CONFIG_PROPERTY_BACKGROUND_DISCOVERY);
61 if (autoDiscoveryPropertyValue != null && !autoDiscoveryPropertyValue.isBlank()) {
62 isAutoDiscoveryEnabled = Boolean.valueOf(autoDiscoveryPropertyValue);
67 public Set<ThingTypeUID> getSupportedThingTypeUIDs() {
68 return SUPPORTED_THING_TYPES_UIDS;
72 public String getServiceType() {
77 public @Nullable ThingUID getThingUID(ServiceInfo service) {
78 ThingTypeUID thingTypeUID = getThingType(service);
79 if (thingTypeUID != null) {
80 String id = service.getPropertyString(PROPERTY_FAMILY); // device id
81 return new ThingUID(thingTypeUID, id);
86 private @Nullable ThingTypeUID getThingType(final ServiceInfo service) {
87 String model = service.getPropertyString(PROPERTY_FAMILY); // model
88 logger.debug("found Type: {}", model);
92 return L510_THING_TYPE;
96 public @Nullable DiscoveryResult createResult(ServiceInfo service) {
97 if (isAutoDiscoveryEnabled) {
98 ThingUID uid = getThingUID(service);
100 String host = service.getHostAddresses()[0];
101 int port = service.getPort();
102 logger.debug("device Found: {} {}", host, port);