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.JsonSmartHomeDeviceAlias;
34 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.DriverIdentity;
35 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
36 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeGroups.SmartHomeGroup;
37 import org.openhab.binding.amazonechocontrol.internal.jsons.SmartHomeBaseDevice;
38 import org.openhab.binding.amazonechocontrol.internal.smarthome.Constants;
39 import org.openhab.core.config.discovery.AbstractDiscoveryService;
40 import org.openhab.core.config.discovery.DiscoveryResult;
41 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
42 import org.openhab.core.thing.ThingUID;
43 import org.osgi.service.component.annotations.Activate;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * @author Lukas Knoeller - Initial contribution
51 public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService {
52 private AccountHandler accountHandler;
53 private final Logger logger = LoggerFactory.getLogger(SmartHomeDevicesDiscovery.class);
55 private @Nullable ScheduledFuture<?> startScanStateJob;
56 private @Nullable Long activateTimeStamp;
58 public SmartHomeDevicesDiscovery(AccountHandler accountHandler) {
59 super(SUPPORTED_SMART_HOME_THING_TYPES_UIDS, 10);
60 this.accountHandler = accountHandler;
63 public void activate() {
64 activate(new Hashtable<String, Object>());
68 public void deactivate() {
73 protected void startScan() {
75 Long activateTimeStamp = this.activateTimeStamp;
76 if (activateTimeStamp != null) {
77 removeOlderResults(activateTimeStamp);
79 setSmartHomeDevices(accountHandler.updateSmartHomeDeviceList(false));
82 protected void startAutomaticScan() {
83 if (!this.accountHandler.getThing().getThings().isEmpty()) {
87 Connection connection = this.accountHandler.findConnection();
88 if (connection == null) {
91 Date verifyTime = connection.tryGetVerifyTime();
92 if (verifyTime == null) {
95 if (new Date().getTime() - verifyTime.getTime() < 10000) {
102 protected void startBackgroundDiscovery() {
104 startScanStateJob = scheduler.scheduleWithFixedDelay(this::startAutomaticScan, 3000, 1000,
105 TimeUnit.MILLISECONDS);
109 protected void stopBackgroundDiscovery() {
114 ScheduledFuture<?> currentStartScanStateJob = startScanStateJob;
115 if (currentStartScanStateJob != null) {
116 currentStartScanStateJob.cancel(false);
117 startScanStateJob = null;
124 public void activate(@Nullable Map<String, Object> config) {
125 super.activate(config);
126 if (config != null) {
129 Long activateTimeStamp = this.activateTimeStamp;
130 if (activateTimeStamp == null) {
131 this.activateTimeStamp = new Date().getTime();
135 synchronized void setSmartHomeDevices(List<SmartHomeBaseDevice> deviceList) {
136 int smartHomeDeviceDiscoveryMode = accountHandler.getSmartHomeDevicesDiscoveryMode();
137 if (smartHomeDeviceDiscoveryMode == 0) {
141 for (Object smartHomeDevice : deviceList) {
142 ThingUID bridgeThingUID = this.accountHandler.getThing().getUID();
143 ThingUID thingUID = null;
144 String deviceName = null;
145 Map<String, Object> props = new HashMap<>();
147 if (smartHomeDevice instanceof SmartHomeDevice) {
148 SmartHomeDevice shd = (SmartHomeDevice) smartHomeDevice;
149 logger.trace("Found SmartHome device: {}", shd);
151 String entityId = shd.entityId;
152 if (entityId == null) {
156 String id = shd.findId();
161 boolean isSkillDevice = false;
162 DriverIdentity driverIdentity = shd.driverIdentity;
163 isSkillDevice = driverIdentity != null && "SKILL".equals(driverIdentity.namespace);
165 if (smartHomeDeviceDiscoveryMode == 1 && isSkillDevice) {
166 // Connected through skill
169 if (!(smartHomeDeviceDiscoveryMode == 2) && "openHAB".equalsIgnoreCase(shd.manufacturerName)) {
174 JsonSmartHomeCapabilities.SmartHomeCapability[] capabilities = shd.capabilities;
175 if (capabilities == null || Stream.of(capabilities).noneMatch(capability -> capability != null
176 && Constants.SUPPORTED_INTERFACES.contains(capability.interfaceName))) {
177 // No supported interface found
181 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE, bridgeThingUID, entityId.replace(".", "-"));
183 JsonSmartHomeDeviceAlias[] aliases = shd.aliases;
184 if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
185 && "SonarCloudService".equals(driverIdentity.identifier)) {
186 deviceName = "Alexa Guard on " + shd.friendlyName;
187 } else if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
188 && "OnGuardSmartHomeBridgeService".equals(driverIdentity.identifier)) {
189 deviceName = "Alexa Guard";
190 } else if (aliases != null && aliases.length > 0 && aliases[0] != null
191 && aliases[0].friendlyName != null) {
192 deviceName = aliases[0].friendlyName;
194 deviceName = shd.friendlyName;
196 props.put(DEVICE_PROPERTY_ID, id);
197 } else if (smartHomeDevice instanceof SmartHomeGroup) {
198 SmartHomeGroup shg = (SmartHomeGroup) smartHomeDevice;
199 logger.trace("Found SmartHome device: {}", shg);
201 String id = shg.findId();
206 Set<SmartHomeDevice> supportedChildren = SmartHomeDeviceHandler.getSupportedSmartHomeDevices(shg,
208 if (supportedChildren.size() == 0) {
209 // No children with an supported interface
212 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE_GROUP, bridgeThingUID, id.replace(".", "-"));
213 deviceName = shg.applianceGroupName;
214 props.put(DEVICE_PROPERTY_ID, id);
217 if (thingUID != null) {
218 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(deviceName)
219 .withProperties(props).withBridge(bridgeThingUID).build();
221 logger.debug("Device [{}] found.", deviceName);
223 thingDiscovered(result);