2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
14 package org.openhab.binding.lutron.internal.discovery;
16 import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;
18 import java.util.HashMap;
19 import java.util.List;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.lutron.internal.LutronHandlerFactory;
25 import org.openhab.binding.lutron.internal.handler.LeapBridgeHandler;
26 import org.openhab.binding.lutron.internal.protocol.leap.dto.Area;
27 import org.openhab.binding.lutron.internal.protocol.leap.dto.Device;
28 import org.openhab.binding.lutron.internal.protocol.leap.dto.OccupancyGroup;
29 import org.openhab.core.config.discovery.AbstractDiscoveryService;
30 import org.openhab.core.config.discovery.DiscoveryResult;
31 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
32 import org.openhab.core.config.discovery.DiscoveryService;
33 import org.openhab.core.thing.ThingTypeUID;
34 import org.openhab.core.thing.ThingUID;
35 import org.openhab.core.thing.binding.ThingHandler;
36 import org.openhab.core.thing.binding.ThingHandlerService;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link LeapDeviceDiscoveryService} discovers devices paired with Lutron bridges using the LEAP protocol.
43 * @author Bob Adair - Initial contribution
46 public class LeapDeviceDiscoveryService extends AbstractDiscoveryService
47 implements DiscoveryService, ThingHandlerService {
49 private static final int DISCOVERY_SERVICE_TIMEOUT = 0; // seconds
51 private final Logger logger = LoggerFactory.getLogger(LeapDeviceDiscoveryService.class);
53 /** Area number to name map **/
54 private @Nullable Map<Integer, String> areaMap;
55 private @Nullable List<OccupancyGroup> oGroupList;
57 private @NonNullByDefault({}) LeapBridgeHandler bridgeHandler;
59 public LeapDeviceDiscoveryService() {
60 super(LutronHandlerFactory.DISCOVERABLE_DEVICE_TYPES_UIDS, DISCOVERY_SERVICE_TIMEOUT);
64 public void setThingHandler(ThingHandler handler) {
65 if (handler instanceof LeapBridgeHandler) {
66 bridgeHandler = (LeapBridgeHandler) handler;
67 bridgeHandler.setDiscoveryService(this);
72 public @Nullable ThingHandler getThingHandler() {
77 protected void startScan() {
78 logger.debug("Active discovery scan started");
79 bridgeHandler.queryDiscoveryData();
82 public void processDeviceDefinitions(List<Device> deviceList) {
83 for (Device device : deviceList) {
84 // Integer zoneid = device.getZone();
85 Integer deviceId = device.getDevice();
86 String label = device.getFullyQualifiedName();
88 logger.debug("Discovered device: {} type: {} id: {}", label, device.deviceType, deviceId);
89 if (device.deviceType != null) {
90 switch (device.deviceType) {
92 case "RA2SelectMainRepeater":
93 notifyDiscovery(THING_TYPE_VIRTUALKEYPAD, deviceId, label, "model", "Caseta");
97 notifyDiscovery(THING_TYPE_DIMMER, deviceId, label);
101 notifyDiscovery(THING_TYPE_SWITCH, deviceId, label);
103 case "CasetaFanSpeedController":
104 case "MaestroFanSpeedController":
105 notifyDiscovery(THING_TYPE_FAN, deviceId, label);
108 notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "2B");
110 case "Pico2ButtonRaiseLower":
111 notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "2BRL");
113 case "Pico3ButtonRaiseLower":
114 notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "3BRL");
116 case "SerenaRollerShade":
117 case "SerenaHoneycombShade":
118 case "TriathlonRollerShade":
119 case "TriathlonHoneycombShade":
120 case "QsWirelessShade":
121 notifyDiscovery(THING_TYPE_SHADE, deviceId, label);
123 case "RPSOccupancySensor":
124 // Don't discover sensors. Using occupancy groups instead.
127 logger.info("Unrecognized device type: {}", device.deviceType);
135 private void processOccupancyGroups() {
136 Map<Integer, String> areaMap = this.areaMap;
137 List<OccupancyGroup> oGroupList = this.oGroupList;
139 if (areaMap != null && oGroupList != null) {
140 logger.trace("Processing occupancy groups");
141 for (OccupancyGroup oGroup : oGroupList) {
142 logger.trace("Processing OccupancyGroup: {}", oGroup.href);
143 int groupNum = oGroup.getOccupancyGroup();
144 // Only process occupancy groups with associated occupancy sensors
145 if (groupNum > 0 && oGroup.associatedSensors != null) {
147 if (oGroup.associatedAreas.length > 0) {
148 // If multiple associated areas are listed, use only the first
149 areaName = areaMap.get(oGroup.associatedAreas[0].getAreaNumber());
151 areaName = "Occupancy Group";
153 logger.debug("Discovered occupancy group: {} areas: {} area name: {}", groupNum,
154 oGroup.associatedAreas.length, areaName);
155 notifyDiscovery(THING_TYPE_OGROUP, groupNum, areaName);
159 this.oGroupList = null;
163 public void setOccupancyGroups(List<OccupancyGroup> oGroupList) {
164 logger.trace("Setting occupancy groups list");
165 this.oGroupList = oGroupList;
167 if (areaMap != null) {
168 processOccupancyGroups();
172 public void setAreas(List<Area> areaList) {
173 Map<Integer, String> areaMap = new HashMap<>();
175 logger.trace("Setting areas map");
176 for (Area area : areaList) {
177 int areaNum = area.getArea();
178 logger.trace("Inserting area into map - num: {} name: {}", areaNum, area.name);
180 areaMap.put(areaNum, area.name);
182 logger.debug("Ignoring area with unparsable href {}", area.href);
185 this.areaMap = areaMap;
187 if (oGroupList != null) {
188 processOccupancyGroups();
192 private void notifyDiscovery(ThingTypeUID thingTypeUID, @Nullable Integer integrationId, String label,
193 @Nullable String propName, @Nullable Object propValue) {
194 if (integrationId == null) {
195 logger.debug("Discovered {} with no integration ID", label);
198 ThingUID bridgeUID = this.bridgeHandler.getThing().getUID();
199 ThingUID uid = new ThingUID(thingTypeUID, bridgeUID, integrationId.toString());
201 Map<String, Object> properties = new HashMap<>();
203 properties.put(INTEGRATION_ID, integrationId);
204 if (propName != null && propValue != null) {
205 properties.put(propName, propValue);
208 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withLabel(label)
209 .withProperties(properties).withRepresentationProperty(INTEGRATION_ID).build();
210 thingDiscovered(result);
211 logger.trace("Discovered {}", uid);
214 private void notifyDiscovery(ThingTypeUID thingTypeUID, Integer integrationId, String label) {
215 notifyDiscovery(thingTypeUID, integrationId, label, null, null);
219 public void deactivate() {