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