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.livisismarthome.internal.discovery;
15 import static org.openhab.binding.livisismarthome.internal.LivisiBindingConstants.PROPERTY_ID;
17 import java.util.Date;
18 import java.util.HashMap;
20 import java.util.Optional;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.openhab.binding.livisismarthome.internal.LivisiBindingConstants;
25 import org.openhab.binding.livisismarthome.internal.client.api.entity.device.DeviceDTO;
26 import org.openhab.binding.livisismarthome.internal.handler.LivisiBridgeHandler;
27 import org.openhab.core.config.discovery.AbstractThingHandlerDiscoveryService;
28 import org.openhab.core.config.discovery.DiscoveryResult;
29 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
30 import org.openhab.core.thing.ThingTypeUID;
31 import org.openhab.core.thing.ThingUID;
32 import org.osgi.service.component.annotations.Component;
33 import org.osgi.service.component.annotations.ServiceScope;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * The {@link LivisiDeviceDiscoveryService} is responsible for discovering new devices.
40 * @author Oliver Kuhl - Initial contribution
41 * @author Sven Strohschein - Renamed from Innogy to Livisi
43 @Component(scope = ServiceScope.PROTOTYPE, service = LivisiDeviceDiscoveryService.class)
45 public class LivisiDeviceDiscoveryService extends AbstractThingHandlerDiscoveryService<LivisiBridgeHandler> {
47 private static final int SEARCH_TIME_SECONDS = 60;
49 private final Logger logger = LoggerFactory.getLogger(LivisiDeviceDiscoveryService.class);
52 * Construct a {@link LivisiDeviceDiscoveryService}.
54 public LivisiDeviceDiscoveryService() {
55 super(LivisiBridgeHandler.class, SEARCH_TIME_SECONDS);
59 * Deactivates the {@link LivisiDeviceDiscoveryService} by unregistering it as
60 * {@link org.openhab.binding.livisismarthome.internal.listener.DeviceStatusListener} on the
61 * {@link LivisiBridgeHandler}. Older discovery results will be removed.
63 * @see org.openhab.core.config.discovery.AbstractDiscoveryService#deactivate()
66 public void dispose() {
68 removeOlderResults(new Date().getTime());
72 public Set<ThingTypeUID> getSupportedThingTypes() {
73 return LivisiBindingConstants.SUPPORTED_DEVICE_THING_TYPES;
77 protected void startScan() {
78 logger.debug("SCAN for new LIVISI SmartHome devices started...");
79 for (final DeviceDTO d : thingHandler.loadDevices()) {
85 protected synchronized void stopScan() {
87 removeOlderResults(getTimestampOfLastScan());
90 public void onDeviceAdded(DeviceDTO device) {
91 final ThingUID bridgeUID = thingHandler.getThing().getUID();
92 final Optional<ThingUID> thingUID = getThingUID(bridgeUID, device);
93 final Optional<ThingTypeUID> thingTypeUID = getThingTypeUID(device);
95 if (thingUID.isPresent() && thingTypeUID.isPresent()) {
96 String name = device.getConfig().getName();
98 name = device.getSerialNumber();
101 final Map<String, Object> properties = new HashMap<>();
102 properties.put(PROPERTY_ID, device.getId());
105 if (device.hasLocation()) {
106 label = device.getType() + ": " + name + " (" + device.getLocation().getName() + ")";
108 label = device.getType() + ": " + name;
111 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID.get())
112 .withThingType(thingTypeUID.get()).withProperties(properties).withBridge(bridgeUID)
113 .withRepresentationProperty(PROPERTY_ID).withLabel(label).build();
115 thingDiscovered(discoveryResult);
117 logger.debug("Discovered unsupported device of type '{}' and name '{}' with id {}", device.getType(),
118 device.getConfig().getName(), device.getId());
123 * Returns the {@link ThingUID} for the given {@link DeviceDTO} or empty, if the device type is not available.
125 * @param bridgeUID bridge
126 * @param device device
127 * @return {@link ThingUID} for the given {@link DeviceDTO} or empty
129 private Optional<ThingUID> getThingUID(ThingUID bridgeUID, DeviceDTO device) {
130 final Optional<ThingTypeUID> thingTypeUID = getThingTypeUID(device);
132 if (thingTypeUID.isPresent() && getSupportedThingTypes().contains(thingTypeUID.get())) {
133 return Optional.of(new ThingUID(thingTypeUID.get(), bridgeUID, device.getId()));
135 return Optional.empty();
139 * Returns a {@link ThingTypeUID} for the given {@link DeviceDTO} or empty, if the device type is not available.
141 * @param device device
142 * @return {@link ThingTypeUID} for the given {@link DeviceDTO} or empty
144 private Optional<ThingTypeUID> getThingTypeUID(DeviceDTO device) {
145 final String thingTypeId = device.getType();
146 if (thingTypeId != null) {
147 return Optional.of(new ThingTypeUID(LivisiBindingConstants.BINDING_ID, thingTypeId));
149 return Optional.empty();