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.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,
61 new Hashtable<String, Object>());
66 protected void startScan() {
67 logger.debug("Starting BLU Discovery");
68 thingTable.startScan();
71 public void discoveredResult(ThingTypeUID tuid, String model, String serviceName, String address,
72 Map<String, Object> properties) {
73 ThingUID uid = ShellyThingCreator.getThingUID(serviceName, model, "", true);
74 logger.debug("Adding discovered thing with id {}", uid.toString());
75 properties.put(PROPERTY_MAC_ADDRESS, address);
76 String thingLabel = "Shelly BLU " + model + " (" + serviceName + ")";
77 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withProperties(properties)
78 .withRepresentationProperty(PROPERTY_DEV_NAME).withLabel(thingLabel).build();
79 thingDiscovered(result);
82 public void unregisterDeviceDiscoveryService() {
83 if (discoveryService != null) {
84 discoveryService.unregister();
89 public void deactivate() {
91 unregisterDeviceDiscoveryService();