2 * Copyright (c) 2010-2023 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.pilight.internal.discovery;
15 import static org.openhab.binding.pilight.internal.PilightBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.List;
20 import java.util.Optional;
22 import java.util.concurrent.CompletableFuture;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.eclipse.jdt.annotation.Nullable;
28 import org.openhab.binding.pilight.internal.PilightHandlerFactory;
29 import org.openhab.binding.pilight.internal.dto.Config;
30 import org.openhab.binding.pilight.internal.dto.DeviceType;
31 import org.openhab.binding.pilight.internal.dto.Status;
32 import org.openhab.binding.pilight.internal.handler.PilightBridgeHandler;
33 import org.openhab.core.config.discovery.AbstractDiscoveryService;
34 import org.openhab.core.config.discovery.DiscoveryResult;
35 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerService;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * The {@link PilightDeviceDiscoveryService} discovers pilight devices after a bridge thing has been created and
45 * connected to the pilight daemon. Things are discovered periodically in the background or after a manual trigger.
47 * @author Niklas Dörfler - Initial contribution
50 public class PilightDeviceDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
52 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = PilightHandlerFactory.SUPPORTED_THING_TYPES_UIDS;
54 private static final int AUTODISCOVERY_SEARCH_TIME_SEC = 10;
55 private static final int AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC = 60 * 10;
57 private final Logger logger = LoggerFactory.getLogger(PilightDeviceDiscoveryService.class);
59 private @Nullable PilightBridgeHandler pilightBridgeHandler;
60 private @Nullable ThingUID bridgeUID;
62 private @Nullable ScheduledFuture<?> backgroundDiscoveryJob;
63 private CompletableFuture<Config> configFuture;
64 private CompletableFuture<List<Status>> statusFuture;
66 public PilightDeviceDiscoveryService() {
67 super(SUPPORTED_THING_TYPES_UIDS, AUTODISCOVERY_SEARCH_TIME_SEC);
68 configFuture = new CompletableFuture<>();
69 statusFuture = new CompletableFuture<>();
73 protected void startScan() {
74 if (pilightBridgeHandler != null) {
75 configFuture = new CompletableFuture<>();
76 statusFuture = new CompletableFuture<>();
78 configFuture.thenAcceptBoth(statusFuture, (config, allStatus) -> {
79 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
80 config.getDevices().forEach((deviceId, device) -> {
81 if (this.pilightBridgeHandler != null) {
82 final Optional<Status> status = allStatus.stream()
83 .filter(s -> s.getDevices().contains(deviceId)).findFirst();
85 final ThingTypeUID thingTypeUID;
86 final String typeString;
88 if (status.isPresent()) {
89 if (status.get().getType().equals(DeviceType.SWITCH)) {
90 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_SWITCH.getId());
91 typeString = "Switch";
92 } else if (status.get().getType().equals(DeviceType.DIMMER)) {
93 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_DIMMER.getId());
94 typeString = "Dimmer";
95 } else if (status.get().getType().equals(DeviceType.VALUE)) {
96 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
97 typeString = "Generic";
98 } else if (status.get().getType().equals(DeviceType.CONTACT)) {
99 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_CONTACT.getId());
100 typeString = "Contact";
102 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
103 typeString = "Generic";
106 thingTypeUID = new ThingTypeUID(BINDING_ID, THING_TYPE_GENERIC.getId());
107 typeString = "Generic";
110 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
111 if (pilightBridgeHandler != null) {
112 final ThingUID thingUID = new ThingUID(thingTypeUID,
113 pilightBridgeHandler.getThing().getUID(), deviceId);
115 final Map<String, Object> properties = new HashMap<>();
116 properties.put(PROPERTY_NAME, deviceId);
118 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID)
119 .withThingType(thingTypeUID).withProperties(properties).withBridge(bridgeUID)
120 .withRepresentationProperty(PROPERTY_NAME)
121 .withLabel("Pilight " + typeString + " Device '" + deviceId + "'").build();
123 thingDiscovered(discoveryResult);
129 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
130 if (pilightBridgeHandler != null) {
131 pilightBridgeHandler.refreshConfigAndStatus();
137 protected synchronized void stopScan() {
139 configFuture.cancel(true);
140 statusFuture.cancel(true);
141 if (bridgeUID != null) {
142 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
147 protected void startBackgroundDiscovery() {
148 logger.debug("Start Pilight device background discovery");
149 final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
150 if (backgroundDiscoveryJob == null || backgroundDiscoveryJob.isCancelled()) {
151 this.backgroundDiscoveryJob = scheduler.scheduleWithFixedDelay(this::startScan, 20,
152 AUTODISCOVERY_BACKGROUND_SEARCH_INTERVAL_SEC, TimeUnit.SECONDS);
157 protected void stopBackgroundDiscovery() {
158 logger.debug("Stop Pilight device background discovery");
159 final @Nullable ScheduledFuture<?> backgroundDiscoveryJob = this.backgroundDiscoveryJob;
160 if (backgroundDiscoveryJob != null) {
161 backgroundDiscoveryJob.cancel(true);
162 this.backgroundDiscoveryJob = null;
167 public void setThingHandler(final ThingHandler handler) {
168 if (handler instanceof PilightBridgeHandler pilightBridgeHandler) {
169 this.pilightBridgeHandler = pilightBridgeHandler;
170 bridgeUID = pilightBridgeHandler.getThing().getUID();
175 public @Nullable ThingHandler getThingHandler() {
176 return pilightBridgeHandler;
180 public void activate() {
181 super.activate(null);
182 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
183 if (pilightBridgeHandler != null) {
184 pilightBridgeHandler.registerDiscoveryListener(this);
189 public void deactivate() {
190 if (bridgeUID != null) {
191 removeOlderResults(getTimestampOfLastScan(), bridgeUID);
194 final @Nullable PilightBridgeHandler pilightBridgeHandler = this.pilightBridgeHandler;
195 if (pilightBridgeHandler != null) {
196 pilightBridgeHandler.unregisterDiscoveryListener();
203 * Method used to get pilight device config into the discovery class.
205 * @param config config to get
207 public void setConfig(Config config) {
208 configFuture.complete(config);
212 * Method used to get pilight device status list into the discovery class.
214 * @param status list of status objects
216 public void setStatus(List<Status> status) {
217 statusFuture.complete(status);
221 public Set<ThingTypeUID> getSupportedThingTypes() {
222 return SUPPORTED_THING_TYPES_UIDS;