2 * Copyright (c) 2010-2024 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.wled.internal;
15 import static org.openhab.binding.wled.internal.WLedBindingConstants.*;
17 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.wled.internal.api.WledApi;
24 import org.openhab.binding.wled.internal.handlers.WLedBridgeHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.config.discovery.DiscoveryService;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
36 * The {@link WLedSegmentDiscoveryService} Discovers and adds any Wled segments found by the bridge device.
38 * @author Matthew Skinner - Initial contribution
41 public class WLedSegmentDiscoveryService extends AbstractDiscoveryService
42 implements DiscoveryService, ThingHandlerService {
43 private @Nullable WLedBridgeHandler bridgeHandler;
44 private @Nullable ThingUID bridgeUID;
45 private static final int SEARCH_TIME = 10;
47 public WLedSegmentDiscoveryService() {
48 super(SUPPORTED_THING_TYPES, SEARCH_TIME);
51 public WLedSegmentDiscoveryService(Set<ThingTypeUID> supportedThingTypes, int timeout)
52 throws IllegalArgumentException {
53 super(supportedThingTypes, timeout);
56 private void buildThing(int segmentIndex, String segmentName) {
57 ThingUID localBridgeUID = bridgeUID;
58 if (localBridgeUID == null) {
61 String newThingUID = localBridgeUID.getId() + "-" + segmentIndex;
62 ThingUID thingUID = new ThingUID(THING_TYPE_SEGMENT, localBridgeUID, newThingUID);
63 Map<String, Object> properties = Map.of(Thing.PROPERTY_SERIAL_NUMBER, newThingUID, CONFIG_SEGMENT_INDEX,
65 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withLabel(segmentName)
66 .withProperties(properties).withBridge(bridgeUID)
67 .withRepresentationProperty(Thing.PROPERTY_SERIAL_NUMBER).build();
68 thingDiscovered(discoveryResult);
72 protected void startScan() {
73 WLedBridgeHandler localBridgeHandler = bridgeHandler;
74 if (localBridgeHandler != null) {
75 WledApi localAPI = localBridgeHandler.api;
76 if (localAPI != null) {
77 List<String> names = localAPI.getSegmentNames();
78 for (int count = 0; count < names.size(); count++) {
79 buildThing(count, names.get(count));
86 public void setThingHandler(@Nullable ThingHandler handler) {
87 if (handler instanceof WLedBridgeHandler wLedBridgeHandler) {
88 bridgeHandler = wLedBridgeHandler;
89 bridgeUID = bridgeHandler.getThing().getUID();
94 public @Nullable ThingHandler getThingHandler() {
99 public void deactivate() {