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.NeeoBrainConfig;
22 import org.openhab.binding.neeo.internal.NeeoConstants;
23 import org.openhab.binding.neeo.internal.handler.NeeoBrainHandler;
24 import org.openhab.binding.neeo.internal.models.NeeoBrain;
25 import org.openhab.binding.neeo.internal.models.NeeoRoom;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * Implementation of {@link AbstractDiscoveryService} that will discover the rooms in a NEEO brain;
38 * @author Tim Roberts - Initial contribution
41 public class NeeoRoomDiscoveryService extends AbstractDiscoveryService {
43 private final Logger logger = LoggerFactory.getLogger(NeeoRoomDiscoveryService.class);
45 /** The room bridge type we support */
46 private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Set.of(NeeoConstants.BRIDGE_TYPE_ROOM);
48 /** The timeout (in seconds) for searching the brain */
49 private static final int SEARCH_TIME = 10;
51 /** The brain handler that we will use */
52 private final NeeoBrainHandler brainHandler;
55 * Constructs the discover service from the brain handler
57 * @param brainHandler a non-null brain handler
59 public NeeoRoomDiscoveryService(NeeoBrainHandler brainHandler) {
60 super(DISCOVERABLE_THING_TYPES_UIDS, SEARCH_TIME);
61 Objects.requireNonNull(brainHandler, "brainHandler cannot be null");
62 this.brainHandler = brainHandler;
66 protected void startScan() {
67 final String brainId = brainHandler.getNeeoBrainId();
69 final Bridge brainBridge = brainHandler.getThing();
70 final ThingUID brainUid = brainBridge.getUID();
72 final NeeoBrainApi api = brainHandler.getNeeoBrainApi();
74 logger.debug("Brain API was not available for {} - skipping", brainId);
79 final NeeoBrain brain = api.getBrain();
80 final NeeoBrainConfig config = brainBridge.getConfiguration().as(NeeoBrainConfig.class);
81 final NeeoRoom[] rooms = brain.getRooms().getRooms();
83 if (rooms.length == 0) {
84 logger.debug("Brain {} ({}) found - but there were no rooms - skipping", brain.getName(), brainId);
88 logger.debug("Brain {} ({}) found, scanning {} rooms in it", brain.getName(), brainId, rooms.length);
89 for (NeeoRoom room : rooms) {
90 final String roomKey = room.getKey();
91 if (roomKey == null || roomKey.isEmpty()) {
92 logger.debug("Room didn't have a room key: {}", room);
96 if (room.getDevices().getDevices().length == 0 && room.getRecipes().getRecipes().length == 0
97 && !config.isDiscoverEmptyRooms()) {
98 logger.debug("Room {} ({}) found but has no devices or recipes, ignoring - {}", roomKey, brainId,
103 final ThingUID thingUID = new ThingUID(NeeoConstants.BRIDGE_TYPE_ROOM, brainUid, roomKey);
105 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
106 .withProperty(NeeoConstants.CONFIG_ROOMKEY, roomKey)
107 .withProperty(NeeoConstants.CONFIG_EXCLUDE_THINGS, true).withBridge(brainUid)
108 .withLabel(room.getName() + " (NEEO " + brainId + ")").build();
109 thingDiscovered(discoveryResult);
111 } catch (IOException e) {
112 logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);