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.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 https://developers.google.com/nest/device-access/reference/rest/v1/enterprises.devices/list
50 public class SDMDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
52 private final Logger logger = LoggerFactory.getLogger(SDMDiscoveryService.class);
53 private @NonNullByDefault({}) SDMAccountHandler accountHandler;
54 private @Nullable Future<?> discoveryJob;
56 public SDMDiscoveryService() {
57 super(SUPPORTED_THING_TYPES_UIDS, 30, false);
60 protected void activate(ComponentContext context) {
64 public void deactivate() {
70 public @Nullable ThingHandler getThingHandler() {
71 return accountHandler;
75 public void setThingHandler(ThingHandler handler) {
76 if (handler instanceof SDMAccountHandler) {
77 accountHandler = (SDMAccountHandler) handler;
82 protected void startScan() {
84 discoveryJob = scheduler.submit(this::discoverDevices);
88 protected synchronized void stopScan() {
93 private void cancelDiscoveryJob() {
94 Future<?> localDiscoveryJob = discoveryJob;
95 if (localDiscoveryJob != null) {
96 localDiscoveryJob.cancel(true);
100 private void discoverDevices() {
101 ThingUID bridgeUID = accountHandler.getThing().getUID();
102 logger.debug("Starting discovery scan for {}", bridgeUID);
104 accountHandler.getAPI().listDevices().forEach(device -> addDeviceDiscoveryResult(bridgeUID, device));
105 } catch (FailedSendingSDMDataException | InvalidSDMAccessTokenException e) {
106 logger.debug("Exception during discovery scan for {}", bridgeUID, e);
108 logger.debug("Finished discovery scan for {}", bridgeUID);
111 private void addDeviceDiscoveryResult(ThingUID bridgeUID, SDMDevice device) {
112 SDMDeviceType type = device.type;
113 ThingTypeUID thingTypeUID = type == null ? null : SDM_THING_TYPE_MAPPING.get(type);
114 if (type == null || thingTypeUID == null) {
115 logger.debug("Ignoring unsupported device type: {}", type);
119 String deviceId = device.name.deviceId;
120 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, deviceId);
122 thingDiscovered(DiscoveryResultBuilder.create(thingUID) //
123 .withThingType(thingTypeUID) //
124 .withLabel(getDeviceLabel(device, type)) //
125 .withBridge(bridgeUID) //
126 .withProperty(SDMDeviceConfiguration.DEVICE_ID, deviceId) //
127 .withProperties(new HashMap<>(SDMBaseHandler.getDeviceProperties(device))) //
128 .withRepresentationProperty(SDMDeviceConfiguration.DEVICE_ID) //
133 private String getDeviceLabel(SDMDevice device, SDMDeviceType type) {
134 String label = device.traits.deviceInfo.customName;
135 if (!label.isBlank()) {
139 List<SDMParentRelation> parentRelations = device.parentRelations;
140 String displayName = !parentRelations.isEmpty() ? parentRelations.get(0).displayName : "";
141 String typeLabel = type.toLabel();
143 return displayName.isBlank() ? String.format("Nest %s", typeLabel)
144 : String.format("Nest %s %s", displayName, typeLabel);