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.DEVICE_PROPERTY_ID;
16 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.SUPPORTED_SMART_HOME_THING_TYPES_UIDS;
17 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.THING_TYPE_SMART_HOME_DEVICE;
18 import static org.openhab.binding.amazonechocontrol.internal.AmazonEchoControlBindingConstants.THING_TYPE_SMART_HOME_DEVICE_GROUP;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.Hashtable;
23 import java.util.List;
26 import java.util.concurrent.ScheduledFuture;
27 import java.util.concurrent.TimeUnit;
28 import java.util.stream.Stream;
30 import org.eclipse.jdt.annotation.NonNullByDefault;
31 import org.eclipse.jdt.annotation.Nullable;
32 import org.openhab.binding.amazonechocontrol.internal.Connection;
33 import org.openhab.binding.amazonechocontrol.internal.handler.AccountHandler;
34 import org.openhab.binding.amazonechocontrol.internal.handler.SmartHomeDeviceHandler;
35 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.DriverIdentity;
36 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeDevices.SmartHomeDevice;
37 import org.openhab.binding.amazonechocontrol.internal.jsons.JsonSmartHomeGroups.SmartHomeGroup;
38 import org.openhab.binding.amazonechocontrol.internal.jsons.SmartHomeBaseDevice;
39 import org.openhab.binding.amazonechocontrol.internal.smarthome.Constants;
40 import org.openhab.core.config.discovery.AbstractDiscoveryService;
41 import org.openhab.core.config.discovery.DiscoveryResult;
42 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
43 import org.openhab.core.thing.ThingUID;
44 import org.osgi.service.component.annotations.Activate;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * @author Lukas Knoeller - Initial contribution
52 public class SmartHomeDevicesDiscovery extends AbstractDiscoveryService {
53 private AccountHandler accountHandler;
54 private final Logger logger = LoggerFactory.getLogger(SmartHomeDevicesDiscovery.class);
56 private @Nullable ScheduledFuture<?> startScanStateJob;
57 private @Nullable Long activateTimeStamp;
59 public SmartHomeDevicesDiscovery(AccountHandler accountHandler) {
60 super(SUPPORTED_SMART_HOME_THING_TYPES_UIDS, 10);
61 this.accountHandler = accountHandler;
64 public void activate() {
65 activate(new Hashtable<String, @Nullable Object>());
69 public void deactivate() {
74 protected void startScan() {
76 Long activateTimeStamp = this.activateTimeStamp;
77 if (activateTimeStamp != null) {
78 removeOlderResults(activateTimeStamp);
80 setSmartHomeDevices(accountHandler.updateSmartHomeDeviceList(false));
83 protected void startAutomaticScan() {
84 if (!this.accountHandler.getThing().getThings().isEmpty()) {
88 Connection connection = this.accountHandler.findConnection();
89 if (connection == null) {
92 Date verifyTime = connection.tryGetVerifyTime();
93 if (verifyTime == null) {
96 if (new Date().getTime() - verifyTime.getTime() < 10000) {
103 protected void startBackgroundDiscovery() {
105 startScanStateJob = scheduler.scheduleWithFixedDelay(this::startAutomaticScan, 3000, 1000,
106 TimeUnit.MILLISECONDS);
110 protected void stopBackgroundDiscovery() {
115 ScheduledFuture<?> currentStartScanStateJob = startScanStateJob;
116 if (currentStartScanStateJob != null) {
117 currentStartScanStateJob.cancel(false);
118 startScanStateJob = null;
125 public void activate(@Nullable Map<String, @Nullable Object> config) {
126 super.activate(config);
127 if (config != null) {
130 Long activateTimeStamp = this.activateTimeStamp;
131 if (activateTimeStamp == null) {
132 this.activateTimeStamp = new Date().getTime();
136 synchronized void setSmartHomeDevices(List<SmartHomeBaseDevice> deviceList) {
137 int smartHomeDeviceDiscoveryMode = accountHandler.getSmartHomeDevicesDiscoveryMode();
138 if (smartHomeDeviceDiscoveryMode == 0) {
142 for (Object smartHomeDevice : deviceList) {
143 ThingUID bridgeThingUID = this.accountHandler.getThing().getUID();
144 ThingUID thingUID = null;
145 String deviceName = null;
146 Map<String, Object> props = new HashMap<>();
148 if (smartHomeDevice instanceof SmartHomeDevice) {
149 SmartHomeDevice shd = (SmartHomeDevice) smartHomeDevice;
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 if (Stream.of(shd.capabilities).noneMatch(capability -> capability != null
175 && Constants.SUPPORTED_INTERFACES.contains(capability.interfaceName))) {
176 // No supported interface found
180 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE, bridgeThingUID, entityId.replace(".", "-"));
182 if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
183 && "SonarCloudService".equals(driverIdentity.identifier)) {
184 deviceName = "Alexa Guard on " + shd.friendlyName;
185 } else if ("Amazon".equals(shd.manufacturerName) && driverIdentity != null
186 && "OnGuardSmartHomeBridgeService".equals(driverIdentity.identifier)) {
187 deviceName = "Alexa Guard";
188 } else if (shd.aliases != null && shd.aliases.length > 0 && shd.aliases[0] != null
189 && shd.aliases[0].friendlyName != null) {
190 deviceName = shd.aliases[0].friendlyName;
192 deviceName = shd.friendlyName;
194 props.put(DEVICE_PROPERTY_ID, id);
197 if (smartHomeDevice instanceof SmartHomeGroup) {
198 SmartHomeGroup shg = (SmartHomeGroup) smartHomeDevice;
199 String id = shg.findId();
204 Set<SmartHomeDevice> supportedChildren = SmartHomeDeviceHandler.getSupportedSmartHomeDevices(shg,
206 if (supportedChildren.size() == 0) {
207 // No children with an supported interface
210 thingUID = new ThingUID(THING_TYPE_SMART_HOME_DEVICE_GROUP, bridgeThingUID, id.replace(".", "-"));
211 deviceName = shg.applianceGroupName;
212 props.put(DEVICE_PROPERTY_ID, id);
215 if (thingUID != null) {
216 DiscoveryResult result = DiscoveryResultBuilder.create(thingUID).withLabel(deviceName)
217 .withProperties(props).withBridge(bridgeThingUID).build();
219 logger.debug("Device [{}] found.", deviceName);
221 thingDiscovered(result);