]> git.basschouten.com Git - openhab-addons.git/blob
3ccdadddd506df1771a84ecbd32fa4fbe7e2ddd2
[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.Objects;
17 import java.util.Set;
18
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;
34
35 /**
36  * Implementation of {@link AbstractDiscoveryService} that will discover the rooms in a NEEO brain;
37  *
38  * @author Tim Roberts - Initial contribution
39  */
40 @NonNullByDefault
41 public class NeeoRoomDiscoveryService extends AbstractDiscoveryService {
42     /** The logger */
43     private final Logger logger = LoggerFactory.getLogger(NeeoRoomDiscoveryService.class);
44
45     /** The room bridge type we support */
46     private static final Set<ThingTypeUID> DISCOVERABLE_THING_TYPES_UIDS = Set.of(NeeoConstants.BRIDGE_TYPE_ROOM);
47
48     /** The timeout (in seconds) for searching the brain */
49     private static final int SEARCH_TIME = 10;
50
51     /** The brain handler that we will use */
52     private final NeeoBrainHandler brainHandler;
53
54     /**
55      * Constructs the discover service from the brain handler
56      *
57      * @param brainHandler a non-null brain handler
58      */
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;
63     }
64
65     @Override
66     protected void startScan() {
67         final String brainId = brainHandler.getNeeoBrainId();
68
69         final Bridge brainBridge = brainHandler.getThing();
70         final ThingUID brainUid = brainBridge.getUID();
71
72         final NeeoBrainApi api = brainHandler.getNeeoBrainApi();
73         if (api == null) {
74             logger.debug("Brain API was not available for {} - skipping", brainId);
75             return;
76         }
77
78         try {
79             final NeeoBrain brain = api.getBrain();
80             final NeeoBrainConfig config = brainBridge.getConfiguration().as(NeeoBrainConfig.class);
81             final NeeoRoom[] rooms = brain.getRooms().getRooms();
82
83             if (rooms.length == 0) {
84                 logger.debug("Brain {} ({}) found - but there were no rooms - skipping", brain.getName(), brainId);
85                 return;
86             }
87
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);
93                     continue;
94                 }
95
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,
99                             room.getName());
100                     continue;
101                 }
102
103                 final ThingUID thingUID = new ThingUID(NeeoConstants.BRIDGE_TYPE_ROOM, brainUid, roomKey);
104
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);
110             }
111         } catch (IOException e) {
112             logger.debug("IOException occurred getting brain info ({}): {}", brainId, e.getMessage(), e);
113         }
114     }
115 }