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.caddx.internal.discovery;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.caddx.internal.CaddxBindingConstants;
20 import org.openhab.binding.caddx.internal.CaddxEvent;
21 import org.openhab.binding.caddx.internal.config.CaddxKeypadConfiguration;
22 import org.openhab.binding.caddx.internal.config.CaddxPartitionConfiguration;
23 import org.openhab.binding.caddx.internal.config.CaddxZoneConfiguration;
24 import org.openhab.binding.caddx.internal.handler.CaddxBridgeHandler;
25 import org.openhab.binding.caddx.internal.handler.CaddxThingType;
26 import org.openhab.core.config.discovery.AbstractDiscoveryService;
27 import org.openhab.core.config.discovery.DiscoveryResult;
28 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
29 import org.openhab.core.config.discovery.DiscoveryService;
30 import org.openhab.core.thing.Bridge;
31 import org.openhab.core.thing.ThingUID;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.openhab.core.thing.binding.ThingHandlerService;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * This class is responsible for discovering the supported Things.
40 * @author Georgios Moutsos - Initial contribution
43 public class CaddxDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService, DiscoveryService {
44 private final Logger logger = LoggerFactory.getLogger(CaddxDiscoveryService.class);
46 private @Nullable CaddxBridgeHandler caddxBridgeHandler = null;
48 public CaddxDiscoveryService() {
49 super(CaddxBindingConstants.SUPPORTED_THING_TYPES_UIDS, 15, false);
53 protected void startScan() {
54 // Discovery is performed implicitly via the CadxBridgeHandler
58 * Method to add a Thing to the Inbox.
61 * @param caddxThingType
64 public void addThing(Bridge bridge, CaddxThingType caddxThingType, CaddxEvent event) {
65 ThingUID thingUID = null;
67 String thingLabel = "";
68 Map<String, Object> properties = null;
70 Integer partition = event.getPartition();
71 Integer zone = event.getZone();
72 Integer keypad = event.getKeypad();
73 String representationProperty = null;
75 switch (caddxThingType) {
79 thingUID = new ThingUID(CaddxBindingConstants.PANEL_THING_TYPE, bridge.getUID(), thingID);
82 thingID = "partition" + partition;
83 thingLabel = "Partition " + partition;
84 thingUID = new ThingUID(CaddxBindingConstants.PARTITION_THING_TYPE, bridge.getUID(), thingID);
86 if (partition != null) {
87 properties = Map.of(CaddxPartitionConfiguration.PARTITION_NUMBER, partition);
88 representationProperty = CaddxPartitionConfiguration.PARTITION_NUMBER;
93 thingID = "zone" + zone;
94 thingLabel = "Zone " + zone;
95 thingUID = new ThingUID(CaddxBindingConstants.ZONE_THING_TYPE, bridge.getUID(), thingID);
98 properties = Map.of(CaddxZoneConfiguration.ZONE_NUMBER, zone);
99 representationProperty = CaddxZoneConfiguration.ZONE_NUMBER;
104 thingLabel = "Keypad";
105 thingUID = new ThingUID(CaddxBindingConstants.KEYPAD_THING_TYPE, bridge.getUID(), thingID);
107 if (keypad != null) {
108 properties = Map.of(CaddxKeypadConfiguration.KEYPAD_ADDRESS, keypad);
109 representationProperty = CaddxKeypadConfiguration.KEYPAD_ADDRESS;
114 if (thingUID != null) {
115 DiscoveryResult discoveryResult;
117 if (properties != null && representationProperty != null) {
118 discoveryResult = DiscoveryResultBuilder.create(thingUID).withProperties(properties)
119 .withRepresentationProperty(representationProperty).withBridge(bridge.getUID())
120 .withLabel(thingLabel).build();
122 discoveryResult = DiscoveryResultBuilder.create(thingUID).withBridge(bridge.getUID())
123 .withLabel(thingLabel).build();
126 thingDiscovered(discoveryResult);
128 logger.warn("addThing(): Unable to Add Caddx Alarm Thing to Inbox!");
133 * Activates the Discovery Service.
136 public void activate() {
137 CaddxBridgeHandler handler = caddxBridgeHandler;
138 if (handler != null) {
139 handler.registerDiscoveryService(this);
144 * Deactivates the Discovery Service.
147 public void deactivate() {
148 CaddxBridgeHandler handler = caddxBridgeHandler;
149 if (handler != null) {
150 handler.unregisterDiscoveryService();
155 public void setThingHandler(@Nullable ThingHandler handler) {
156 if (handler instanceof CaddxBridgeHandler bridgeHandler) {
157 caddxBridgeHandler = bridgeHandler;
162 public @Nullable ThingHandler getThingHandler() {
163 return caddxBridgeHandler;