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