]> git.basschouten.com Git - openhab-addons.git/blob
092821b156b8541a3a12233af75ab0ec651a7007
[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.Optional;
19 import java.util.Set;
20
21 import javax.ws.rs.core.UriBuilder;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
26 import org.openhab.binding.netatmo.internal.api.data.NetatmoConstants.FeatureArea;
27 import org.openhab.binding.netatmo.internal.api.dto.HomeData;
28 import org.openhab.binding.netatmo.internal.api.dto.NAHomeStatus;
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 Optional<NAHomeStatus> getHomeStatus(String homeId) throws NetatmoException {
47         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_HOMESTATUS, PARAM_HOME_ID, homeId);
48
49         return Optional.ofNullable(get(uriBuilder, NAHomeStatusResponse.class).getBody());
50     }
51
52     public @Nullable HomeData getHomeData(String homeId) throws NetatmoException {
53         Collection<HomeData> result = getHomesData(homeId, null);
54         return result.isEmpty() ? null : result.iterator().next();
55     }
56
57     public Collection<HomeData> getHomesData(@Nullable String homeId, @Nullable ModuleType type)
58             throws NetatmoException {
59         UriBuilder uriBuilder = getApiUriBuilder(SUB_PATH_HOMES_DATA, PARAM_HOME_ID, homeId);
60
61         if (type != null) {
62             uriBuilder.queryParam(PARAM_GATEWAY_TYPE, type.name());
63         }
64
65         HomeData.HomesDataResponse response = get(uriBuilder, HomeData.HomesDataResponse.class);
66         ListBodyResponse<HomeData> body = response.getBody();
67         return body != null ? body.getElements() : Set.of();
68     }
69 }