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