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.neeo.internal.discovery;
15 import java.io.IOException;
16 import java.util.Objects;
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;
37 * Implementation of {@link AbstractDiscoveryService} that will discover the devices in a NEEO room;
39 * @author Tim Roberts - Initial contribution
42 public class NeeoDeviceDiscoveryService extends AbstractDiscoveryService {
45 private final Logger logger = LoggerFactory.getLogger(NeeoDeviceDiscoveryService.class);
47 /** The device thing type we support */
48 private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Set.of(NeeoConstants.THING_TYPE_DEVICE);
50 /** The timeout (in seconds) for searching the room */
51 private static final int SEARCH_TIME = 10;
53 /** The room handler to search */
54 private final NeeoRoomHandler roomHandler;
57 * Constructs the discovery service from the room handler
59 * @param roomHandler a non-null room handler
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;
68 protected void startScan() {
69 final Bridge roomBridge = roomHandler.getThing();
70 final ThingUID roomUid = roomBridge.getUID();
72 final String brainId = roomHandler.getNeeoBrainId();
73 if (brainId == null || brainId.isEmpty()) {
74 logger.debug("Unknown brain ID for roomHandler: {}", roomHandler);
78 final NeeoBrainApi api = roomHandler.getNeeoBrainApi();
80 logger.debug("Brain API was not available for {} - skipping", brainId);
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);
92 final NeeoRoom room = api.getRoom(roomKey);
93 final NeeoDevice[] devices = room.getDevices().getDevices();
95 if (devices.length == 0) {
96 logger.debug("Room {} found - but there were no devices - skipping", room.getName());
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);
108 if (config.isExcludeThings() && UidUtils.isThing(device)) {
109 logger.debug("Found openHAB thing but ignoring per configuration: {}", device);
113 logger.debug("Device #{} found - {}", deviceKey, device.getName());
115 final ThingUID thingUID = new ThingUID(NeeoConstants.THING_TYPE_DEVICE, roomUid, deviceKey);
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);
122 } catch (IOException e) {
123 logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);