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.icloud.internal.discovery;
15 import static org.openhab.binding.icloud.internal.ICloudBindingConstants.*;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.icloud.internal.ICloudDeviceInformationListener;
21 import org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler;
22 import org.openhab.binding.icloud.internal.handler.dto.json.response.ICloudDeviceInformation;
23 import org.openhab.binding.icloud.internal.utilities.ICloudTextTranslator;
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.i18n.LocaleProvider;
28 import org.openhab.core.i18n.TranslationProvider;
29 import org.openhab.core.thing.ThingUID;
30 import org.osgi.framework.Bundle;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Device discovery creates a thing in the inbox for each icloud device
36 * found in the data received from {@link ICloudAccountBridgeHandler}.
38 * @author Patrik Gfeller - Initial Contribution
42 public class ICloudDeviceDiscovery extends AbstractDiscoveryService implements ICloudDeviceInformationListener {
43 private final Logger logger = LoggerFactory.getLogger(ICloudDeviceDiscovery.class);
44 private static final int TIMEOUT = 10;
45 private ThingUID bridgeUID;
46 private ICloudAccountBridgeHandler handler;
47 private ICloudTextTranslator translatorService;
49 public ICloudDeviceDiscovery(ICloudAccountBridgeHandler bridgeHandler, Bundle bundle,
50 TranslationProvider i18nProvider, LocaleProvider localeProvider) {
51 super(SUPPORTED_THING_TYPES_UIDS, TIMEOUT);
53 this.handler = bridgeHandler;
54 this.bridgeUID = bridgeHandler.getThing().getUID();
55 this.translatorService = new ICloudTextTranslator(bundle, i18nProvider, localeProvider);
59 public void deviceInformationUpdate(List<ICloudDeviceInformation> deviceInformationList) {
60 for (ICloudDeviceInformation deviceInformationRecord : deviceInformationList) {
62 String deviceTypeName = deviceInformationRecord.getDeviceDisplayName();
63 String deviceOwnerName = deviceInformationRecord.getName();
65 String thingLabel = deviceOwnerName + " (" + deviceTypeName + ")";
66 String deviceDiscoveryId = deviceInformationRecord.getDeviceDiscoveryId();
67 if (deviceDiscoveryId == null || deviceDiscoveryId.isBlank()) {
68 logger.debug("deviceDiscoveryId is empty, using device name for identification.");
69 deviceDiscoveryId = deviceOwnerName;
71 String deviceIdHash = Integer.toHexString(deviceDiscoveryId.hashCode());
73 logger.debug("iCloud device discovery for [{}]", deviceInformationRecord.getDeviceDisplayName());
75 ThingUID uid = new ThingUID(THING_TYPE_ICLOUDDEVICE, bridgeUID, deviceIdHash);
76 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID)
77 .withProperty(DEVICE_PROPERTY_ID, deviceDiscoveryId)
78 .withProperty(translatorService.getText(DEVICE_PROPERTY_ID_LABEL), deviceDiscoveryId)
79 .withProperty(translatorService.getText(DEVICE_PROPERTY_OWNER_LABEL), deviceOwnerName)
80 .withRepresentationProperty(DEVICE_PROPERTY_ID).withLabel(thingLabel).build();
82 logger.debug("Device [{}, {}] found.", deviceIdHash, deviceDiscoveryId);
84 thingDiscovered(result);
90 protected void startScan() {
93 public void activate() {
94 handler.registerListener(this);
98 public void deactivate() {
100 handler.unregisterListener(this);