]> git.basschouten.com Git - openhab-addons.git/blob
fbc60526a9ab81928655a307dd7e513c9dfce3e5
[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.neeo.internal.discovery;
14
15 import java.io.IOException;
16 import java.util.Objects;
17 import java.util.Set;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.neeo.internal.NeeoBrainApi;
21 import org.openhab.binding.neeo.internal.NeeoConstants;
22 import org.openhab.binding.neeo.internal.NeeoRoomConfig;
23 import org.openhab.binding.neeo.internal.UidUtils;
24 import org.openhab.binding.neeo.internal.handler.NeeoRoomHandler;
25 import org.openhab.binding.neeo.internal.models.NeeoDevice;
26 import org.openhab.binding.neeo.internal.models.NeeoRoom;
27 import org.openhab.core.config.discovery.AbstractDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.thing.ThingUID;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 /**
37  * Implementation of {@link AbstractDiscoveryService} that will discover the devices in a NEEO room;
38  *
39  * @author Tim Roberts - Initial contribution
40  */
41 @NonNullByDefault
42 public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
43
44     /** The logger */
45     private final Logger logger = LoggerFactory.getLogger(NeeoDeviceDiscoveryService.class);
46
47     /** The device thing type we support */
48     private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Set.of(NeeoConstants.THING_TYPE_DEVICE);
49
50     /** The timeout (in seconds) for searching the room */
51     private static final int SEARCH_TIME = 10;
52
53     /** The room handler to search */
54     private final NeeoRoomHandler roomHandler;
55
56     /**
57      * Constructs the discovery service from the room handler
58      *
59      * @param roomHandler a non-null room handler
60      */
61     public NeeoDeviceDiscoveryService(NeeoRoomHandler roomHandler) {
62         super(DISCOVERABLE_THING_TYPES_UIDS, SEARCH_TIME);
63         Objects.requireNonNull(roomHandler, "roomHandler cannot be null");
64         this.roomHandler = roomHandler;
65     }
66
67     @Override
68     protected void startScan() {
69         final Bridge roomBridge = roomHandler.getThing();
70         final ThingUID roomUid = roomBridge.getUID();
71
72         final String brainId = roomHandler.getNeeoBrainId();
73         if (brainId == null || brainId.isEmpty()) {
74             logger.debug("Unknown brain ID for roomHandler: {}", roomHandler);
75             return;
76         }
77
78         final NeeoBrainApi api = roomHandler.getNeeoBrainApi();
79         if (api == null) {
80             logger.debug("Brain API was not available for {} - skipping", brainId);
81             return;
82         }
83
84         final NeeoRoomConfig config = roomBridge.getConfiguration().as(NeeoRoomConfig.class);
85         final String roomKey = config.getRoomKey();
86         if (roomKey == null || roomKey.isEmpty()) {
87             logger.debug("RoomKey wasn't configured for {} - skipping", brainId);
88             return;
89         }
90
91         try {
92             final NeeoRoom room = api.getRoom(roomKey);
93             final NeeoDevice[] devices = room.getDevices().getDevices();
94
95             if (devices.length == 0) {
96                 logger.debug("Room {} found - but there were no devices - skipping", room.getName());
97                 return;
98             }
99
100             logger.debug("Room {} found, scanning {} devices in it", room.getName(), devices.length);
101             for (NeeoDevice device : devices) {
102                 final String deviceKey = device.getKey();
103                 if (deviceKey == null || deviceKey.isEmpty()) {
104                     logger.debug("Device key wasn't found for device: {}", device);
105                     continue;
106                 }
107
108                 if (config.isExcludeThings() && UidUtils.isThing(device)) {
109                     logger.debug("Found openHAB thing but ignoring per configuration: {}", device);
110                     continue;
111                 }
112
113                 logger.debug("Device #{} found - {}", deviceKey, device.getName());
114
115                 final ThingUID thingUID = new ThingUID(NeeoConstants.THING_TYPE_DEVICE, roomUid, deviceKey);
116
117                 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
118                         .withProperty(NeeoConstants.CONFIG_DEVICEKEY, deviceKey).withBridge(roomUid)
119                         .withLabel(device.getName() + " (NEEO " + brainId + ")").build();
120                 thingDiscovered(discoveryResult);
121             }
122         } catch (IOException e) {
123             logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);
124         }
125     }
126 }