2 * Copyright (c) 2010-2024 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.hw;
15 import java.util.HashMap;
17 import java.util.concurrent.atomic.AtomicBoolean;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lutron.internal.LutronHandlerFactory;
22 import org.openhab.core.config.discovery.AbstractDiscoveryService;
23 import org.openhab.core.config.discovery.DiscoveryResult;
24 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
25 import org.openhab.core.config.discovery.DiscoveryService;
26 import org.openhab.core.thing.ThingUID;
27 import org.openhab.core.thing.binding.ThingHandler;
28 import org.openhab.core.thing.binding.ThingHandlerService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
34 * The Discovery Service for Lutron HomeWorks processors. There is no great way to automatically
35 * discover modules in the legacy HomeWorks processor (that I know of) so this service simply iterates
36 * through possible addresses and asks for status on that address. If it's a valid module, the processor will return
37 * with the dimmer status and it will be discovered.
39 * @author Andrew Shilliday - Initial contribution
41 public class HwDiscoveryService extends AbstractDiscoveryService implements DiscoveryService, ThingHandlerService {
42 private Logger logger = LoggerFactory.getLogger(HwDiscoveryService.class);
44 private final AtomicBoolean isScanning = new AtomicBoolean(false);
46 private @NonNullByDefault({}) HwSerialBridgeHandler handler;
48 public HwDiscoveryService() {
49 super(LutronHandlerFactory.HW_DISCOVERABLE_DEVICE_TYPES_UIDS, 10);
53 public void setThingHandler(@Nullable ThingHandler handler) {
54 if (handler instanceof HwSerialBridgeHandler bridgeHandler) {
55 this.handler = bridgeHandler;
56 this.handler.setDiscoveryService(this);
61 public @Nullable ThingHandler getThingHandler() {
66 public void activate() {
71 public void deactivate() {
76 protected void startScan() {
77 scheduler.submit(() -> {
78 if (isScanning.compareAndSet(false, true)) {
80 logger.debug("Starting scan for HW Dimmers");
81 for (int m = 1; m <= 8; m++) { // Modules
82 for (int o = 1; o <= 4; o++) { // Outputs
83 String address = String.format("[01:01:00:%02d:%02d]", m, o);
84 handler.sendCommand("RDL, " + address);
88 } catch (InterruptedException e) {
89 logger.debug("Scan interrupted");
91 isScanning.set(false);
98 * Called by the bridge when it receives a status update for a dimmer that is not registered.
100 public void declareUnknownDimmer(String address) {
101 if (address == null) {
102 logger.info("Discovered HomeWorks dimmer with no address");
105 String addressUid = address.replaceAll("[\\[\\]]", "").replace(":", "-");
106 ThingUID bridgeUID = this.handler.getThing().getUID();
107 ThingUID uid = new ThingUID(HwConstants.THING_TYPE_HWDIMMER, bridgeUID, addressUid);
109 Map<String, Object> props = new HashMap<>();
111 props.put("address", address);
112 DiscoveryResult result = DiscoveryResultBuilder.create(uid).withBridge(bridgeUID).withProperties(props)
113 .withRepresentationProperty("address").build();
115 thingDiscovered(result);
117 logger.debug("Discovered {}", uid);