]> git.basschouten.com Git - openhab-addons.git/blob
fab8d55734a576e7269b384c8263f9ddf0b06afa
[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 https://developers.google.com/nest/device-access/reference/rest/v1/enterprises.devices/list
48  */
49 @NonNullByDefault
50 public class SDMDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService {
51
52     private final Logger logger = LoggerFactory.getLogger(SDMDiscoveryService.class);
53     private @NonNullByDefault({}) SDMAccountHandler accountHandler;
54     private @Nullable Future<?> discoveryJob;
55
56     public SDMDiscoveryService() {
57         super(SUPPORTED_THING_TYPES_UIDS, 30, false);
58     }
59
60     protected void activate(ComponentContext context) {
61     }
62
63     @Override
64     public void deactivate() {
65         cancelDiscoveryJob();
66         super.deactivate();
67     }
68
69     @Override
70     public @Nullable ThingHandler getThingHandler() {
71         return accountHandler;
72     }
73
74     @Override
75     public void setThingHandler(ThingHandler handler) {
76         if (handler instanceof SDMAccountHandler sdmAccountHandler) {
77             accountHandler = sdmAccountHandler;
78         }
79     }
80
81     @Override
82     protected void startScan() {
83         cancelDiscoveryJob();
84         discoveryJob = scheduler.submit(this::discoverDevices);
85     }
86
87     @Override
88     protected synchronized void stopScan() {
89         cancelDiscoveryJob();
90         super.stopScan();
91     }
92
93     private void cancelDiscoveryJob() {
94         Future<?> localDiscoveryJob = discoveryJob;
95         if (localDiscoveryJob != null) {
96             localDiscoveryJob.cancel(true);
97         }
98     }
99
100     private void discoverDevices() {
101         ThingUID bridgeUID = accountHandler.getThing().getUID();
102         logger.debug("Starting discovery scan for {}", bridgeUID);
103         try {
104             accountHandler.getAPI().listDevices().forEach(device -> addDeviceDiscoveryResult(bridgeUID, device));
105         } catch (FailedSendingSDMDataException | InvalidSDMAccessTokenException e) {
106             logger.debug("Exception during discovery scan for {}", bridgeUID, e);
107         }
108         logger.debug("Finished discovery scan for {}", bridgeUID);
109     }
110
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);
116             return;
117         }
118
119         String deviceId = device.name.deviceId;
120         ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, deviceId);
121
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) //
129                 .build() //
130         );
131     }
132
133     private String getDeviceLabel(SDMDevice device, SDMDeviceType type) {
134         String label = device.traits.deviceInfo.customName;
135         if (!label.isBlank()) {
136             return label;
137         }
138
139         List<SDMParentRelation> parentRelations = device.parentRelations;
140         String displayName = !parentRelations.isEmpty() ? parentRelations.get(0).displayName : "";
141         String typeLabel = type.toLabel();
142
143         return displayName.isBlank() ? String.format("Nest %s", typeLabel)
144                 : String.format("Nest %s %s", displayName, typeLabel);
145     }
146 }