2 * Copyright (c) 2010-2022 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.openwebnet.internal.discovery;
15 import java.util.HashMap;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.openwebnet.internal.OpenWebNetBindingConstants;
22 import org.openhab.binding.openwebnet.internal.handler.OpenWebNetBridgeHandler;
23 import org.openhab.core.config.discovery.AbstractDiscoveryService;
24 import org.openhab.core.config.discovery.DiscoveryResult;
25 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
26 import org.openhab.core.config.discovery.DiscoveryService;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.thing.ThingUID;
29 import org.openhab.core.thing.binding.ThingHandler;
30 import org.openhab.core.thing.binding.ThingHandlerService;
31 import org.openwebnet4j.OpenDeviceType;
32 import org.openwebnet4j.message.BaseOpenMessage;
33 import org.openwebnet4j.message.Where;
34 import org.openwebnet4j.message.WhereZigBee;
35 import org.openwebnet4j.message.Who;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
40 * The {@link OpenWebNetDeviceDiscoveryService} is responsible for discovering OpenWebNet devices connected to a
43 * @author Massimo Valla - Initial contribution
44 * @author Andrea Conte - Energy management, Thermoregulation
45 * @author Gilberto Cocchi - Thermoregulation
46 * @author Giovanni Fabiani - Aux support
49 public class OpenWebNetDeviceDiscoveryService extends AbstractDiscoveryService
50 implements DiscoveryService, ThingHandlerService {
52 private final Logger logger = LoggerFactory.getLogger(OpenWebNetDeviceDiscoveryService.class);
54 private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = OpenWebNetBindingConstants.DEVICE_SUPPORTED_THING_TYPES;
55 private static final int SEARCH_TIME_SEC = 60;
57 private @NonNullByDefault({}) OpenWebNetBridgeHandler bridgeHandler;
58 private @NonNullByDefault({}) ThingUID bridgeUID;
60 public OpenWebNetDeviceDiscoveryService() {
61 super(SUPPORTED_THING_TYPES, SEARCH_TIME_SEC);
65 public Set<ThingTypeUID> getSupportedThingTypes() {
66 return SUPPORTED_THING_TYPES;
70 protected void startScan() {
71 logger.info("------ SEARCHING for DEVICES on bridge '{}' ({}) ...", bridgeHandler.getThing().getLabel(),
73 bridgeHandler.searchDevices();
77 protected void stopScan() {
78 logger.debug("------ stopScan() on bridge '{}'", bridgeUID);
79 bridgeHandler.scanStopped();
83 public void abortScan() {
84 logger.debug("------ abortScan() on bridge '{}'", bridgeUID);
85 bridgeHandler.scanStopped();
89 * Create and notify to Inbox a new DiscoveryResult based on WHERE, OpenDeviceType and BaseOpenMessage
91 * @param where the discovered device's address (WHERE)
92 * @param deviceType {@link OpenDeviceType} of the discovered device
93 * @param message the OWN message received that identified the device (optional)
95 public void newDiscoveryResult(Where where, OpenDeviceType deviceType, @Nullable BaseOpenMessage baseMsg) {
96 logger.info("newDiscoveryResult() WHERE={}, deviceType={}", where, deviceType);
97 ThingTypeUID thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_GENERIC_DEVICE; // generic device
98 String thingLabel = OpenWebNetBindingConstants.THING_LABEL_GENERIC_DEVICE;
99 Who deviceWho = Who.UNKNOWN;
100 switch (deviceType) {
101 case ZIGBEE_ON_OFF_SWITCH:
102 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH;
103 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_ON_OFF_SWITCH;
104 deviceWho = Who.LIGHTING;
106 case ZIGBEE_DIMMER_SWITCH:
107 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_DIMMER;
108 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_DIMMER;
109 deviceWho = Who.LIGHTING;
111 case SCS_ON_OFF_SWITCH:
112 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_ON_OFF_SWITCH;
113 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_ON_OFF_SWITCH;
114 deviceWho = Who.LIGHTING;
116 case SCS_DIMMER_SWITCH:
117 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_DIMMER;
118 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_DIMMER;
119 deviceWho = Who.LIGHTING;
121 case SCS_SHUTTER_SWITCH:
122 case SCS_SHUTTER_CONTROL: {
123 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_AUTOMATION;
124 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_AUTOMATION;
125 deviceWho = Who.AUTOMATION;
128 case ZIGBEE_SHUTTER_SWITCH:
129 case ZIGBEE_SHUTTER_CONTROL: {
130 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_AUTOMATION;
131 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_AUTOMATION;
132 deviceWho = Who.AUTOMATION;
135 case SCS_THERMO_SENSOR: {
136 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_THERMO_SENSOR;
137 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_THERMO_SENSOR;
138 deviceWho = Who.THERMOREGULATION;
141 case SCS_THERMO_ZONE: {
142 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_THERMO_ZONE;
143 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_THERMO_ZONE;
144 deviceWho = Who.THERMOREGULATION;
147 case SCS_THERMO_CENTRAL_UNIT: {
148 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_THERMO_CU;
149 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_THERMO_CU;
150 deviceWho = Who.THERMOREGULATION;
153 case SCS_ENERGY_METER: {
154 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_ENERGY_METER;
155 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_ENERGY_METER;
156 deviceWho = Who.ENERGY_MANAGEMENT;
159 case SCENARIO_CONTROL: {
160 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_CEN_SCENARIO_CONTROL;
161 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_CEN_SCENARIO_CONTROL;
162 deviceWho = Who.CEN_SCENARIO_SCHEDULER;
165 case SCS_DRY_CONTACT_IR: {
166 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_DRY_CONTACT_IR;
167 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_DRY_CONTACT_IR;
168 deviceWho = Who.CEN_PLUS_SCENARIO_SCHEDULER;
171 case MULTIFUNCTION_SCENARIO_CONTROL: {
172 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_CENPLUS_SCENARIO_CONTROL;
173 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_CENPLUS_SCENARIO_CONTROL;
174 deviceWho = Who.CEN_PLUS_SCENARIO_SCHEDULER;
177 case SCS_AUXILIARY_TOGGLE_CONTROL: {
178 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_BUS_AUX;
179 thingLabel = OpenWebNetBindingConstants.THING_LABEL_BUS_AUX;
184 logger.warn("Device type {} is not supported, default to GENERIC device (WHERE={})", deviceType, where);
185 if (where instanceof WhereZigBee) {
186 thingLabel = "ZigBee " + thingLabel;
188 if (baseMsg != null) {
189 deviceWho = baseMsg.getWho();
193 String ownId = bridgeHandler.ownIdFromWhoWhere(deviceWho, where);
194 if (OpenWebNetBindingConstants.THING_TYPE_BUS_ON_OFF_SWITCH.equals(thingTypeUID)) {
195 if (bridgeHandler.getRegisteredDevice(ownId) != null) {
196 logger.debug("dimmer/switch with WHERE={} already registered, skipping this discovery result", where);
201 String tId = bridgeHandler.thingIdFromWhere(where);
202 ThingUID thingUID = new ThingUID(thingTypeUID, bridgeUID, tId);
204 DiscoveryResult discoveryResult = null;
206 String whereConfig = where.value();
207 if (where instanceof WhereZigBee && WhereZigBee.UNIT_02.equals(((WhereZigBee) where).getUnit())) {
208 logger.debug("UNIT=02 found (WHERE={}) -> will remove previous result if exists", where);
209 thingRemoved(thingUID); // remove previously discovered thing
210 // re-create thingUID with new type
211 thingTypeUID = OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH_2UNITS;
212 thingLabel = OpenWebNetBindingConstants.THING_LABEL_ZB_ON_OFF_SWITCH_2UNITS;
213 thingUID = new ThingUID(thingTypeUID, bridgeUID, tId);
214 whereConfig = ((WhereZigBee) where).valueWithUnit(WhereZigBee.UNIT_ALL); // replace unit '02' with '00'
215 logger.debug("UNIT=02, switching type from {} to {}",
216 OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH,
217 OpenWebNetBindingConstants.THING_TYPE_ZB_ON_OFF_SWITCH_2UNITS);
219 Map<String, Object> properties = new HashMap<>(2);
220 properties.put(OpenWebNetBindingConstants.CONFIG_PROPERTY_WHERE, whereConfig);
221 properties.put(OpenWebNetBindingConstants.PROPERTY_OWNID, ownId);
222 if (OpenWebNetBindingConstants.THING_TYPE_GENERIC_DEVICE.equals(thingTypeUID)) {
223 thingLabel = thingLabel + " (WHO=" + deviceWho + ", WHERE=" + whereConfig + ")";
225 thingLabel = thingLabel + " (WHERE=" + whereConfig + ")";
227 discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID).withProperties(properties)
228 .withRepresentationProperty(OpenWebNetBindingConstants.PROPERTY_OWNID).withBridge(bridgeUID)
229 .withLabel(thingLabel).build();
230 thingDiscovered(discoveryResult);
234 public void deactivate() {
239 public void setThingHandler(@Nullable ThingHandler handler) {
240 if (handler instanceof OpenWebNetBridgeHandler) {
241 logger.debug("attaching {} to handler {} ", this, handler);
242 bridgeHandler = (OpenWebNetBridgeHandler) handler;
243 bridgeHandler.deviceDiscoveryService = this;
244 bridgeUID = bridgeHandler.getThing().getUID();
249 public @Nullable ThingHandler getThingHandler() {
250 return bridgeHandler;