2 * Copyright (c) 2010-2020 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.apache.commons.lang.StringUtils;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.neeo.internal.NeeoBrainApi;
23 import org.openhab.binding.neeo.internal.NeeoConstants;
24 import org.openhab.binding.neeo.internal.NeeoRoomConfig;
25 import org.openhab.binding.neeo.internal.UidUtils;
26 import org.openhab.binding.neeo.internal.handler.NeeoRoomHandler;
27 import org.openhab.binding.neeo.internal.models.NeeoDevice;
28 import org.openhab.binding.neeo.internal.models.NeeoRoom;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.thing.Bridge;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * Implementation of {@link AbstractDiscoveryService} that will discover the devices in a NEEO room;
41 * @author Tim Roberts - Initial contribution
44 public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
47 private final Logger logger = LoggerFactory.getLogger(NeeoDeviceDiscoveryService.class);
49 /** The device thing type we support */
50 private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Collections
51 .singleton(NeeoConstants.THING_TYPE_DEVICE);
53 /** The timeout (in seconds) for searching the room */
54 private static final int SEARCH_TIME = 10;
56 /** The room handler to search */
57 private final NeeoRoomHandler roomHandler;
60 * Constructs the discovery service from the room handler
62 * @param roomHandler a non-null room handler
64 public NeeoDeviceDiscoveryService(NeeoRoomHandler roomHandler) {
65 super(DISCOVERABLE_THING_TYPES_UIDS, SEARCH_TIME);
66 Objects.requireNonNull(roomHandler, "roomHandler cannot be null");
67 this.roomHandler = roomHandler;
71 protected void startScan() {
72 final Bridge roomBridge = roomHandler.getThing();
73 final ThingUID roomUid = roomBridge.getUID();
75 final String brainId = roomHandler.getNeeoBrainId();
76 if (brainId == null || StringUtils.isEmpty(brainId)) {
77 logger.debug("Unknown brain ID for roomHandler: {}", roomHandler);
81 final NeeoBrainApi api = roomHandler.getNeeoBrainApi();
83 logger.debug("Brain API was not available for {} - skipping", brainId);
87 final NeeoRoomConfig config = roomBridge.getConfiguration().as(NeeoRoomConfig.class);
88 final String roomKey = config.getRoomKey();
89 if (roomKey == null || StringUtils.isEmpty(roomKey)) {
90 logger.debug("RoomKey wasn't configured for {} - skipping", brainId);
95 final NeeoRoom room = api.getRoom(roomKey);
96 final NeeoDevice[] devices = room.getDevices().getDevices();
98 if (devices.length == 0) {
99 logger.debug("Room {} found - but there were no devices - skipping", room.getName());
103 logger.debug("Room {} found, scanning {} devices in it", room.getName(), devices.length);
104 for (NeeoDevice device : devices) {
105 final String deviceKey = device.getKey();
106 if (deviceKey == null || StringUtils.isEmpty(deviceKey)) {
107 logger.debug("Device key wasn't found for device: {}", device);
111 if (config.isExcludeThings() && UidUtils.isThing(device)) {
112 logger.debug("Found openHAB thing but ignoring per configuration: {}", device);
116 logger.debug("Device #{} found - {}", deviceKey, device.getName());
118 final ThingUID thingUID = new ThingUID(NeeoConstants.THING_TYPE_DEVICE, roomUid, deviceKey);
120 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
121 .withProperty(NeeoConstants.CONFIG_DEVICEKEY, deviceKey).withBridge(roomUid)
122 .withLabel(device.getName() + " (NEEO " + brainId + ")").build();
123 thingDiscovered(discoveryResult);
125 } catch (IOException e) {
126 logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);