2 * Copyright (c) 2010-2020 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.amazonechocontrol.internal.discovery;
15 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.*;
17 import java.util.Date;
18 import java.util.HashMap;
19 import java.util.Hashtable;
20 import java.util.List;
23 import java.util.concurrent.ScheduledFuture;
24 import java.util.concurrent.TimeUnit;
25 import java.util.stream.Stream;
27 import org.eclipse.jdt.annotation.NonNullByDefault;
28 import org.eclipse.jdt.annotation.Nullable;
29 import org.openhab.binding.amazonechocontrol.internal.Connection;
30 import org.openhab.binding.amazonechocontrol.internal.handler.AccountHandler;
31 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
32 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeCapabilities;
33 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.DriverIdentity;
34 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
35 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeGroups.SmartHomeGroup;
36 import org.openhab.binding.amazonechocontrol.internal.jsons.SmartHomeBaseDevice;
37 import org.openhab.binding.amazonechocontrol.internal.smarthome.Constants;
38 import org.openhab.core.config.discovery.AbstractDiscoveryService;
39 import org.openhab.core.config.discovery.DiscoveryResult;
40 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
41 import org.openhab.core.thing.ThingUID;
42 import org.osgi.service.component.annotations.Activate;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * @author Lukas Knoeller - Initial contribution
50 public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService {
51 private AccountHandler accountHandler;
52 private final Logger logger = LoggerFactory.getLogger(SmartHomeDevicesDiscovery.class);
54 private @Nullable ScheduledFuture<?> startScanStateJob;
55 private @Nullable Long activateTimeStamp;
57 public SmartHomeDevicesDiscovery(AccountHandler accountHandler) {
58 super(SUPPORTED_SMART_HOME_THING_TYPES_UIDS, 10);
59 this.accountHandler = accountHandler;
62 public void activate() {
63 activate(new Hashtable<String, Object>());
67 public void deactivate() {
72 protected void startScan() {
74 Long activateTimeStamp = this.activateTimeStamp;
75 if (activateTimeStamp != null) {
76 removeOlderResults(activateTimeStamp);
78 setSmartHomeDevices(accountHandler.updateSmartHomeDeviceList(false));
81 protected void startAutomaticScan() {
82 if (!this.accountHandler.getThing().getThings().isEmpty()) {
86 Connection connection = this.accountHandler.findConnection();
87 if (connection == null) {
90 Date verifyTime = connection.tryGetVerifyTime();
91 if (verifyTime == null) {
94 if (new Date().getTime() - verifyTime.getTime() < 10000) {
101 protected void startBackgroundDiscovery() {
103 startScanStateJob = scheduler.scheduleWithFixedDelay(this::startAutomaticScan, 3000, 1000,
104 TimeUnit.MILLISECONDS);
108 protected void stopBackgroundDiscovery() {
113 ScheduledFuture<?> currentStartScanStateJob = startScanStateJob;
114 if (currentStartScanStateJob != null) {
115 currentStartScanStateJob.cancel(false);
116 startScanStateJob = null;
123 public void activate(@Nullable Map<String, Object> config) {
124 super.activate(config);
125 if (config != null) {
128 Long activateTimeStamp = this.activateTimeStamp;
129 if (activateTimeStamp == null) {
130 this.activateTimeStamp = new Date().getTime();
134 synchronized void setSmartHomeDevices(List<SmartHomeBaseDevice> deviceList) {
135 int smartHomeDeviceDiscoveryMode = accountHandler.getSmartHomeDevicesDiscoveryMode();
136 if (smartHomeDeviceDiscoveryMode == 0) {
140 for (Object smartHomeDevice : deviceList) {
141 ThingUID bridgeThingUID = this.accountHandler.getThing().getUID();
142 ThingUID thingUID = null;
143 String deviceName = null;
144 Map<String, Object> props = new HashMap<>();
146 if (smartHomeDevice instanceof SmartHomeDevice) {
147 SmartHomeDevice shd = (SmartHomeDevice) smartHomeDevice;
149 String entityId = shd.entityId;
150 if (entityId == null) {
154 String id = shd.findId();
159 boolean isSkillDevice = false;
160 DriverIdentity driverIdentity = shd.driverIdentity;
161 isSkillDevice = driverIdentity != null && "SKILL".equals(driverIdentity.namespace);
163 if (smartHomeDeviceDiscoveryMode == 1 && isSkillDevice) {
164 // Connected through skill
167 if (!(smartHomeDeviceDiscoveryMode == 2) && "openHAB".equalsIgnoreCase(shd.manufacturerName)) {
172 JsonSmartHomeCapabilities.SmartHomeCapability[] capabilities = shd.capabilities;
173 if (capabilities == null || Stream.of(capabilities).noneMatch(capability -> capability != null
174 && Constants.SUPPORTED_INTERFACES.contains(capability.interfaceName))) {
175 // No supported interface found
179 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE, bridgeThingUID, entityId.replace(".", "-"));
181 if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
182 && "SonarCloudService".equals(driverIdentity.identifier)) {
183 deviceName = "Alexa Guard on " + shd.friendlyName;
184 } else if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
185 && "OnGuardSmartHomeBridgeService".equals(driverIdentity.identifier)) {
186 deviceName = "Alexa Guard";
187 } else if (shd.aliases != null && shd.aliases.length > 0 && shd.aliases[0] != null
188 && shd.aliases[0].friendlyName != null) {
189 deviceName = shd.aliases[0].friendlyName;
191 deviceName = shd.friendlyName;
193 props.put(DEVICE_PROPERTY_ID, id);
196 if (smartHomeDevice instanceof SmartHomeGroup) {
197 SmartHomeGroup shg = (SmartHomeGroup) smartHomeDevice;
198 String id = shg.findId();
203 Set<SmartHomeDevice> supportedChildren = SmartHomeDeviceHandler.getSupportedSmartHomeDevices(shg,
205 if (supportedChildren.size() == 0) {
206 // No children with an supported interface
209 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE_GROUP, bridgeThingUID, id.replace(".", "-"));
210 deviceName = shg.applianceGroupName;
211 props.put(DEVICE_PROPERTY_ID, id);
214 if (thingUID != null) {
215 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(deviceName)
216 .withProperties(props).withBridge(bridgeThingUID).build();
218 logger.debug("Device [{}] found.", deviceName);
220 thingDiscovered(result);