|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2026 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.unifiaccess.internal; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +import org.eclipse.jdt.annotation.NonNullByDefault; |
| 20 | +import org.openhab.binding.unifiaccess.internal.api.UniFiAccessApiClient; |
| 21 | +import org.openhab.binding.unifiaccess.internal.dto.Device; |
| 22 | +import org.openhab.binding.unifiaccess.internal.dto.Door; |
| 23 | +import org.openhab.binding.unifiaccess.internal.dto.UniFiAccessApiException; |
| 24 | +import org.openhab.binding.unifiaccess.internal.handler.UnifiAccessBridgeHandler; |
| 25 | +import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService; |
| 26 | +import org.openhab.core.config.discovery.DiscoveryResult; |
| 27 | +import org.openhab.core.config.discovery.DiscoveryResultBuilder; |
| 28 | +import org.openhab.core.thing.ThingUID; |
| 29 | +import org.openhab.core.thing.binding.ThingHandler; |
| 30 | +import org.osgi.service.component.annotations.Component; |
| 31 | +import org.osgi.service.component.annotations.ServiceScope; |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +/** |
| 36 | + * Discovery service for UniFi Access Door things. |
| 37 | + * |
| 38 | + * @author Dan Cunningham - Initial contribution |
| 39 | + */ |
| 40 | +@Component(scope = ServiceScope.PROTOTYPE, service = UnifiAccessDiscoveryService.class) |
| 41 | +@NonNullByDefault |
| 42 | +public class UnifiAccessDiscoveryService extends AbstractThingHandlerDiscoveryService<UnifiAccessBridgeHandler> { |
| 43 | + |
| 44 | + private final Logger logger = LoggerFactory.getLogger(UnifiAccessDiscoveryService.class); |
| 45 | + |
| 46 | + public UnifiAccessDiscoveryService() { |
| 47 | + super(UnifiAccessBridgeHandler.class, |
| 48 | + Set.of(UnifiAccessBindingConstants.DOOR_THING_TYPE, UnifiAccessBindingConstants.DEVICE_THING_TYPE), 30, |
| 49 | + false); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void setThingHandler(ThingHandler handler) { |
| 54 | + if (handler instanceof UnifiAccessBridgeHandler childDiscoveryHandler) { |
| 55 | + childDiscoveryHandler.setDiscoveryService(this); |
| 56 | + this.thingHandler = childDiscoveryHandler; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void startScan() { |
| 62 | + removeOlderResults(getTimestampOfLastScan()); |
| 63 | + final UniFiAccessApiClient client = thingHandler.getApiClient(); |
| 64 | + if (client == null) { |
| 65 | + return; |
| 66 | + } |
| 67 | + try { |
| 68 | + List<Door> doors = client.getDoors(); |
| 69 | + discoverDoors(doors); |
| 70 | + List<Device> devices = client.getDevices(); |
| 71 | + discoverDevices(devices); |
| 72 | + } catch (UniFiAccessApiException e) { |
| 73 | + logger.debug("Error discovering doors: {}", e.getMessage()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public void discoverDoors(List<Door> doors) { |
| 78 | + for (Door d : doors) { |
| 79 | + ThingUID uid = new ThingUID(UnifiAccessBindingConstants.DOOR_THING_TYPE, thingHandler.getThing().getUID(), |
| 80 | + d.id); |
| 81 | + Map<String, Object> props = Map.of(UnifiAccessBindingConstants.CONFIG_DEVICE_ID, d.id); |
| 82 | + DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(thingHandler.getThing().getUID()) |
| 83 | + .withThingType(UnifiAccessBindingConstants.DOOR_THING_TYPE).withProperties(props) |
| 84 | + .withRepresentationProperty(UnifiAccessBindingConstants.CONFIG_DEVICE_ID) |
| 85 | + .withLabel("UniFi Door: " + (d.fullName != null ? d.fullName : d.name)).build(); |
| 86 | + thingDiscovered(result); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + public void discoverDevices(List<Device> devices) { |
| 91 | + for (Device d : devices) { |
| 92 | + String devId = d.id; |
| 93 | + if (devId == null || d.isHub()) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + ThingUID uid = new ThingUID(UnifiAccessBindingConstants.DEVICE_THING_TYPE, thingHandler.getThing().getUID(), |
| 97 | + devId); |
| 98 | + Map<String, Object> props = Map.of(UnifiAccessBindingConstants.CONFIG_DEVICE_ID, devId); |
| 99 | + String label = "UniFi Device: " + (d.type != null ? d.type : "") + " - " |
| 100 | + + (d.alias != null ? d.alias : (d.name != null ? d.name : devId)); |
| 101 | + DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(thingHandler.getThing().getUID()) |
| 102 | + .withThingType(UnifiAccessBindingConstants.DEVICE_THING_TYPE).withProperties(props) |
| 103 | + .withRepresentationProperty(UnifiAccessBindingConstants.CONFIG_DEVICE_ID).withLabel(label).build(); |
| 104 | + thingDiscovered(result); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments