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.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.livisismarthome.internal.LivisiBindingConstants;
26 import org.openhab.binding.livisismarthome.internal.client.api.entity.device.DeviceDTO;
27 import org.openhab.binding.livisismarthome.internal.handler.LivisiBridgeHandler;
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 LivisiDeviceDiscoveryService} is responsible for discovering new devices.
42 * @author Oliver Kuhl - Initial contribution
43 * @author Sven Strohschein - Renamed from Innogy to Livisi
46 public class LivisiDeviceDiscoveryService extends AbstractDiscoveryService
47 implements DiscoveryService, ThingHandlerService {
49 private static final int SEARCH_TIME_SECONDS = 60;
51 private final Logger logger = LoggerFactory.getLogger(LivisiDeviceDiscoveryService.class);
53 private @Nullable LivisiBridgeHandler bridgeHandler;
56 * Construct a {@link LivisiDeviceDiscoveryService}.
58 public LivisiDeviceDiscoveryService() {
59 super(SEARCH_TIME_SECONDS);
63 * Deactivates the {@link LivisiDeviceDiscoveryService} by unregistering it as
64 * {@link org.openhab.binding.livisismarthome.internal.listener.DeviceStatusListener} on the
65 * {@link LivisiBridgeHandler}. Older discovery results will be removed.
67 * @see org.openhab.core.config.discovery.AbstractDiscoveryService#deactivate()
70 public void deactivate() {
71 removeOlderResults(new Date().getTime());
75 public Set<ThingTypeUID> getSupportedThingTypes() {
76 return LivisiBindingConstants.SUPPORTED_DEVICE_THING_TYPES;
80 protected void startScan() {
81 logger.debug("SCAN for new LIVISI SmartHome devices started...");
82 final LivisiBridgeHandler bridgeHandlerNonNullable = bridgeHandler;
83 if (bridgeHandlerNonNullable != null) {
84 for (final DeviceDTO d : bridgeHandlerNonNullable.loadDevices()) {
91 protected synchronized void stopScan() {
93 removeOlderResults(getTimestampOfLastScan());
96 public void onDeviceAdded(DeviceDTO device) {
97 final LivisiBridgeHandler bridgeHandlerNonNullable = bridgeHandler;
98 if (bridgeHandlerNonNullable != null) {
99 final ThingUID bridgeUID = bridgeHandlerNonNullable.getThing().getUID();
100 final Optional<ThingUID> thingUID = getThingUID(bridgeUID, device);
101 final Optional<ThingTypeUID> thingTypeUID = getThingTypeUID(device);
103 if (thingUID.isPresent() && thingTypeUID.isPresent()) {
104 String name = device.getConfig().getName();
105 if (name.isEmpty()) {
106 name = device.getSerialNumber();
109 final Map<String, Object> properties = new HashMap<>();
110 properties.put(PROPERTY_ID, device.getId());
113 if (device.hasLocation()) {
114 label = device.getType() + ": " + name + " (" + device.getLocation().getName() + ")";
116 label = device.getType() + ": " + name;
119 final DiscoveryResult discoveryResult = DiscoveryResultBuilder.create(thingUID.get())
120 .withThingType(thingTypeUID.get()).withProperties(properties).withBridge(bridgeUID)
121 .withRepresentationProperty(PROPERTY_ID).withLabel(label).build();
123 thingDiscovered(discoveryResult);
125 logger.debug("Discovered unsupported device of type '{}' and name '{}' with id {}", device.getType(),
126 device.getConfig().getName(), device.getId());
132 * Returns the {@link ThingUID} for the given {@link DeviceDTO} or empty, if the device type is not available.
134 * @param bridgeUID bridge
135 * @param device device
136 * @return {@link ThingUID} for the given {@link DeviceDTO} or empty
138 private Optional<ThingUID> getThingUID(ThingUID bridgeUID, DeviceDTO device) {
139 final Optional<ThingTypeUID> thingTypeUID = getThingTypeUID(device);
141 if (thingTypeUID.isPresent() && getSupportedThingTypes().contains(thingTypeUID.get())) {
142 return Optional.of(new ThingUID(thingTypeUID.get(), bridgeUID, device.getId()));
144 return Optional.empty();
148 * Returns a {@link ThingTypeUID} for the given {@link DeviceDTO} or empty, if the device type is not available.
150 * @param device device
151 * @return {@link ThingTypeUID} for the given {@link DeviceDTO} or empty
153 private Optional<ThingTypeUID> getThingTypeUID(DeviceDTO device) {
154 final String thingTypeId = device.getType();
155 if (thingTypeId != null) {
156 return Optional.of(new ThingTypeUID(LivisiBindingConstants.BINDING_ID, thingTypeId));
158 return Optional.empty();
162 public void setThingHandler(@Nullable ThingHandler handler) {
163 if (handler instanceof LivisiBridgeHandler livisiBridgeHandler) {
164 bridgeHandler = livisiBridgeHandler;
169 public @Nullable ThingHandler getThingHandler() {
170 return bridgeHandler;