]> git.basschouten.com Git - openhab-addons.git/blob
87a986619ea7c22f9fea8b04aa15da0112fbe547
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.insteon.internal.discovery;
14
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.insteon.internal.InsteonBindingConstants;
23 import org.openhab.binding.insteon.internal.handler.InsteonNetworkHandler;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.thing.ThingUID;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link InsteonDeviceDiscoveryService} is responsible for device discovery.
32  *
33  * @author Rob Nielsen - Initial contribution
34  */
35 @NonNullByDefault
36 public class InsteonDeviceDiscoveryService extends AbstractDiscoveryService {
37     private static final String ADDRESS = "address";
38
39     private final Logger logger = LoggerFactory.getLogger(InsteonDeviceDiscoveryService.class);
40
41     public InsteonDeviceDiscoveryService(InsteonNetworkHandler handler) {
42         super(new HashSet<>(Arrays.asList(InsteonBindingConstants.DEVICE_THING_TYPE)), 0, false);
43
44         handler.setInsteonDeviceDiscoveryService(this);
45
46         logger.debug("Initializing InsteonNetworkDiscoveryService");
47     }
48
49     @Override
50     protected void startScan() {
51     }
52
53     public void addInsteonDevices(List<String> addresses, ThingUID bridgeUid) {
54         for (String address : addresses) {
55             String[] parts = address.split("\\.");
56             if (parts.length != 3) {
57                 logger.warn("Address {} must be in the format XX.XX.XX", address);
58
59                 continue;
60             }
61
62             String name = parts[0] + parts[1] + parts[2];
63             ThingUID uid = new ThingUID(InsteonBindingConstants.DEVICE_THING_TYPE, bridgeUid, name);
64             Map<String, Object> properties = new HashMap<>();
65             properties.put(ADDRESS, address);
66
67             thingDiscovered(
68                     DiscoveryResultBuilder.create(uid).withProperties(properties).withLabel("Insteon Device  " + name)
69                             .withBridge(bridgeUid).withRepresentationProperty(ADDRESS).build());
70
71             logger.debug("Added Insteon device {} with the address {}", name, address);
72         }
73     }
74 }