]> git.basschouten.com Git - openhab-addons.git/blob
5fde308d92b76a48ae48e323c719eb8b9fffcb92
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.harmonyhub.internal.discovery;
14
15 import static org.openhab.binding.harmonyhub.internal.HarmonyHubBindingConstants.*;
16
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;
27
28 import com.digitaldan.harmony.config.Device;
29 import com.digitaldan.harmony.config.HarmonyConfig;
30
31 /**
32  * The {@link HarmonyDeviceDiscoveryService} class discovers Harmony Devices connected to a Harmony Hub
33  *
34  * @author Dan Cunningham - Initial contribution
35  * @author Wouter Born - Add null annotations
36  */
37 @NonNullByDefault
38 public class HarmonyDeviceDiscoveryService extends AbstractDiscoveryService implements HubStatusListener {
39
40     private static final int TIMEOUT = 5;
41
42     private final Logger logger = LoggerFactory.getLogger(HarmonyDeviceDiscoveryService.class);
43     private final HarmonyHubHandler bridge;
44
45     public HarmonyDeviceDiscoveryService(HarmonyHubHandler bridge) {
46         super(HarmonyHubHandler.SUPPORTED_THING_TYPES_UIDS, TIMEOUT, true);
47         logger.debug("HarmonyDeviceDiscoveryService {}", bridge);
48         this.bridge = bridge;
49         this.bridge.addHubStatusListener(this);
50     }
51
52     @Override
53     protected void startScan() {
54         discoverDevices();
55     }
56
57     @Override
58     protected void startBackgroundDiscovery() {
59         discoverDevices();
60     }
61
62     @Override
63     public void hubStatusChanged(ThingStatus status) {
64         if (status.equals(ThingStatus.ONLINE)) {
65             discoverDevices();
66         }
67     }
68
69     @Override
70     protected void deactivate() {
71         super.deactivate();
72         bridge.removeHubStatusListener(this);
73     }
74
75     /**
76      * Discovers devices connected to a hub
77      */
78     private void discoverDevices() {
79         if (bridge.getThing().getStatus() != ThingStatus.ONLINE) {
80             logger.debug("Harmony Hub not online, scanning postponed");
81             return;
82         }
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");
86             return null;
87         });
88     }
89
90     private void addDiscoveryResults(@Nullable HarmonyConfig config) {
91         if (config == null) {
92             logger.debug("addDiscoveryResults: skipping null config");
93             return;
94         }
95
96         for (Device device : config.getDevices()) {
97             String label = device.getLabel();
98             int id = device.getId();
99
100             ThingUID bridgeUID = bridge.getThing().getUID();
101             ThingUID thingUID = new ThingUID(HARMONY_DEVICE_THING_TYPE, bridgeUID, String.valueOf(id));
102
103             // @formatter:off
104             thingDiscovered(DiscoveryResultBuilder.create(thingUID)
105                     .withLabel(label)
106                     .withBridge(bridgeUID)
107                     .withProperty(DEVICE_PROPERTY_ID, id)
108                     .withProperty(DEVICE_PROPERTY_NAME, label)
109                     .build());
110             // @formatter:on
111         }
112     }
113 }