2 * Copyright (c) 2010-2022 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 static java.util.Comparator.*;
17 import java.util.HashMap;
19 import java.util.Optional;
20 import java.util.stream.Collectors;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.netatmo.internal.api.AircareApi;
25 import org.openhab.binding.netatmo.internal.api.HomeApi;
26 import org.openhab.binding.netatmo.internal.api.ListBodyResponse;
27 import org.openhab.binding.netatmo.internal.api.NetatmoException;
28 import org.openhab.binding.netatmo.internal.api.WeatherApi;
29 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
30 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
31 import org.openhab.binding.netatmo.internal.api.dto.HomeDataModule;
32 import org.openhab.binding.netatmo.internal.api.dto.NAMain;
33 import org.openhab.binding.netatmo.internal.api.dto.NAModule;
34 import org.openhab.binding.netatmo.internal.config.NAThingConfiguration;
35 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
36 import org.openhab.core.config.discovery.AbstractDiscoveryService;
37 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
38 import org.openhab.core.config.discovery.DiscoveryService;
39 import org.openhab.core.thing.ThingTypeUID;
40 import org.openhab.core.thing.ThingUID;
41 import org.openhab.core.thing.binding.ThingHandler;
42 import org.openhab.core.thing.binding.ThingHandlerService;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * The {@link NetatmoDiscoveryService} searches for available Netatmo things
49 * @author Gaƫl L'hopital - Initial contribution
53 public class NetatmoDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService, DiscoveryService {
54 private static final int DISCOVER_TIMEOUT_SECONDS = 3;
55 private final Logger logger = LoggerFactory.getLogger(NetatmoDiscoveryService.class);
57 private @Nullable ApiBridgeHandler handler;
59 public NetatmoDiscoveryService() {
60 super(ModuleType.AS_SET.stream().filter(mt -> !mt.apiName.isBlank()).map(mt -> mt.thingTypeUID)
61 .collect(Collectors.toSet()), DISCOVER_TIMEOUT_SECONDS);
65 public void startScan() {
66 ApiBridgeHandler localHandler = handler;
67 if (localHandler != null) {
68 ThingUID accountUID = localHandler.getThing().getUID();
70 AircareApi airCareApi = localHandler.getRestManager(AircareApi.class);
71 if (airCareApi != null) { // Search Healthy Home Coaches
72 ListBodyResponse<NAMain> body = airCareApi.getHomeCoachData(null).getBody();
74 body.getElements().stream().forEach(homeCoach -> createThing(homeCoach, accountUID));
77 WeatherApi weatherApi = localHandler.getRestManager(WeatherApi.class);
78 if (weatherApi != null) { // Search owned or favorite stations
79 weatherApi.getFavoriteAndGuestStationsData().stream().forEach(station -> {
80 if (!station.isReadOnly() || localHandler.getReadFriends()) {
81 createThing(station, accountUID).ifPresent(stationUID -> station.getModules().values()
82 .stream().forEach(module -> createThing(module, stationUID)));
86 HomeApi homeApi = localHandler.getRestManager(HomeApi.class);
87 if (homeApi != null) { // Search those depending from a home that has modules + not only weather modules
88 homeApi.getHomesData(null, null).stream()
89 .filter(h -> !(h.getFeatures().isEmpty()
90 || h.getFeatures().contains(FeatureArea.WEATHER) && h.getFeatures().size() == 1))
92 createThing(home, accountUID).ifPresent(homeUID -> {
93 home.getKnownPersons().forEach(person -> createThing(person, homeUID));
95 Map<String, ThingUID> bridgesUids = new HashMap<>();
97 home.getRooms().values().stream().forEach(room -> {
98 room.getModuleIds().stream().map(id -> home.getModules().get(id))
99 .map(m -> m != null ? m.getType().feature : FeatureArea.NONE)
100 .filter(f -> FeatureArea.ENERGY.equals(f)).findAny().ifPresent(f -> {
101 createThing(room, homeUID).ifPresent(
102 roomUID -> bridgesUids.put(room.getId(), roomUID));
106 // Creating modules that have no bridge first, avoiding weather station itself
107 home.getModules().values().stream()
108 .filter(module -> module.getType().feature != FeatureArea.WEATHER)
109 .sorted(comparing(HomeDataModule::getBridge, nullsFirst(naturalOrder())))
111 String bridgeId = module.getBridge();
112 if (bridgeId == null) {
113 createThing(module, homeUID).ifPresent(
114 moduleUID -> bridgesUids.put(module.getId(), moduleUID));
116 createThing(module, bridgesUids.getOrDefault(bridgeId, homeUID));
122 } catch (NetatmoException e) {
123 logger.warn("Error during discovery process : {}", e.getMessage());
128 private @Nullable ThingUID findThingUID(ModuleType thingType, String thingId, ThingUID bridgeUID) {
129 ThingTypeUID thingTypeUID = thingType.thingTypeUID;
130 return getSupportedThingTypes().stream().filter(supported -> supported.equals(thingTypeUID)).findFirst()
131 .map(supported -> new ThingUID(supported, bridgeUID, thingId.replaceAll("[^a-zA-Z0-9_]", "")))
135 private Optional<ThingUID> createThing(NAModule module, ThingUID bridgeUID) {
136 ThingUID moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID);
137 if (moduleUID != null) {
138 DiscoveryResultBuilder resultBuilder = DiscoveryResultBuilder.create(moduleUID)
139 .withProperty(NAThingConfiguration.ID, module.getId())
140 .withRepresentationProperty(NAThingConfiguration.ID)
141 .withLabel(module.getName() != null ? module.getName() : module.getId()).withBridge(bridgeUID);
142 thingDiscovered(resultBuilder.build());
144 logger.info("Module '{}' is not handled by this version of the binding - it is ignored.", module.getName());
146 return Optional.ofNullable(moduleUID);
150 public void setThingHandler(ThingHandler handler) {
151 if (handler instanceof ApiBridgeHandler) {
152 this.handler = (ApiBridgeHandler) handler;
157 public @Nullable ThingHandler getThingHandler() {
162 public void deactivate() {