]> git.basschouten.com Git - openhab-addons.git/blob
3ea2add766aa06c68558f62b7db67e4c125fcd0e
[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.lutron.internal.discovery;
14
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.eclipse.jdt.annotation.Nullable;
23 import org.openhab.binding.lutron.internal.LutronHandlerFactory;
24 import org.openhab.binding.lutron.internal.handler.LeapBridgeHandler;
25 import org.openhab.binding.lutron.internal.protocol.leap.dto.Area;
26 import org.openhab.binding.lutron.internal.protocol.leap.dto.Device;
27 import org.openhab.binding.lutron.internal.protocol.leap.dto.OccupancyGroup;
28 import org.openhab.core.config.discovery.AbstractDiscoveryService;
29 import org.openhab.core.config.discovery.DiscoveryResult;
30 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
31 import org.openhab.core.config.discovery.DiscoveryService;
32 import org.openhab.core.thing.ThingTypeUID;
33 import org.openhab.core.thing.ThingUID;
34 import org.openhab.core.thing.binding.ThingHandler;
35 import org.openhab.core.thing.binding.ThingHandlerService;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * The {@link LeapDeviceDiscoveryService} discovers devices paired with Lutron bridges using the LEAP protocol.
41  *
42  * @author Bob Adair - Initial contribution
43  */
44 @NonNullByDefault
45 public class LeapDeviceDiscoveryService extends AbstractDiscoveryService
46         implements DiscoveryService, ThingHandlerService {
47
48     private static final int DISCOVERY_SERVICE_TIMEOUT = 0; // seconds
49
50     private final Logger logger = LoggerFactory.getLogger(LeapDeviceDiscoveryService.class);
51
52     /** Area number to name map **/
53     private @Nullable Map<Integer, String> areaMap;
54     private @Nullable List<OccupancyGroup> oGroupList;
55
56     private @NonNullByDefault({}) LeapBridgeHandler bridgeHandler;
57
58     public LeapDeviceDiscoveryService() {
59         super(LutronHandlerFactory.DISCOVERABLE_DEVICE_TYPES_UIDS, DISCOVERY_SERVICE_TIMEOUT);
60     }
61
62     @Override
63     public void setThingHandler(ThingHandler handler) {
64         if (handler instanceof LeapBridgeHandler) {
65             bridgeHandler = (LeapBridgeHandler) handler;
66             bridgeHandler.setDiscoveryService(this);
67         }
68     }
69
70     @Override
71     public @Nullable ThingHandler getThingHandler() {
72         return bridgeHandler;
73     }
74
75     @Override
76     protected void startScan() {
77         logger.debug("Active discovery scan started");
78         bridgeHandler.queryDiscoveryData();
79     }
80
81     public void processDeviceDefinitions(List<Device> deviceList) {
82         for (Device device : deviceList) {
83             // Integer zoneid = device.getZone();
84             Integer deviceId = device.getDevice();
85             String label = device.getFullyQualifiedName();
86             if (deviceId > 0) {
87                 logger.debug("Discovered device: {} type: {} id: {}", label, device.deviceType, deviceId);
88                 if (device.deviceType != null) {
89                     switch (device.deviceType) {
90                         case "SmartBridge":
91                         case "RA2SelectMainRepeater":
92                             notifyDiscovery(THING_TYPE_VIRTUALKEYPAD, deviceId, label, "model", "Caseta");
93                             break;
94                         case "WallDimmer":
95                         case "PlugInDimmer":
96                             notifyDiscovery(THING_TYPE_DIMMER, deviceId, label);
97                             break;
98                         case "WallSwitch":
99                         case "PlugInSwitch":
100                             notifyDiscovery(THING_TYPE_SWITCH, deviceId, label);
101                             break;
102                         case "CasetaFanSpeedController":
103                         case "MaestroFanSpeedController":
104                             notifyDiscovery(THING_TYPE_FAN, deviceId, label);
105                             break;
106                         case "Pico2Button":
107                             notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "2B");
108                             break;
109                         case "Pico2ButtonRaiseLower":
110                             notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "2BRL");
111                             break;
112                         case "Pico3ButtonRaiseLower":
113                             notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "3BRL");
114                             break;
115                         case "SerenaRollerShade":
116                         case "SerenaHoneycombShade":
117                         case "TriathlonRollerShade":
118                         case "TriathlonHoneycombShade":
119                         case "QsWirelessShade":
120                             notifyDiscovery(THING_TYPE_SHADE, deviceId, label);
121                             break;
122                         case "RPSOccupancySensor":
123                             // Don't discover sensors. Using occupancy groups instead.
124                             break;
125                         default:
126                             logger.info("Unrecognized device type: {}", device.deviceType);
127                             break;
128                     }
129                 }
130             }
131         }
132     }
133
134     private void processOccupancyGroups() {
135         Map<Integer, String> areaMap = this.areaMap;
136         List<OccupancyGroup> oGroupList = this.oGroupList;
137
138         if (areaMap != null && oGroupList != null) {
139             logger.trace("Processing occupancy groups");
140             for (OccupancyGroup oGroup : oGroupList) {
141                 logger.trace("Processing OccupancyGroup: {}", oGroup.href);
142                 int groupNum = oGroup.getOccupancyGroup();
143                 // Only process occupancy groups with associated occupancy sensors
144                 if (groupNum > 0 && oGroup.associatedSensors != null) {
145                     String areaName;
146                     if (oGroup.associatedAreas.length > 0) {
147                         // If multiple associated areas are listed, use only the first
148                         areaName = areaMap.get(oGroup.associatedAreas[0].getAreaNumber());
149                     } else {
150                         areaName = "Occupancy Group";
151                     }
152                     if (areaName != null) {
153                         logger.debug("Discovered occupancy group: {} areas: {} area name: {}", groupNum,
154                                 oGroup.associatedAreas.length, areaName);
155                         notifyDiscovery(THING_TYPE_OGROUP, groupNum, areaName);
156                     }
157                 }
158             }
159             this.areaMap = null;
160             this.oGroupList = null;
161         }
162     }
163
164     public void setOccupancyGroups(List<OccupancyGroup> oGroupList) {
165         logger.trace("Setting occupancy groups list");
166         this.oGroupList = oGroupList;
167
168         if (areaMap != null) {
169             processOccupancyGroups();
170         }
171     }
172
173     public void setAreas(List<Area> areaList) {
174         Map<Integer, String> areaMap = new HashMap<>();
175
176         logger.trace("Setting areas map");
177         for (Area area : areaList) {
178             int areaNum = area.getArea();
179             logger.trace("Inserting area into map - num: {} name: {}", areaNum, area.name);
180             if (areaNum > 0) {
181                 areaMap.put(areaNum, area.name);
182             } else {
183                 logger.debug("Ignoring area with unparsable href {}", area.href);
184             }
185         }
186         this.areaMap = areaMap;
187
188         if (oGroupList != null) {
189             processOccupancyGroups();
190         }
191     }
192
193     private void notifyDiscovery(ThingTypeUID thingTypeUID, @Nullable Integer integrationId, String label,
194             @Nullable String propName, @Nullable Object propValue) {
195         if (integrationId == null) {
196             logger.debug("Discovered {} with no integration ID", label);
197             return;
198         }
199         ThingUID bridgeUID = this.bridgeHandler.getThing().getUID();
200         ThingUID uid = new ThingUID(thingTypeUID, bridgeUID, integrationId.toString());
201
202         Map<String, Object> properties = new HashMap<>();
203
204         properties.put(INTEGRATION_ID, integrationId);
205         if (propName != null && propValue != null) {
206             properties.put(propName, propValue);
207         }
208
209         DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withLabel(label)
210                 .withProperties(properties).withRepresentationProperty(INTEGRATION_ID).build();
211         thingDiscovered(result);
212         logger.trace("Discovered {}", uid);
213     }
214
215     private void notifyDiscovery(ThingTypeUID thingTypeUID, Integer integrationId, String label) {
216         notifyDiscovery(thingTypeUID, integrationId, label, null, null);
217     }
218
219     @Override
220     public void deactivate() {
221         super.deactivate();
222     }
223 }