]> git.basschouten.com Git - openhab-addons.git/blob
e60525e220bc2977b4bc497699044256f194a049
[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.sleepiq.internal.discovery;
14
15 import java.util.HashMap;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.sleepiq.internal.SleepIQBindingConstants;
21 import org.openhab.binding.sleepiq.internal.api.dto.Bed;
22 import org.openhab.binding.sleepiq.internal.config.SleepIQBedConfiguration;
23 import org.openhab.binding.sleepiq.internal.handler.SleepIQCloudHandler;
24 import org.openhab.binding.sleepiq.internal.handler.SleepIQDualBedHandler;
25 import org.openhab.core.config.discovery.AbstractDiscoveryService;
26 import org.openhab.core.config.discovery.DiscoveryResult;
27 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
28 import org.openhab.core.thing.ThingUID;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link SleepIQBedDiscoveryService} is responsible for processing the
34  * results of devices found through the SleepIQ cloud service.
35  *
36  * @author Gregory Moyer - Initial contribution
37  */
38 @NonNullByDefault
39 public class SleepIQBedDiscoveryService extends AbstractDiscoveryService {
40     private static final int TIMEOUT = 60;
41
42     private final Logger logger = LoggerFactory.getLogger(SleepIQBedDiscoveryService.class);
43
44     private final SleepIQCloudHandler cloudHandler;
45
46     /**
47      * Discovers beds in the SleepIQ cloud service account.
48      *
49      * @param cloudHandler the cloud service handler (bridge)
50      */
51     public SleepIQBedDiscoveryService(final SleepIQCloudHandler cloudHandler) {
52         super(SleepIQDualBedHandler.SUPPORTED_THING_TYPE_UIDS, TIMEOUT, true);
53         this.cloudHandler = cloudHandler;
54     }
55
56     @Override
57     protected void startBackgroundDiscovery() {
58         logger.debug("Starting background discovery for new beds");
59         startScan();
60     }
61
62     @Override
63     protected void startScan() {
64         logger.debug("Starting scan for new beds");
65         List<Bed> beds = cloudHandler.getBeds();
66         if (beds != null) {
67             for (Bed bed : beds) {
68                 // only dual chamber beds are supported currently
69                 if (!bed.isDualSleep()) {
70                     logger.info("Found a bed that is not dual chamber - currently unsupported");
71                     continue;
72                 }
73
74                 ThingUID bridgeUID = cloudHandler.getThing().getUID();
75                 ThingUID thingUID = new ThingUID(SleepIQBindingConstants.THING_TYPE_DUAL_BED, bridgeUID,
76                         bed.getMacAddress());
77
78                 // thing already exists
79                 if (cloudHandler.getThing().getThing(thingUID) != null) {
80                     continue;
81                 }
82
83                 logger.debug("New bed found with MAC address {}", bed.getMacAddress());
84                 @SuppressWarnings({ "unchecked", "rawtypes" })
85                 Map<String, Object> properties = (Map) cloudHandler.updateProperties(bed, new HashMap<>());
86                 properties.put(SleepIQBedConfiguration.BED_ID, bed.getBedId());
87
88                 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
89                         .withBridge(bridgeUID).withLabel(bed.getName() + " - " + bed.getModel()).build();
90                 thingDiscovered(discoveryResult);
91             }
92         }
93         stopScan();
94     }
95 }