]> git.basschouten.com Git - openhab-addons.git/blob
bbb6aba467773ccab10ae2b668dfffb6dac67a76
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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                 return new ThingUID(YeelightBindingConstants.THING_TYPE_WONDER, device.getDeviceId());
96             case mono:
97                 return new ThingUID(YeelightBindingConstants.THING_TYPE_DOLPHIN, device.getDeviceId());
98             case ct_bulb:
99                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CTBULB, device.getDeviceId());
100             case stripe:
101                 return new ThingUID(YeelightBindingConstants.THING_TYPE_STRIPE, device.getDeviceId());
102             case desklamp:
103                 return new ThingUID(YeelightBindingConstants.THING_TYPE_DESKLAMP, device.getDeviceId());
104             default:
105                 return null;
106         }
107     }
108
109     private ThingTypeUID getThingTypeUID(DeviceBase device) {
110         switch (device.getDeviceType()) {
111             case ceiling:
112                 return YeelightBindingConstants.THING_TYPE_CEILING;
113             case ceiling1:
114                 return YeelightBindingConstants.THING_TYPE_CEILING1;
115             case ceiling3:
116                 return YeelightBindingConstants.THING_TYPE_CEILING3;
117             case ceiling4:
118                 return YeelightBindingConstants.THING_TYPE_CEILING4;
119             case color:
120                 return YeelightBindingConstants.THING_TYPE_WONDER;
121             case mono:
122                 return YeelightBindingConstants.THING_TYPE_DOLPHIN;
123             case ct_bulb:
124                 return YeelightBindingConstants.THING_TYPE_CTBULB;
125             case stripe:
126                 return YeelightBindingConstants.THING_TYPE_STRIPE;
127             case desklamp:
128                 return YeelightBindingConstants.THING_TYPE_DESKLAMP;
129             default:
130                 return null;
131         }
132     }
133 }