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.nuki.internal.discovery;
15 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.nuki.internal.constants.NukiBindingConstants;
21 import org.openhab.binding.nuki.internal.dataexchange.BridgeListResponse;
22 import org.openhab.binding.nuki.internal.dto.BridgeApiListDeviceDto;
23 import org.openhab.binding.nuki.internal.handler.NukiBridgeHandler;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
25 import org.openhab.core.config.discovery.DiscoveryResult;
26 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.thing.binding.ThingHandler;
29 import org.openhab.core.thing.binding.ThingHandlerService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Discovery service which uses Brige API to find all devices connected to bridges.
36 * @author Jan Vybíral - Initial contribution
39 public class NukiDeviceDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
41 private final Logger logger = LoggerFactory.getLogger(NukiDeviceDiscoveryService.class);
43 private NukiBridgeHandler bridge;
45 public NukiDeviceDiscoveryService() {
46 super(Set.of(NukiBindingConstants.THING_TYPE_SMARTLOCK), 5, false);
50 protected void startScan() {
51 NukiBridgeHandler bridgeHandler = bridge;
52 if (bridgeHandler == null) {
53 logger.warn("Cannot start Nuki discovery - no bridge available");
57 scheduler.execute(() -> {
58 bridgeHandler.withHttpClient(client -> {
59 BridgeListResponse list = client.getList();
60 list.getDevices().stream().map(device -> createDiscoveryResult(device, bridgeHandler))
61 .flatMap(Optional::stream).forEach(this::thingDiscovered);
66 private Optional<DiscoveryResult> createDiscoveryResult(BridgeApiListDeviceDto device,
67 NukiBridgeHandler bridgeHandler) {
68 ThingUID uid = getUid(device.getNukiId(), device.getDeviceType(), bridgeHandler);
70 logger.warn("Failed to create UID for device '{}' - deviceType '{}' is not supported", device,
71 device.getDeviceType());
72 return Optional.empty();
74 return Optional.of(DiscoveryResultBuilder.create(uid).withBridge(bridgeHandler.getThing().getUID())
75 .withLabel(device.getName()).withRepresentationProperty(NukiBindingConstants.PROPERTY_NUKI_ID)
76 .withProperty(NukiBindingConstants.PROPERTY_NAME, device.getName())
77 .withProperty(NukiBindingConstants.PROPERTY_NUKI_ID, device.getNukiId())
78 .withProperty(NukiBindingConstants.PROPERTY_DEVICE_TYPE, device.getDeviceType())
79 .withProperty(NukiBindingConstants.PROPERTY_FIRMWARE_VERSION, device.getFirmwareVersion()).build());
84 private ThingUID getUid(String nukiId, int deviceType, NukiBridgeHandler bridgeHandler) {
86 case NukiBindingConstants.DEVICE_OPENER:
87 return new ThingUID(NukiBindingConstants.THING_TYPE_OPENER, bridgeHandler.getThing().getUID(), nukiId);
88 case NukiBindingConstants.DEVICE_SMART_LOCK:
89 case NukiBindingConstants.DEVICE_SMART_DOOR:
90 case NukiBindingConstants.DEVICE_SMART_LOCK_3:
91 return new ThingUID(NukiBindingConstants.THING_TYPE_SMARTLOCK, bridgeHandler.getThing().getUID(),
99 protected synchronized void stopScan() {
101 removeOlderResults(getTimestampOfLastScan());
105 public void setThingHandler(@Nullable ThingHandler handler) {
106 if (handler instanceof NukiBridgeHandler) {
107 bridge = (NukiBridgeHandler) handler;
112 public @Nullable ThingHandler getThingHandler() {
117 public void deactivate() {