]> git.basschouten.com Git - openhab-addons.git/blob
e1ec6ff247ef40344b605ee4d1b443e33487a0ee
[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 org.openhab.binding.netatmo.internal.NetatmoBindingConstants.EQUIPMENT_ID;
16
17 import java.util.Set;
18 import java.util.stream.Collectors;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.netatmo.internal.api.AircareApi;
23 import org.openhab.binding.netatmo.internal.api.HomeApi;
24 import org.openhab.binding.netatmo.internal.api.ListBodyResponse;
25 import org.openhab.binding.netatmo.internal.api.NetatmoException;
26 import org.openhab.binding.netatmo.internal.api.WeatherApi;
27 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
28 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
29 import org.openhab.binding.netatmo.internal.api.dto.NAMain;
30 import org.openhab.binding.netatmo.internal.api.dto.NAModule;
31 import org.openhab.binding.netatmo.internal.config.BindingConfiguration;
32 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
33 import org.openhab.core.config.discovery.AbstractDiscoveryService;
34 import org.openhab.core.config.discovery.DiscoveryResultBuilder;
35 import org.openhab.core.config.discovery.DiscoveryService;
36 import org.openhab.core.thing.ThingTypeUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.thing.binding.ThingHandler;
39 import org.openhab.core.thing.binding.ThingHandlerService;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 /**
44  * The {@link NetatmoDiscoveryService} searches for available Netatmo things
45  *
46  * @author GaĆ«l L'hopital - Initial contribution
47  *
48  */
49 @NonNullByDefault
50 public class NetatmoDiscoveryService extends AbstractDiscoveryService implements ThingHandlerService, DiscoveryService {
51     private static final Set<ModuleType> SKIPPED_TYPES = Set.of(ModuleType.UNKNOWN, ModuleType.ACCOUNT);
52     private static final int DISCOVER_TIMEOUT_SECONDS = 5;
53     private final Logger logger = LoggerFactory.getLogger(NetatmoDiscoveryService.class);
54     private @Nullable ApiBridgeHandler handler;
55     private @Nullable BindingConfiguration config;
56
57     public NetatmoDiscoveryService() {
58         super(ModuleType.AS_SET.stream().filter(mt -> !SKIPPED_TYPES.contains(mt)).map(mt -> mt.thingTypeUID)
59                 .collect(Collectors.toSet()), DISCOVER_TIMEOUT_SECONDS);
60     }
61
62     @Override
63     public void startScan() {
64         BindingConfiguration localConf = config;
65         ApiBridgeHandler localHandler = handler;
66         if (localHandler != null && localConf != null) {
67             ThingUID apiBridgeUID = localHandler.getThing().getUID();
68             try {
69                 AircareApi airCareApi = localHandler.getRestManager(AircareApi.class);
70                 if (airCareApi != null) { // Search Healthy Home Coaches
71                     ListBodyResponse<NAMain> body = airCareApi.getHomeCoachData(null).getBody();
72                     if (body != null) {
73                         body.getElements().stream().forEach(homeCoach -> createThing(homeCoach, apiBridgeUID));
74                     }
75                 }
76                 if (localConf.readFriends) {
77                     WeatherApi weatherApi = localHandler.getRestManager(WeatherApi.class);
78                     if (weatherApi != null) { // Search favorite stations
79                         ListBodyResponse<NAMain> body = weatherApi.getStationsData(null, true).getBody();
80                         if (body != null) {
81                             body.getElements().stream().filter(NAMain::isReadOnly).forEach(station -> {
82                                 ThingUID bridgeUID = createThing(station, apiBridgeUID);
83                                 station.getModules().values().stream()
84                                         .forEach(module -> createThing(module, bridgeUID));
85                             });
86                         }
87                     }
88                 }
89                 HomeApi homeApi = localHandler.getRestManager(HomeApi.class);
90                 if (homeApi != null) { // Search all the rest
91                     homeApi.getHomesData(null, null).stream().filter(h -> !h.getFeatures().isEmpty()).forEach(home -> {
92                         ThingUID homeUID = createThing(home, apiBridgeUID);
93                         home.getKnownPersons().forEach(person -> createThing(person, homeUID));
94                         home.getModules().values().stream().forEach(device -> {
95                             ModuleType deviceType = device.getType();
96                             String deviceBridge = device.getBridge();
97                             ThingUID bridgeUID = deviceBridge != null && deviceType.getBridge() != ModuleType.HOME
98                                     ? findThingUID(deviceType.getBridge(), deviceBridge, apiBridgeUID)
99                                     : deviceType.getBridge() == ModuleType.HOME ? homeUID : apiBridgeUID;
100                             createThing(device, bridgeUID);
101                         });
102                         home.getRooms().values().stream().forEach(room -> {
103                             room.getModuleIds().stream().map(id -> home.getModules().get(id))
104                                     .map(m -> m != null ? m.getType().feature : FeatureArea.NONE)
105                                     .filter(f -> FeatureArea.ENERGY.equals(f)).findAny()
106                                     .ifPresent(f -> createThing(room, homeUID));
107                         });
108                     });
109                 }
110             } catch (NetatmoException e) {
111                 logger.warn("Error during discovery process : {}", e.getMessage());
112             }
113         }
114     }
115
116     private ThingUID findThingUID(ModuleType thingType, String thingId, @Nullable ThingUID brigdeUID) {
117         for (ThingTypeUID supported : getSupportedThingTypes()) {
118             ThingTypeUID thingTypeUID = thingType.thingTypeUID;
119             if (supported.equals(thingTypeUID)) {
120                 String id = thingId.replaceAll("[^a-zA-Z0-9_]", "");
121                 return brigdeUID == null ? new ThingUID(supported, id) : new ThingUID(supported, brigdeUID, id);
122             }
123         }
124         throw new IllegalArgumentException("Unsupported device type discovered : " + thingType);
125     }
126
127     private ThingUID createThing(NAModule module, @Nullable ThingUID bridgeUID) {
128         ThingUID moduleUID = findThingUID(module.getType(), module.getId(), bridgeUID);
129         DiscoveryResultBuilder resultBuilder = DiscoveryResultBuilder.create(moduleUID)
130                 .withProperty(EQUIPMENT_ID, module.getId()).withRepresentationProperty(EQUIPMENT_ID)
131                 .withLabel(module.getName() != null ? module.getName() : module.getId());
132         if (bridgeUID != null) {
133             resultBuilder.withBridge(bridgeUID);
134         }
135         thingDiscovered(resultBuilder.build());
136         return moduleUID;
137     }
138
139     @Override
140     public void setThingHandler(ThingHandler handler) {
141         if (handler instanceof ApiBridgeHandler) {
142             this.handler = (ApiBridgeHandler) handler;
143             this.config = ((ApiBridgeHandler) handler).getConfiguration();
144         }
145     }
146
147     @Override
148     public @Nullable ThingHandler getThingHandler() {
149         return handler;
150     }
151
152     @Override
153     public void deactivate() {
154         super.deactivate();
155     }
156 }