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.yeelight.internal.discovery;
15 import org.openhab.binding.yeelight.internal.YeelightBindingConstants;
16 import org.openhab.binding.yeelight.internal.YeelightHandlerFactory;
17 import org.openhab.binding.yeelight.internal.lib.device.DeviceBase;
18 import org.openhab.binding.yeelight.internal.lib.listeners.DeviceListener;
19 import org.openhab.binding.yeelight.internal.lib.services.DeviceManager;
20 import org.openhab.core.config.discovery.AbstractDiscoveryService;
21 import org.openhab.core.config.discovery.DiscoveryResult;
22 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
23 import org.openhab.core.config.discovery.DiscoveryService;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.ThingUID;
26 import org.osgi.service.component.annotations.Component;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
31 * The {@link YeelightDiscoveryService} is responsible for search and discovery of new devices.
33 * @author Coaster Li - Initial contribution
35 @Component(service = DiscoveryService.class, configurationPid = "discovery.yeelight")
36 public class YeelightDiscoveryService extends AbstractDiscoveryService implements DeviceListener {
38 private final Logger logger = LoggerFactory.getLogger(YeelightDiscoveryService.class.getSimpleName());
40 public YeelightDiscoveryService() {
41 super(YeelightHandlerFactory.SUPPORTED_THING_TYPES_UIDS, 2, false);
45 protected void startScan() {
46 logger.debug("Starting Scan");
47 DeviceManager.getInstance().registerDeviceListener(this);
48 DeviceManager.getInstance().startDiscovery();
52 protected synchronized void stopScan() {
53 logger.debug(": stopScan");
54 DeviceManager.getInstance().stopDiscovery();
55 DeviceManager.getInstance().unregisterDeviceListener(this);
59 public void onDeviceFound(DeviceBase device) {
60 logger.info("onDeviceFound, id: {}", device.getDeviceId());
61 ThingUID thingUID = getThingUID(device);
63 if (thingUID == null) {
64 // We don't know about this thing type
65 logger.info("Skipping device {}, unknown type.", device.getDeviceId());
69 ThingTypeUID thingTypeUID = getThingTypeUID(device);
70 String deviceName = device.getDeviceName().isEmpty() ? DeviceManager.getDefaultName(device)
71 : device.getDeviceName();
73 DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
74 .withLabel(deviceName).withProperties(device.getBulbInfo())
75 .withProperty(YeelightBindingConstants.PARAMETER_DEVICE_ID, device.getDeviceId()).build();
77 thingDiscovered(discoveryResult);
81 public void onDeviceLost(DeviceBase device) {
82 logger.debug("onDeviceLost, id: {}", device.getDeviceId());
85 private ThingUID getThingUID(DeviceBase device) {
86 switch (device.getDeviceType()) {
89 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING, device.getDeviceId());
93 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING1, device.getDeviceId());
95 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING4, device.getDeviceId());
98 return new ThingUID(YeelightBindingConstants.THING_TYPE_WONDER, device.getDeviceId());
100 return new ThingUID(YeelightBindingConstants.THING_TYPE_DOLPHIN, device.getDeviceId());
102 return new ThingUID(YeelightBindingConstants.THING_TYPE_CTBULB, device.getDeviceId());
105 return new ThingUID(YeelightBindingConstants.THING_TYPE_STRIPE, device.getDeviceId());
107 return new ThingUID(YeelightBindingConstants.THING_TYPE_DESKLAMP, device.getDeviceId());
113 private ThingTypeUID getThingTypeUID(DeviceBase device) {
114 switch (device.getDeviceType()) {
116 return YeelightBindingConstants.THING_TYPE_CEILING;
120 return YeelightBindingConstants.THING_TYPE_CEILING1;
122 return YeelightBindingConstants.THING_TYPE_CEILING3;
124 return YeelightBindingConstants.THING_TYPE_CEILING4;
127 return YeelightBindingConstants.THING_TYPE_WONDER;
129 return YeelightBindingConstants.THING_TYPE_DOLPHIN;
131 return YeelightBindingConstants.THING_TYPE_CTBULB;
134 return YeelightBindingConstants.THING_TYPE_STRIPE;
136 return YeelightBindingConstants.THING_TYPE_DESKLAMP;