]> git.basschouten.com Git - openhab-addons.git/blob
f43f7230d25267e3ddbf9271eeac846994c8ced4
[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             case ceil26:
92             case ceiling11:
93                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING1, device.getDeviceId());
94             case ceiling4:
95                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CEILING4, device.getDeviceId());
96             case color:
97             case color4:
98                 return new ThingUID(YeelightBindingConstants.THING_TYPE_WONDER, device.getDeviceId());
99             case mono:
100                 return new ThingUID(YeelightBindingConstants.THING_TYPE_DOLPHIN, device.getDeviceId());
101             case ct_bulb:
102                 return new ThingUID(YeelightBindingConstants.THING_TYPE_CTBULB, device.getDeviceId());
103             case stripe:
104             case strip6:
105                 return new ThingUID(YeelightBindingConstants.THING_TYPE_STRIPE, device.getDeviceId());
106             case desklamp:
107                 return new ThingUID(YeelightBindingConstants.THING_TYPE_DESKLAMP, device.getDeviceId());
108             default:
109                 return null;
110         }
111     }
112
113     private ThingTypeUID getThingTypeUID(DeviceBase device) {
114         switch (device.getDeviceType()) {
115             case ceiling:
116                 return YeelightBindingConstants.THING_TYPE_CEILING;
117             case ceiling1:
118             case ceil26:
119             case ceiling11:
120                 return YeelightBindingConstants.THING_TYPE_CEILING1;
121             case ceiling3:
122                 return YeelightBindingConstants.THING_TYPE_CEILING3;
123             case ceiling4:
124                 return YeelightBindingConstants.THING_TYPE_CEILING4;
125             case color:
126             case color4:
127                 return YeelightBindingConstants.THING_TYPE_WONDER;
128             case mono:
129                 return YeelightBindingConstants.THING_TYPE_DOLPHIN;
130             case ct_bulb:
131                 return YeelightBindingConstants.THING_TYPE_CTBULB;
132             case stripe:
133             case strip6:
134                 return YeelightBindingConstants.THING_TYPE_STRIPE;
135             case desklamp:
136                 return YeelightBindingConstants.THING_TYPE_DESKLAMP;
137             default:
138                 return null;
139         }
140     }
141 }