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;
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;
31 * The {@link SleepIQBedDiscoveryService} is responsible for processing the
32 * results of devices found through the SleepIQ cloud service.
34 * @author Gregory Moyer - Initial contribution
36 public class SleepIQBedDiscoveryService extends AbstractDiscoveryService {
37 private static final int TIMEOUT = 60;
39 private final Logger logger = LoggerFactory.getLogger(SleepIQBedDiscoveryService.class);
41 private final SleepIQCloudHandler cloudHandler;
44 * Discovers beds in the SleepIQ cloud service account.
46 * @param cloudHandler the cloud service handler (bridge)
48 public SleepIQBedDiscoveryService(final SleepIQCloudHandler cloudHandler) {
49 super(SleepIQDualBedHandler.SUPPORTED_THING_TYPE_UIDS, TIMEOUT, true);
50 this.cloudHandler = cloudHandler;
54 protected void startBackgroundDiscovery() {
55 logger.debug("Starting background discovery for new beds");
60 protected void startScan() {
61 logger.debug("Starting scan for new beds");
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");
70 ThingUID bridgeUID = cloudHandler.getThing().getUID();
71 ThingUID thingUID = new ThingUID(SleepIQBindingConstants.THING_TYPE_DUAL_BED, bridgeUID,
74 // thing already exists
75 if (cloudHandler.getThing().getThing(thingUID) != null) {
79 logger.debug("New bed found with MAC address {}", bed.getMacAddress());
81 @SuppressWarnings({ "unchecked", "rawtypes" })
82 Map<String, Object> properties = (Map) cloudHandler.updateProperties(bed, new HashMap<>());
83 properties.put(SleepIQBedConfiguration.BED_ID, bed.getBedId());
85 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
86 .withBridge(bridgeUID).withLabel(bed.getName() + " - " + bed.getModel()).build();
87 thingDiscovered(discoveryResult);