2 * Copyright (c) 2010-2023 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.sleepiq.internal.discovery;
15 import java.util.HashMap;
16 import java.util.List;
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;
33 * The {@link SleepIQBedDiscoveryService} is responsible for processing the
34 * results of devices found through the SleepIQ cloud service.
36 * @author Gregory Moyer - Initial contribution
39 public class SleepIQBedDiscoveryService extends AbstractDiscoveryService {
40 private static final int TIMEOUT = 60;
42 private final Logger logger = LoggerFactory.getLogger(SleepIQBedDiscoveryService.class);
44 private final SleepIQCloudHandler cloudHandler;
47 * Discovers beds in the SleepIQ cloud service account.
49 * @param cloudHandler the cloud service handler (bridge)
51 public SleepIQBedDiscoveryService(final SleepIQCloudHandler cloudHandler) {
52 super(SleepIQDualBedHandler.SUPPORTED_THING_TYPE_UIDS, TIMEOUT, true);
53 this.cloudHandler = cloudHandler;
57 protected void startBackgroundDiscovery() {
58 logger.debug("Starting background discovery for new beds");
63 protected void startScan() {
64 logger.debug("Starting scan for new beds");
65 List<Bed> beds = cloudHandler.getBeds();
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");
74 ThingUID bridgeUID = cloudHandler.getThing().getUID();
75 ThingUID thingUID = new ThingUID(SleepIQBindingConstants.THING_TYPE_DUAL_BED, bridgeUID,
78 // thing already exists
79 if (cloudHandler.getThing().getThing(thingUID) != null) {
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());
88 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
89 .withBridge(bridgeUID).withLabel(bed.getName() + " - " + bed.getModel()).build();
90 thingDiscovered(discoveryResult);