]> git.basschouten.com Git - openhab-addons.git/blob
d8bd40fee478fbf26513884285630a5fa0bb2004
[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.yeelight.internal.discovery;
14
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;
29
30 /**
31  * The {@link YeelightDiscoveryService} is responsible for search and discovery of new devices.
32  *
33  * @author Coaster Li - Initial contribution
34  */
35 @Component(service = DiscoveryService.class, configurationPid = "discovery.yeelight")
36 public class YeelightDiscoveryService extends AbstractDiscoveryService implements DeviceListener {
37
38     private final Logger logger = LoggerFactory.getLogger(YeelightDiscoveryService.class.getSimpleName());
39
40     public YeelightDiscoveryService() {
41         super(YeelightHandlerFactory.SUPPORTED_THING_TYPES_UIDS, 2, false);
42     }
43
44     @Override
45     protected void startScan() {
46         logger.debug("Starting Scan");
47         DeviceManager.getInstance().registerDeviceListener(this);
48         DeviceManager.getInstance().startDiscovery();
49     }
50
51     @Override
52     protected synchronized void stopScan() {
53         logger.debug(": stopScan");
54         DeviceManager.getInstance().stopDiscovery();
55         DeviceManager.getInstance().unregisterDeviceListener(this);
56     }
57
58     @Override
59     public void onDeviceFound(DeviceBase device) {
60         logger.info("onDeviceFound, id: {}", device.getDeviceId());
61         ThingUID thingUID = getThingUID(device);
62
63         if (thingUID == null) {
64             // We don't know about this thing type
65             logger.info("Skipping device {}, unknown type.", device.getDeviceId());
66             return;
67         }
68
69         ThingTypeUID thingTypeUID = getThingTypeUID(device);
70         String deviceName = device.getDeviceName().isEmpty() ? DeviceManager.getDefaultName(device)
71                 : device.getDeviceName();
72
73         DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
74                 .withLabel(deviceName).withProperties(device.getBulbInfo())
75                 .withProperty(YeelightBindingConstants.PARAMETER_DEVICE_ID, device.getDeviceId()).build();
76
77         thingDiscovered(discoveryResult);
78     }
79
80     @Override
81     public void onDeviceLost(DeviceBase device) {
82         logger.debug("onDeviceLost, id: {}", device.getDeviceId());
83     }
84
85     private ThingUID getThingUID(DeviceBase device) {
86         switch (device.getDeviceType()) {
87             case ceiling:
88             case ceiling3:
89                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING, device.getDeviceId());
90             case ceiling1:
91                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING1, device.getDeviceId());
92             case ceiling4:
93                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING4, device.getDeviceId());
94             case color:
95             case color4:
96                 return new ThingUID(YeelightBindingConstants.THING_TYPE_WONDER, device.getDeviceId());
97             case mono:
98                 return new ThingUID(YeelightBindingConstants.THING_TYPE_DOLPHIN, device.getDeviceId());
99             case ct_bulb:
100                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CTBULB, device.getDeviceId());
101             case stripe:
102                 return new ThingUID(YeelightBindingConstants.THING_TYPE_STRIPE, device.getDeviceId());
103             case desklamp:
104                 return new ThingUID(YeelightBindingConstants.THING_TYPE_DESKLAMP, device.getDeviceId());
105             default:
106                 return null;
107         }
108     }
109
110     private ThingTypeUID getThingTypeUID(DeviceBase device) {
111         switch (device.getDeviceType()) {
112             case ceiling:
113                 return YeelightBindingConstants.THING_TYPE_CEILING;
114             case ceiling1:
115                 return YeelightBindingConstants.THING_TYPE_CEILING1;
116             case ceiling3:
117                 return YeelightBindingConstants.THING_TYPE_CEILING3;
118             case ceiling4:
119                 return YeelightBindingConstants.THING_TYPE_CEILING4;
120             case color:
121             case color4:
122                 return YeelightBindingConstants.THING_TYPE_WONDER;
123             case mono:
124                 return YeelightBindingConstants.THING_TYPE_DOLPHIN;
125             case ct_bulb:
126                 return YeelightBindingConstants.THING_TYPE_CTBULB;
127             case stripe:
128                 return YeelightBindingConstants.THING_TYPE_STRIPE;
129             case desklamp:
130                 return YeelightBindingConstants.THING_TYPE_DESKLAMP;
131             default:
132                 return null;
133         }
134     }
135 }