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