]> git.basschouten.com Git - openhab-addons.git/blob
0bffe3aeb5e96d2126b76050ada7d7c7f34bcc37
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.nest.internal.sdm.discovery;
14
15 import static org.openhab.binding.nest.internal.sdm.SDMBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.concurrent.Future;
20
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;
40
41 /**
42  * The {@link SDMDiscoveryService} is discovers devices using the SDM API list devices method.
43  *
44  * @author Brian Higginbotham - Initial contribution
45  * @author Wouter Born - Initial contribution
46  *
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>
49  */
50 @NonNullByDefault
51 public class SDMDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
52
53     private final Logger logger = LoggerFactory.getLogger(SDMDiscoveryService.class);
54     private @NonNullByDefault({}) SDMAccountHandler accountHandler;
55     private @Nullable Future<?> discoveryJob;
56
57     public SDMDiscoveryService() {
58         super(SUPPORTED_THING_TYPES_UIDS, 30, false);
59     }
60
61     protected void activate(ComponentContext context) {
62     }
63
64     @Override
65     public void deactivate() {
66         cancelDiscoveryJob();
67         super.deactivate();
68     }
69
70     @Override
71     public @Nullable ThingHandler getThingHandler() {
72         return accountHandler;
73     }
74
75     @Override
76     public void setThingHandler(ThingHandler handler) {
77         if (handler instanceof SDMAccountHandler sdmAccountHandler) {
78             accountHandler = sdmAccountHandler;
79         }
80     }
81
82     @Override
83     protected void startScan() {
84         cancelDiscoveryJob();
85         discoveryJob = scheduler.submit(this::discoverDevices);
86     }
87
88     @Override
89     protected synchronized void stopScan() {
90         cancelDiscoveryJob();
91         super.stopScan();
92     }
93
94     private void cancelDiscoveryJob() {
95         Future<?> localDiscoveryJob = discoveryJob;
96         if (localDiscoveryJob != null) {
97             localDiscoveryJob.cancel(true);
98         }
99     }
100
101     private void discoverDevices() {
102         ThingUID bridgeUID = accountHandler.getThing().getUID();
103         logger.debug("Starting discovery scan for {}", bridgeUID);
104         try {
105             accountHandler.getAPI().listDevices().forEach(device -> addDeviceDiscoveryResult(bridgeUID, device));
106         } catch (FailedSendingSDMDataException | InvalidSDMAccessTokenException e) {
107             logger.debug("Exception during discovery scan for {}", bridgeUID, e);
108         }
109         logger.debug("Finished discovery scan for {}", bridgeUID);
110     }
111
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);
117             return;
118         }
119
120         String deviceId = device.name.deviceId;
121         ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, deviceId);
122
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) //
130                 .build() //
131         );
132     }
133
134     private String getDeviceLabel(SDMDevice device, SDMDeviceType type) {
135         String label = device.traits.deviceInfo.customName;
136         if (!label.isBlank()) {
137             return label;
138         }
139
140         List<SDMParentRelation> parentRelations = device.parentRelations;
141         String displayName = !parentRelations.isEmpty() ? parentRelations.get(0).displayName : "";
142         String typeLabel = type.toLabel();
143
144         return displayName.isBlank() ? String.format("Nest %s", typeLabel)
145                 : String.format("Nest %s %s", displayName, typeLabel);
146     }
147 }