]> git.basschouten.com Git - openhab-addons.git/blob
7b567ba698db2eb1d390daa0cf5333ceffbbaebc
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 static java.util.Comparator.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19 import java.util.Optional;
20 import java.util.stream.Collectors;
21
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;
45
46 /**
47  * The {@link NetatmoDiscoveryService} searches for available Netatmo things
48  *
49  * @author GaĆ«l L'hopital - Initial contribution
50  *
51  */
52 @NonNullByDefault
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);
56
57     private @Nullable ApiBridgeHandler handler;
58
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);
62     }
63
64     @Override
65     public void startScan() {
66         ApiBridgeHandler localHandler = handler;
67         if (localHandler != null) {
68             ThingUID accountUID = localHandler.getThing().getUID();
69             try {
70                 AircareApi airCareApi = localHandler.getRestManager(AircareApi.class);
71                 if (airCareApi != null) { // Search Healthy Home Coaches
72                     ListBodyResponse<NAMain> body = airCareApi.getHomeCoachData(null).getBody();
73                     if (body != null) {
74                         body.getElements().stream().forEach(homeCoach -> createThing(homeCoach, accountUID));
75                     }
76                 }
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)));
83                         }
84                     });
85                 }
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))
91                             .forEach(home -> {
92                                 createThing(home, accountUID).ifPresent(homeUID -> {
93                                     home.getKnownPersons().forEach(person -> createThing(person, homeUID));
94
95                                     Map<String, ThingUID> bridgesUids = new HashMap<>();
96
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));
103                                                 });
104                                     });
105
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())))
110                                             .forEach(module -> {
111                                                 String bridgeId = module.getBridge();
112                                                 if (bridgeId == null) {
113                                                     createThing(module, homeUID).ifPresent(
114                                                             moduleUID -> bridgesUids.put(module.getId(), moduleUID));
115                                                 } else {
116                                                     createThing(module, bridgesUids.getOrDefault(bridgeId, homeUID));
117                                                 }
118                                             });
119                                 });
120                             });
121                 }
122             } catch (NetatmoException e) {
123                 logger.warn("Error during discovery process : {}", e.getMessage());
124             }
125         }
126     }
127
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_]", "")))
132                 .orElse(null);
133     }
134
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());
143         } else {
144             logger.info("Module '{}' is not handled by this version of the binding - it is ignored.", module.getName());
145         }
146         return Optional.ofNullable(moduleUID);
147     }
148
149     @Override
150     public void setThingHandler(ThingHandler handler) {
151         if (handler instanceof ApiBridgeHandler) {
152             this.handler = (ApiBridgeHandler) handler;
153         }
154     }
155
156     @Override
157     public @Nullable ThingHandler getThingHandler() {
158         return handler;
159     }
160
161     @Override
162     public void deactivate() {
163         super.deactivate();
164     }
165 }