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.nest.internal.sdm.discovery;
15 import static org.openhab.binding.nest.internal.sdm.SDMBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.concurrent.Future;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.nest.internal.sdm.config.SDMDeviceConfiguration;
24 import org.openhab.binding.nest.internal.sdm.dto.SDMDevice;
25 import org.openhab.binding.nest.internal.sdm.dto.SDMDeviceType;
26 import org.openhab.binding.nest.internal.sdm.dto.SDMParentRelation;
27 import org.openhab.binding.nest.internal.sdm.exception.FailedSendingSDMDataException;
28 import org.openhab.binding.nest.internal.sdm.exception.InvalidSDMAccessTokenException;
29 import org.openhab.binding.nest.internal.sdm.handler.SDMAccountHandler;
30 import org.openhab.binding.nest.internal.sdm.handler.SDMBaseHandler;
31 import org.openhab.core.config.discovery.AbstractDiscoveryService;
32 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerService;
37 import org.osgi.service.component.ComponentContext;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link SDMDiscoveryService} is discovers devices using the SDM API list devices method.
44 * @author Brian Higginbotham - Initial contribution
45 * @author Wouter Born - Initial contribution
47 * @see <a href="https://developers.google.com/nest/device-access/reference/rest/v1/enterprises.devices/list">
48 * https://developers.google.com/nest/device-access/reference/rest/v1/enterprises.devices/list</a>
51 public class SDMDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
53 private final Logger logger = LoggerFactory.getLogger(SDMDiscoveryService.class);
54 private @NonNullByDefault({}) SDMAccountHandler accountHandler;
55 private @Nullable Future<?> discoveryJob;
57 public SDMDiscoveryService() {
58 super(SUPPORTED_THING_TYPES_UIDS, 30, false);
61 protected void activate(ComponentContext context) {
65 public void deactivate() {
71 public @Nullable ThingHandler getThingHandler() {
72 return accountHandler;
76 public void setThingHandler(ThingHandler handler) {
77 if (handler instanceof SDMAccountHandler sdmAccountHandler) {
78 accountHandler = sdmAccountHandler;
83 protected void startScan() {
85 discoveryJob = scheduler.submit(this::discoverDevices);
89 protected synchronized void stopScan() {
94 private void cancelDiscoveryJob() {
95 Future<?> localDiscoveryJob = discoveryJob;
96 if (localDiscoveryJob != null) {
97 localDiscoveryJob.cancel(true);
101 private void discoverDevices() {
102 ThingUID bridgeUID = accountHandler.getThing().getUID();
103 logger.debug("Starting discovery scan for {}", bridgeUID);
105 accountHandler.getAPI().listDevices().forEach(device -> addDeviceDiscoveryResult(bridgeUID, device));
106 } catch (FailedSendingSDMDataException | InvalidSDMAccessTokenException e) {
107 logger.debug("Exception during discovery scan for {}", bridgeUID, e);
109 logger.debug("Finished discovery scan for {}", bridgeUID);
112 private void addDeviceDiscoveryResult(ThingUID bridgeUID, SDMDevice device) {
113 SDMDeviceType type = device.type;
114 ThingTypeUID thingTypeUID = type == null ? null : SDM_THING_TYPE_MAPPING.get(type);
115 if (type == null || thingTypeUID == null) {
116 logger.debug("Ignoring unsupported device type: {}", type);
120 String deviceId = device.name.deviceId;
121 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, deviceId);
123 thingDiscovered(DiscoveryResultBuilder.create(thingUID) //
124 .withThingType(thingTypeUID) //
125 .withLabel(getDeviceLabel(device, type)) //
126 .withBridge(bridgeUID) //
127 .withProperty(SDMDeviceConfiguration.DEVICE_ID, deviceId) //
128 .withProperties(new HashMap<>(SDMBaseHandler.getDeviceProperties(device))) //
129 .withRepresentationProperty(SDMDeviceConfiguration.DEVICE_ID) //
134 private String getDeviceLabel(SDMDevice device, SDMDeviceType type) {
135 String label = device.traits.deviceInfo.customName;
136 if (!label.isBlank()) {
140 List<SDMParentRelation> parentRelations = device.parentRelations;
141 String displayName = !parentRelations.isEmpty() ? parentRelations.get(0).displayName : "";
142 String typeLabel = type.toLabel();
144 return displayName.isBlank() ? String.format("Nest %s", typeLabel)
145 : String.format("Nest %s %s", displayName, typeLabel);