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.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.NeeoBrainConfig;
23 import org.openhab.binding.neeo.internal.NeeoConstants;
24 import org.openhab.binding.neeo.internal.handler.NeeoBrainHandler;
25 import org.openhab.binding.neeo.internal.models.NeeoBrain;
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 rooms in a NEEO brain;
39 * @author Tim Roberts - Initial contribution
42 public class NeeoRoomDiscoveryService extends AbstractDiscoveryService {
44 private final Logger logger = LoggerFactory.getLogger(NeeoRoomDiscoveryService.class);
46 /** The room bridge type we support */
47 private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Collections
48 .singleton(NeeoConstants.BRIDGE_TYPE_ROOM);
50 /** The timeout (in seconds) for searching the brain */
51 private static final int SEARCH_TIME = 10;
53 /** The brain handler that we will use */
54 private final NeeoBrainHandler brainHandler;
57 * Constructs the discover service from the brain handler
59 * @param brainHandler a non-null brain handler
61 public NeeoRoomDiscoveryService(NeeoBrainHandler brainHandler) {
62 super(DISCOVERABLE_THING_TYPES_UIDS, SEARCH_TIME);
63 Objects.requireNonNull(brainHandler, "brainHandler cannot be null");
64 this.brainHandler = brainHandler;
68 protected void startScan() {
69 final String brainId = brainHandler.getNeeoBrainId();
71 final Bridge brainBridge = brainHandler.getThing();
72 final ThingUID brainUid = brainBridge.getUID();
74 final NeeoBrainApi api = brainHandler.getNeeoBrainApi();
76 logger.debug("Brain API was not available for {} - skipping", brainId);
81 final NeeoBrain brain = api.getBrain();
82 final NeeoBrainConfig config = brainBridge.getConfiguration().as(NeeoBrainConfig.class);
83 final NeeoRoom[] rooms = brain.getRooms().getRooms();
85 if (rooms.length == 0) {
86 logger.debug("Brain {} ({}) found - but there were no rooms - skipping", brain.getName(), brainId);
90 logger.debug("Brain {} ({}) found, scanning {} rooms in it", brain.getName(), brainId, rooms.length);
91 for (NeeoRoom room : rooms) {
92 final String roomKey = room.getKey();
93 if (roomKey == null || roomKey.isEmpty()) {
94 logger.debug("Room didn't have a room key: {}", room);
98 if (room.getDevices().getDevices().length == 0 && room.getRecipes().getRecipes().length == 0
99 && !config.isDiscoverEmptyRooms()) {
100 logger.debug("Room {} ({}) found but has no devices or recipes, ignoring - {}", roomKey, brainId,
105 final ThingUID thingUID = new ThingUID(NeeoConstants.BRIDGE_TYPE_ROOM, brainUid, roomKey);
107 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
108 .withProperty(NeeoConstants.CONFIG_ROOMKEY, roomKey)
109 .withProperty(NeeoConstants.CONFIG_EXCLUDE_THINGS, true).withBridge(brainUid)
110 .withLabel(room.getName() + " (NEEO " + brainId + ")").build();
111 thingDiscovered(discoveryResult);
113 } catch (IOException e) {
114 logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);