]> git.basschouten.com Git - openhab-addons.git/blob
39adb3e6a794620c78d72d0b855856921090ae98
[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.api;
14
15 import static org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.*;
16
17 import java.util.Collection;
18 import java.util.Set;
19
20 import javax.ws.rs.core.UriBuilder;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
25 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
26 import org.openhab.binding.netatmo.internal.api.dto.HomeData;
27 import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus;
28 import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.HomeStatus;
29 import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus.NAHomeStatusResponse;
30 import org.openhab.binding.netatmo.internal.handler.ApiBridgeHandler;
31
32 /**
33  * The {@link HomeApi} handles general API endpoints not requiring specific scope area
34  *
35  * @author GaĆ«l L'hopital - Initial contribution
36  *
37  */
38
39 @NonNullByDefault
40 public class HomeApi extends RestManager {
41
42     public HomeApi(ApiBridgeHandler apiClient) {
43         super(apiClient, FeatureArea.NONE);
44     }
45
46     public @Nullable HomeStatus getHomeStatus(String homeId) throws NetatmoException {
47         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_HOMESTATUS, PARAM_HOME_ID, homeId);
48
49         NAHomeStatusResponse response = get(uriBuilder, NAHomeStatusResponse.class);
50         NAHomeStatus body = response.getBody();
51         return body != null ? body.getHomeStatus().orElse(null) : null;
52     }
53
54     public @Nullable HomeData getHomeData(String homeId) throws NetatmoException {
55         Collection<HomeData> result = getHomesData(homeId, null);
56         return result.isEmpty() ? null : result.iterator().next();
57     }
58
59     public Collection<HomeData> getHomesData(@Nullable String homeId, @Nullable ModuleType type)
60             throws NetatmoException {
61         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_HOMES_DATA, PARAM_HOME_ID, homeId);
62
63         if (type != null) {
64             uriBuilder.queryParam(PARAM_GATEWAY_TYPE, type.name());
65         }
66
67         HomeData.HomesDataResponse response = get(uriBuilder, HomeData.HomesDataResponse.class);
68         ListBodyResponse<HomeData> body = response.getBody();
69         return body != null ? body.getElements() : Set.of();
70     }
71 }