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.innogysmarthome.internal.discovery;
15 import static org.openhab.binding.innogysmarthome.internal.InnogyBindingConstants.*;
17 import java.util.Date;
18 import java.util.HashMap;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.innogysmarthome.internal.client.entity.device.Device;
25 import org.openhab.binding.innogysmarthome.internal.handler.InnogyBridgeHandler;
26 import org.openhab.binding.innogysmarthome.internal.handler.InnogyDeviceHandler;
27 import org.openhab.binding.innogysmarthome.internal.listener.DeviceStatusListener;
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 InnogyDeviceDiscoveryService} is responsible for discovering new devices.
42 * @author Oliver Kuhl - Initial contribution
45 public class InnogyDeviceDiscoveryService extends AbstractDiscoveryService
46 implements DiscoveryService, ThingHandlerService {
48 private static final int SEARCH_TIME_SECONDS = 60;
50 private final Logger logger = LoggerFactory.getLogger(InnogyDeviceDiscoveryService.class);
52 private @Nullable InnogyBridgeHandler bridgeHandler;
55 * Construct an {@link InnogyDeviceDiscoveryService}.
57 public InnogyDeviceDiscoveryService() {
58 super(SEARCH_TIME_SECONDS);
62 * Deactivates the {@link InnogyDeviceDiscoveryService} by unregistering it as {@link DeviceStatusListener} on the
63 * {@link InnogyBridgeHandler}. Older discovery results will be removed.
65 * @see org.openhab.core.config.discovery.AbstractDiscoveryService#deactivate()
68 public void deactivate() {
69 removeOlderResults(new Date().getTime());
73 public Set<ThingTypeUID> getSupportedThingTypes() {
74 return InnogyDeviceHandler.SUPPORTED_THING_TYPES;
78 protected void startScan() {
79 logger.debug("SCAN for new innogy devices started...");
80 if (bridgeHandler != null) {
81 for (final Device d : bridgeHandler.loadDevices()) {
88 protected synchronized void stopScan() {
90 removeOlderResults(getTimestampOfLastScan());
93 public void onDeviceAdded(Device device) {
94 if (bridgeHandler == null) {
97 final ThingUID bridgeUID = bridgeHandler.getThing().getUID();
98 final ThingUID thingUID = getThingUID(bridgeUID, device);
99 final ThingTypeUID thingTypeUID = getThingTypeUID(device);
101 if (thingUID != null && thingTypeUID != null) {
102 String name = device.getConfig().getName();
103 if (name.isEmpty()) {
104 name = device.getSerialnumber();
107 final Map<String, Object> properties = new HashMap<>();
108 properties.put(PROPERTY_ID, device.getId());
111 if (device.hasLocation()) {
112 label = device.getType() + ": " + name + " (" + device.getLocation().getName() + ")";
114 label = device.getType() + ": " + name;
117 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID).withThingType(thingTypeUID)
118 .withProperties(properties).withBridge(bridgeUID).withLabel(label).build();
120 thingDiscovered(discoveryResult);
122 logger.debug("Discovered unsupported device of type '{}' and name '{}' with id {}", device.getType(),
123 device.getConfig().getName(), device.getId());
128 * Returns the {@link ThingUID} for the given {@link Device} or null, if the device type is not available.
133 private @Nullable ThingUID getThingUID(ThingUID bridgeUID, Device device) {
134 final ThingTypeUID thingTypeUID = getThingTypeUID(device);
136 if (thingTypeUID != null && getSupportedThingTypes().contains(thingTypeUID)) {
137 return new ThingUID(thingTypeUID, bridgeUID, device.getId());
143 * Returns a {@link ThingTypeUID} for the given {@link Device} or null, if the device type is not available.
148 private @Nullable ThingTypeUID getThingTypeUID(Device device) {
149 final String thingTypeId = device.getType();
150 return thingTypeId != null ? new ThingTypeUID(BINDING_ID, thingTypeId) : null;
154 public void setThingHandler(@Nullable ThingHandler handler) {
155 if (handler instanceof InnogyBridgeHandler) {
156 bridgeHandler = (InnogyBridgeHandler) handler;
161 public @Nullable ThingHandler getThingHandler() {
162 return bridgeHandler;