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.netatmo.internal.discovery;
15 import java.util.Optional;
16 import java.util.stream.Collectors;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
21 import org.openhab.binding.netatmo.internal.api.dto.NAModule;
22 import org.openhab.binding.netatmo.internal.config.NAThingConfiguration;
23 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
24 import org.openhab.core.config.discovery.AbstractDiscoveryService;
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.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link NetatmoDiscoveryService} searches for available Netatmo things
37 * @author Gaƫl L'hopital - Initial contribution
41 public class NetatmoDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService, DiscoveryService {
42 private static final int DISCOVER_TIMEOUT_SECONDS = 3;
43 private final Logger logger = LoggerFactory.getLogger(NetatmoDiscoveryService.class);
45 private @Nullable ApiBridgeHandler handler;
47 public NetatmoDiscoveryService() {
48 super(ModuleType.AS_SET.stream().filter(mt -> !mt.apiName.isBlank()).map(mt -> mt.thingTypeUID)
49 .collect(Collectors.toSet()), DISCOVER_TIMEOUT_SECONDS);
53 public void startScan() {
54 ApiBridgeHandler localHandler = handler;
55 if (localHandler != null) {
56 localHandler.identifyAllModulesAndApplyAction(this::createThing);
60 private Optional<ThingUID> findThingUID(ModuleType moduleType, String thingId, ThingUID bridgeUID) {
61 ThingTypeUID thingTypeUID = moduleType.thingTypeUID;
62 return getSupportedThingTypes().stream().filter(supported -> supported.equals(thingTypeUID)).findFirst()
63 .map(supported -> new ThingUID(supported, bridgeUID, thingId.replaceAll("[^a-zA-Z0-9_]", "")));
66 private Optional<ThingUID> createThing(NAModule module, ThingUID bridgeUID) {
67 Optional<ThingUID> moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID);
68 if (moduleUID.isPresent()) {
69 DiscoveryResultBuilder resultBuilder = DiscoveryResultBuilder.create(moduleUID.get())
70 .withProperty(NAThingConfiguration.ID, module.getId())
71 .withRepresentationProperty(NAThingConfiguration.ID)
72 .withLabel(module.getName() != null ? module.getName() : module.getId()).withBridge(bridgeUID);
73 thingDiscovered(resultBuilder.build());
75 logger.info("Module '{}' is not handled by this version of the binding - it is ignored.", module.getName());
81 public void setThingHandler(ThingHandler handler) {
82 if (handler instanceof ApiBridgeHandler) {
83 this.handler = (ApiBridgeHandler) handler;
88 public @Nullable ThingHandler getThingHandler() {
93 public void deactivate() {