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.harmonyhub.internal.discovery;
15 import static org.openhab.binding.harmonyhub.internal.HarmonyHubBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler;
20 import org.openhab.binding.harmonyhub.internal.handler.HubStatusListener;
21 import org.openhab.core.config.discovery.AbstractDiscoveryService;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingUID;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.digitaldan.harmony.config.Device;
29 import com.digitaldan.harmony.config.HarmonyConfig;
32 * The {@link HarmonyDeviceDiscoveryService} class discovers Harmony Devices connected to a Harmony Hub
34 * @author Dan Cunningham - Initial contribution
35 * @author Wouter Born - Add null annotations
38 public class HarmonyDeviceDiscoveryService extends AbstractDiscoveryService implements HubStatusListener {
40 private static final int TIMEOUT = 5;
42 private final Logger logger = LoggerFactory.getLogger(HarmonyDeviceDiscoveryService.class);
43 private final HarmonyHubHandler bridge;
45 public HarmonyDeviceDiscoveryService(HarmonyHubHandler bridge) {
46 super(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
47 logger.debug("HarmonyDeviceDiscoveryService {}", bridge);
49 this.bridge.addHubStatusListener(this);
53 protected void startScan() {
58 protected void startBackgroundDiscovery() {
63 public void hubStatusChanged(ThingStatus status) {
64 if (status.equals(ThingStatus.ONLINE)) {
70 protected void deactivate() {
72 bridge.removeHubStatusListener(this);
76 * Discovers devices connected to a hub
78 private void discoverDevices() {
79 if (bridge.getThing().getStatus() != ThingStatus.ONLINE) {
80 logger.debug("Harmony Hub not online, scanning postponed");
83 logger.debug("getting devices on {}", bridge.getThing().getUID().getId());
84 bridge.getConfigFuture().thenAccept(this::addDiscoveryResults).exceptionally(e -> {
85 logger.debug("Could not get harmony config for discovery, skipping");
90 private void addDiscoveryResults(@Nullable HarmonyConfig config) {
92 logger.debug("addDiscoveryResults: skipping null config");
96 for (Device device : config.getDevices()) {
97 String label = device.getLabel();
98 int id = device.getId();
100 ThingUID bridgeUID = bridge.getThing().getUID();
101 ThingUID thingUID = new ThingUID(HARMONY_DEVICE_THING_TYPE, bridgeUID, String.valueOf(id));
104 thingDiscovered(DiscoveryResultBuilder.create(thingUID)
106 .withBridge(bridgeUID)
107 .withProperty(DEVICE_PROPERTY_ID, id)
108 .withProperty(DEVICE_PROPERTY_NAME, label)