]> git.basschouten.com Git - openhab-addons.git/blob
728644064c5f48e6dbe77c57a20f17cf043f335e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.netatmo.internal.discovery;
14
15 import java.util.Optional;
16 import java.util.stream.Collectors;
17
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;
33
34 /**
35  * The {@link NetatmoDiscoveryService} searches for available Netatmo things
36  *
37  * @author GaĆ«l L'hopital - Initial contribution
38  *
39  */
40 @NonNullByDefault
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);
44
45     private @Nullable ApiBridgeHandler handler;
46
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);
50     }
51
52     @Override
53     public void startScan() {
54         ApiBridgeHandler localHandler = handler;
55         if (localHandler != null) {
56             localHandler.identifyAllModulesAndApplyAction(this::createThing);
57         }
58     }
59
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_]", "")));
64     }
65
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());
74         } else {
75             logger.info("Module '{}' is not handled by this version of the binding - it is ignored.", module.getName());
76         }
77         return moduleUID;
78     }
79
80     @Override
81     public void setThingHandler(ThingHandler handler) {
82         if (handler instanceof ApiBridgeHandler bridgeHandler) {
83             this.handler = bridgeHandler;
84         }
85     }
86
87     @Override
88     public @Nullable ThingHandler getThingHandler() {
89         return handler;
90     }
91
92     @Override
93     public void deactivate() {
94         super.deactivate();
95     }
96 }