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.shelly.internal.discovery;
15 import static org.openhab.binding.shelly.internal.ShellyBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_MAC_ADDRESS;
18 import java.util.Hashtable;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.shelly.internal.handler.ShellyThingTable;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.config.discovery.DiscoveryService;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.ServiceRegistration;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Device discovery creates a thing in the inbox for each vehicle
37 * found in the data received from {@link ShellyBluDiscoveryService}.
39 * @author Markus Michels - Initial Contribution
43 public class ShellyBluDiscoveryService extends AbstractDiscoveryService {
44 private final Logger logger = LoggerFactory.getLogger(ShellyBluDiscoveryService.class);
46 private final BundleContext bundleContext;
47 private final ShellyThingTable thingTable;
48 private static final int TIMEOUT = 10;
49 private @Nullable ServiceRegistration<?> discoveryService;
51 public ShellyBluDiscoveryService(BundleContext bundleContext, ShellyThingTable thingTable) {
52 super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT);
53 this.bundleContext = bundleContext;
54 this.thingTable = thingTable;
57 @SuppressWarnings("null")
58 public void registerDeviceDiscoveryService() {
59 if (discoveryService == null) {
60 discoveryService = bundleContext.registerService(DiscoveryService.class.getName(), this, new Hashtable<>());
65 protected void startScan() {
66 logger.debug("Starting BLU Discovery");
67 thingTable.startScan();
70 public void discoveredResult(ThingTypeUID tuid, String model, String serviceName, String address,
71 Map<String, Object> properties) {
72 ThingUID uid = ShellyThingCreator.getThingUID(serviceName, model, "", true);
73 logger.debug("Adding discovered thing with id {}", uid.toString());
74 properties.put(PROPERTY_MAC_ADDRESS, address);
75 String thingLabel = "Shelly BLU " + model + " (" + serviceName + ")";
76 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
77 .withRepresentationProperty(PROPERTY_DEV_NAME).withLabel(thingLabel).build();
78 thingDiscovered(result);
81 public void unregisterDeviceDiscoveryService() {
82 if (discoveryService != null) {
83 discoveryService.unregister();
88 public void deactivate() {
90 unregisterDeviceDiscoveryService();