2 * Copyright (c) 2010-2023 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
13 package org.openhab.binding.lutron.internal.discovery;
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.*;
17 import java.util.HashMap;
18 import java.util.List;
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;
40 * The {@link LeapDeviceDiscoveryService} discovers devices paired with Lutron bridges using the LEAP protocol.
42 * @author Bob Adair - Initial contribution
45 public class LeapDeviceDiscoveryService extends AbstractDiscoveryService
46 implements DiscoveryService, ThingHandlerService {
48 private static final int DISCOVERY_SERVICE_TIMEOUT = 0; // seconds
50 private final Logger logger = LoggerFactory.getLogger(LeapDeviceDiscoveryService.class);
52 /** Area number to name map **/
53 private @Nullable Map<Integer, String> areaMap;
54 private @Nullable List<OccupancyGroup> oGroupList;
56 private @NonNullByDefault({}) LeapBridgeHandler bridgeHandler;
58 public LeapDeviceDiscoveryService() {
59 super(LutronHandlerFactory.DISCOVERABLE_DEVICE_TYPES_UIDS, DISCOVERY_SERVICE_TIMEOUT);
63 public void setThingHandler(ThingHandler handler) {
64 if (handler instanceof LeapBridgeHandler) {
65 bridgeHandler = (LeapBridgeHandler) handler;
66 bridgeHandler.setDiscoveryService(this);
71 public @Nullable ThingHandler getThingHandler() {
76 protected void startScan() {
77 logger.debug("Active discovery scan started");
78 bridgeHandler.queryDiscoveryData();
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();
87 logger.debug("Discovered device: {} type: {} id: {}", label, device.deviceType, deviceId);
88 if (device.deviceType != null) {
89 switch (device.deviceType) {
91 case "RA2SelectMainRepeater":
92 notifyDiscovery(THING_TYPE_VIRTUALKEYPAD, deviceId, label, "model", "Caseta");
96 notifyDiscovery(THING_TYPE_DIMMER, deviceId, label);
100 notifyDiscovery(THING_TYPE_SWITCH, deviceId, label);
102 case "CasetaFanSpeedController":
103 case "MaestroFanSpeedController":
104 notifyDiscovery(THING_TYPE_FAN, deviceId, label);
107 notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "2B");
109 case "Pico2ButtonRaiseLower":
110 notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "2BRL");
112 case "Pico3ButtonRaiseLower":
113 notifyDiscovery(THING_TYPE_PICO, deviceId, label, "model", "3BRL");
115 case "SerenaRollerShade":
116 case "SerenaHoneycombShade":
117 case "TriathlonRollerShade":
118 case "TriathlonHoneycombShade":
119 case "QsWirelessShade":
120 notifyDiscovery(THING_TYPE_SHADE, deviceId, label);
122 case "RPSOccupancySensor":
123 // Don't discover sensors. Using occupancy groups instead.
126 logger.info("Unrecognized device type: {}", device.deviceType);
134 private void processOccupancyGroups() {
135 Map<Integer, String> areaMap = this.areaMap;
136 List<OccupancyGroup> oGroupList = this.oGroupList;
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) {
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());
150 areaName = "Occupancy Group";
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);
160 this.oGroupList = null;
164 public void setOccupancyGroups(List<OccupancyGroup> oGroupList) {
165 logger.trace("Setting occupancy groups list");
166 this.oGroupList = oGroupList;
168 if (areaMap != null) {
169 processOccupancyGroups();
173 public void setAreas(List<Area> areaList) {
174 Map<Integer, String> areaMap = new HashMap<>();
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);
181 areaMap.put(areaNum, area.name);
183 logger.debug("Ignoring area with unparsable href {}", area.href);
186 this.areaMap = areaMap;
188 if (oGroupList != null) {
189 processOccupancyGroups();
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);
199 ThingUID bridgeUID = this.bridgeHandler.getThing().getUID();
200 ThingUID uid = new ThingUID(thingTypeUID, bridgeUID, integrationId.toString());
202 Map<String, Object> properties = new HashMap<>();
204 properties.put(INTEGRATION_ID, integrationId);
205 if (propName != null && propValue != null) {
206 properties.put(propName, propValue);
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);
215 private void notifyDiscovery(ThingTypeUID thingTypeUID, Integer integrationId, String label) {
216 notifyDiscovery(thingTypeUID, integrationId, label, null, null);
220 public void deactivate() {