]> git.basschouten.com Git - openhab-addons.git/blob
4d108e79eaa8ecad5509f9312bb4940f1ecca2cb
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.neeo.internal.discovery;
14
15 import java.io.IOException;
16 import java.util.Collections;
17 import java.util.Objects;
18 import java.util.Set;
19
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;
35
36 /**
37  * Implementation of {@link AbstractDiscoveryService} that will discover the rooms in a NEEO brain;
38  *
39  * @author Tim Roberts - Initial contribution
40  */
41 @NonNullByDefault
42 public class NeeoRoomDiscoveryService extends AbstractDiscoveryService {
43     /** The logger */
44     private final Logger logger = LoggerFactory.getLogger(NeeoRoomDiscoveryService.class);
45
46     /** The room bridge type we support */
47     private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Collections
48             .singleton(NeeoConstants.BRIDGE_TYPE_ROOM);
49
50     /** The timeout (in seconds) for searching the brain */
51     private static final int SEARCH_TIME = 10;
52
53     /** The brain handler that we will use */
54     private final NeeoBrainHandler brainHandler;
55
56     /**
57      * Constructs the discover service from the brain handler
58      *
59      * @param brainHandler a non-null brain handler
60      */
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;
65     }
66
67     @Override
68     protected void startScan() {
69         final String brainId = brainHandler.getNeeoBrainId();
70
71         final Bridge brainBridge = brainHandler.getThing();
72         final ThingUID brainUid = brainBridge.getUID();
73
74         final NeeoBrainApi api = brainHandler.getNeeoBrainApi();
75         if (api == null) {
76             logger.debug("Brain API was not available for {} - skipping", brainId);
77             return;
78         }
79
80         try {
81             final NeeoBrain brain = api.getBrain();
82             final NeeoBrainConfig config = brainBridge.getConfiguration().as(NeeoBrainConfig.class);
83             final NeeoRoom[] rooms = brain.getRooms().getRooms();
84
85             if (rooms.length == 0) {
86                 logger.debug("Brain {} ({}) found - but there were no rooms - skipping", brain.getName(), brainId);
87                 return;
88             }
89
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);
95                     continue;
96                 }
97
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,
101                             room.getName());
102                     continue;
103                 }
104
105                 final ThingUID thingUID = new ThingUID(NeeoConstants.BRIDGE_TYPE_ROOM, brainUid, roomKey);
106
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);
112             }
113         } catch (IOException e) {
114             logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);
115         }
116     }
117 }