2 * Copyright (c) 2010-2024 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.tado.swagger.codegen.api.client;
15 import java.io.IOException;
16 import java.lang.reflect.Type;
17 import java.util.List;
18 import java.util.concurrent.TimeUnit;
20 import org.eclipse.jetty.client.HttpClient;
21 import org.eclipse.jetty.client.api.ContentResponse;
22 import org.eclipse.jetty.client.api.Request;
23 import org.eclipse.jetty.client.util.StringContentProvider;
24 import org.eclipse.jetty.http.HttpHeader;
25 import org.eclipse.jetty.http.HttpMethod;
26 import org.eclipse.jetty.http.HttpStatus;
27 import org.eclipse.jetty.util.ssl.SslContextFactory;
28 import org.openhab.binding.tado.swagger.codegen.api.ApiException;
29 import org.openhab.binding.tado.swagger.codegen.api.auth.Authorizer;
30 import org.openhab.binding.tado.swagger.codegen.api.model.GenericZoneCapabilities;
31 import org.openhab.binding.tado.swagger.codegen.api.model.HomeInfo;
32 import org.openhab.binding.tado.swagger.codegen.api.model.HomePresence;
33 import org.openhab.binding.tado.swagger.codegen.api.model.HomeState;
34 import org.openhab.binding.tado.swagger.codegen.api.model.MobileDevice;
35 import org.openhab.binding.tado.swagger.codegen.api.model.Overlay;
36 import org.openhab.binding.tado.swagger.codegen.api.model.OverlayTemplate;
37 import org.openhab.binding.tado.swagger.codegen.api.model.User;
38 import org.openhab.binding.tado.swagger.codegen.api.model.Zone;
39 import org.openhab.binding.tado.swagger.codegen.api.model.ZoneState;
41 import com.google.gson.Gson;
42 import com.google.gson.reflect.TypeToken;
45 * Static imported copy of class created by Swagger Codegen
47 * @author Andrew Fiddian-Green - Initial contribution
49 public class HomeApi {
50 private static final HttpClient CLIENT = new HttpClient(new SslContextFactory());
52 private String baseUrl = "https://my.tado.com/api/v2";
53 private int timeout = 5000;
56 private Authorizer authorizer;
58 public HomeApi(Gson gson, Authorizer authorizer) {
60 this.authorizer = authorizer;
63 public void deleteZoneOverlay(Long homeId, Long zoneId) throws IOException, ApiException {
65 // verify the required parameter 'homeId' is set
67 throw new ApiException(400, "Missing the required parameter 'homeId' when calling deleteZoneOverlay");
70 // verify the required parameter 'zoneId' is set
72 throw new ApiException(400, "Missing the required parameter 'zoneId' when calling deleteZoneOverlay");
75 startHttpClient(CLIENT);
77 // create path and map variables
78 String path = "/homes/{home_id}/zones/{zone_id}/overlay"
79 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
80 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
82 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.DELETE).timeout(timeout,
83 TimeUnit.MILLISECONDS);
85 request.accept("application/json");
86 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
88 if (authorizer != null) {
89 authorizer.addAuthorization(request);
92 ContentResponse response;
94 response = request.send();
95 } catch (Exception e) {
96 throw new IOException(e);
99 int statusCode = response.getStatus();
100 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
101 throw new ApiException(response, "Operation deleteZoneOverlay failed with error " + statusCode);
105 public HomeState homeState(Long homeId) throws IOException, ApiException {
107 // verify the required parameter 'homeId' is set
108 if (homeId == null) {
109 throw new ApiException(400, "Missing the required parameter 'homeId' when calling homeState");
112 startHttpClient(CLIENT);
114 // create path and map variables
115 String path = "/homes/{home_id}/state".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
117 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
118 TimeUnit.MILLISECONDS);
120 request.accept("application/json");
121 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
123 if (authorizer != null) {
124 authorizer.addAuthorization(request);
127 ContentResponse response;
129 response = request.send();
130 } catch (Exception e) {
131 throw new IOException(e);
134 int statusCode = response.getStatus();
135 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
136 throw new ApiException(response, "Operation homeState failed with error " + statusCode);
139 Type returnType = new TypeToken<HomeState>() {
141 return gson.fromJson(response.getContentAsString(), returnType);
144 public List<MobileDevice> listMobileDevices(Long homeId) throws IOException, ApiException {
146 // verify the required parameter 'homeId' is set
147 if (homeId == null) {
148 throw new ApiException(400, "Missing the required parameter 'homeId' when calling listMobileDevices");
151 startHttpClient(CLIENT);
153 // create path and map variables
154 String path = "/homes/{home_id}/mobileDevices".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
156 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
157 TimeUnit.MILLISECONDS);
159 request.accept("application/json");
160 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
162 if (authorizer != null) {
163 authorizer.addAuthorization(request);
166 ContentResponse response;
168 response = request.send();
169 } catch (Exception e) {
170 throw new IOException(e);
173 int statusCode = response.getStatus();
174 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
175 throw new ApiException(response, "Operation listMobileDevices failed with error " + statusCode);
178 Type returnType = new TypeToken<List<MobileDevice>>() {
180 return gson.fromJson(response.getContentAsString(), returnType);
183 public List<Zone> listZones(Long homeId) throws IOException, ApiException {
185 // verify the required parameter 'homeId' is set
186 if (homeId == null) {
187 throw new ApiException(400, "Missing the required parameter 'homeId' when calling listZones");
190 startHttpClient(CLIENT);
192 // create path and map variables
193 String path = "/homes/{home_id}/zones".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
195 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
196 TimeUnit.MILLISECONDS);
198 request.accept("application/json");
199 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
201 if (authorizer != null) {
202 authorizer.addAuthorization(request);
205 ContentResponse response;
207 response = request.send();
208 } catch (Exception e) {
209 throw new IOException(e);
212 int statusCode = response.getStatus();
213 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
214 throw new ApiException(response, "Operation listZones failed with error " + statusCode);
217 Type returnType = new TypeToken<List<Zone>>() {
219 return gson.fromJson(response.getContentAsString(), returnType);
222 public HomeInfo showHome(Long homeId) throws IOException, ApiException {
224 // verify the required parameter 'homeId' is set
225 if (homeId == null) {
226 throw new ApiException(400, "Missing the required parameter 'homeId' when calling showHome");
229 startHttpClient(CLIENT);
231 // create path and map variables
232 String path = "/homes/{home_id}".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
234 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
235 TimeUnit.MILLISECONDS);
237 request.accept("application/json");
238 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
240 if (authorizer != null) {
241 authorizer.addAuthorization(request);
244 ContentResponse response;
246 response = request.send();
247 } catch (Exception e) {
248 throw new IOException(e);
251 int statusCode = response.getStatus();
252 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
253 throw new ApiException(response, "Operation showHome failed with error " + statusCode);
256 Type returnType = new TypeToken<HomeInfo>() {
258 return gson.fromJson(response.getContentAsString(), returnType);
261 public User showUser() throws IOException, ApiException {
263 startHttpClient(CLIENT);
265 // create path and map variables
268 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
269 TimeUnit.MILLISECONDS);
271 request.accept("application/json");
272 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
274 if (authorizer != null) {
275 authorizer.addAuthorization(request);
278 ContentResponse response;
280 response = request.send();
281 } catch (Exception e) {
282 throw new IOException(e);
285 int statusCode = response.getStatus();
286 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
287 throw new ApiException(response, "Operation showUser failed with error " + statusCode);
290 Type returnType = new TypeToken<User>() {
292 return gson.fromJson(response.getContentAsString(), returnType);
295 public GenericZoneCapabilities showZoneCapabilities(Long homeId, Long zoneId) throws IOException, ApiException {
297 // verify the required parameter 'homeId' is set
298 if (homeId == null) {
299 throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneCapabilities");
302 // verify the required parameter 'zoneId' is set
303 if (zoneId == null) {
304 throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneCapabilities");
307 startHttpClient(CLIENT);
309 // create path and map variables
310 String path = "/homes/{home_id}/zones/{zone_id}/capabilities"
311 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
312 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
314 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
315 TimeUnit.MILLISECONDS);
317 request.accept("application/json");
318 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
320 if (authorizer != null) {
321 authorizer.addAuthorization(request);
324 ContentResponse response;
326 response = request.send();
327 } catch (Exception e) {
328 throw new IOException(e);
331 int statusCode = response.getStatus();
332 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
333 throw new ApiException(response, "Operation showZoneCapabilities failed with error " + statusCode);
336 Type returnType = new TypeToken<GenericZoneCapabilities>() {
338 return gson.fromJson(response.getContentAsString(), returnType);
341 public OverlayTemplate showZoneDefaultOverlay(Long homeId, Long zoneId) throws IOException, ApiException {
343 // verify the required parameter 'homeId' is set
344 if (homeId == null) {
345 throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneDefaultOverlay");
348 // verify the required parameter 'zoneId' is set
349 if (zoneId == null) {
350 throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneDefaultOverlay");
353 startHttpClient(CLIENT);
355 // create path and map variables
356 String path = "/homes/{home_id}/zones/{zone_id}/defaultOverlay"
357 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
358 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
360 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
361 TimeUnit.MILLISECONDS);
363 request.accept("application/json");
364 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
366 if (authorizer != null) {
367 authorizer.addAuthorization(request);
370 ContentResponse response;
372 response = request.send();
373 } catch (Exception e) {
374 throw new IOException(e);
377 int statusCode = response.getStatus();
378 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
379 throw new ApiException(response, "Operation showZoneDefaultOverlay failed with error " + statusCode);
382 Type returnType = new TypeToken<OverlayTemplate>() {
384 return gson.fromJson(response.getContentAsString(), returnType);
387 public Zone showZoneDetails(Long homeId, Long zoneId) throws IOException, ApiException {
389 // verify the required parameter 'homeId' is set
390 if (homeId == null) {
391 throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneDetails");
394 // verify the required parameter 'zoneId' is set
395 if (zoneId == null) {
396 throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneDetails");
399 startHttpClient(CLIENT);
401 // create path and map variables
402 String path = "/homes/{home_id}/zones/{zone_id}/details"
403 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
404 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
406 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
407 TimeUnit.MILLISECONDS);
409 request.accept("application/json");
410 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
412 if (authorizer != null) {
413 authorizer.addAuthorization(request);
416 ContentResponse response;
418 response = request.send();
419 } catch (Exception e) {
420 throw new IOException(e);
423 int statusCode = response.getStatus();
424 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
425 throw new ApiException(response, "Operation showZoneDetails failed with error " + statusCode);
428 Type returnType = new TypeToken<Zone>() {
430 return gson.fromJson(response.getContentAsString(), returnType);
433 public Overlay showZoneOverlay(Long homeId, Long zoneId) throws IOException, ApiException {
435 // verify the required parameter 'homeId' is set
436 if (homeId == null) {
437 throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneOverlay");
440 // verify the required parameter 'zoneId' is set
441 if (zoneId == null) {
442 throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneOverlay");
445 startHttpClient(CLIENT);
447 // create path and map variables
448 String path = "/homes/{home_id}/zones/{zone_id}/overlay"
449 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
450 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
452 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
453 TimeUnit.MILLISECONDS);
455 request.accept("application/json");
456 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
458 if (authorizer != null) {
459 authorizer.addAuthorization(request);
462 ContentResponse response;
464 response = request.send();
465 } catch (Exception e) {
466 throw new IOException(e);
469 int statusCode = response.getStatus();
470 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
471 throw new ApiException(response, "Operation showZoneOverlay failed with error " + statusCode);
474 Type returnType = new TypeToken<Overlay>() {
476 return gson.fromJson(response.getContentAsString(), returnType);
479 public ZoneState showZoneState(Long homeId, Long zoneId) throws IOException, ApiException {
481 // verify the required parameter 'homeId' is set
482 if (homeId == null) {
483 throw new ApiException(400, "Missing the required parameter 'homeId' when calling showZoneState");
486 // verify the required parameter 'zoneId' is set
487 if (zoneId == null) {
488 throw new ApiException(400, "Missing the required parameter 'zoneId' when calling showZoneState");
491 startHttpClient(CLIENT);
493 // create path and map variables
494 String path = "/homes/{home_id}/zones/{zone_id}/state".replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
495 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
497 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.GET).timeout(timeout,
498 TimeUnit.MILLISECONDS);
500 request.accept("application/json");
501 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
503 if (authorizer != null) {
504 authorizer.addAuthorization(request);
507 ContentResponse response;
509 response = request.send();
510 } catch (Exception e) {
511 throw new IOException(e);
514 int statusCode = response.getStatus();
515 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
516 throw new ApiException(response, "Operation showZoneState failed with error " + statusCode);
519 Type returnType = new TypeToken<ZoneState>() {
521 return gson.fromJson(response.getContentAsString(), returnType);
524 public void updatePresenceLock(Long homeId, HomePresence json) throws IOException, ApiException {
526 // verify the required parameter 'homeId' is set
527 if (homeId == null) {
528 throw new ApiException(400, "Missing the required parameter 'homeId' when calling updatePresenceLock");
531 // verify the required parameter 'json' is set
533 throw new ApiException(400, "Missing the required parameter 'json' when calling updatePresenceLock");
536 startHttpClient(CLIENT);
538 // create path and map variables
539 String path = "/homes/{home_id}/presenceLock".replaceAll("\\{" + "home_id" + "\\}", homeId.toString());
541 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.PUT).timeout(timeout,
542 TimeUnit.MILLISECONDS);
544 request.accept("application/json");
545 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
547 if (authorizer != null) {
548 authorizer.addAuthorization(request);
551 String serializedBody = gson.toJson(json);
552 request.content(new StringContentProvider(serializedBody), "application/json");
554 ContentResponse response;
556 response = request.send();
557 } catch (Exception e) {
558 throw new IOException(e);
561 int statusCode = response.getStatus();
562 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
563 throw new ApiException(response, "Operation updatePresenceLock failed with error " + statusCode);
567 public Overlay updateZoneOverlay(Long homeId, Long zoneId, Overlay json) throws IOException, ApiException {
569 // verify the required parameter 'homeId' is set
570 if (homeId == null) {
571 throw new ApiException(400, "Missing the required parameter 'homeId' when calling updateZoneOverlay");
574 // verify the required parameter 'zoneId' is set
575 if (zoneId == null) {
576 throw new ApiException(400, "Missing the required parameter 'zoneId' when calling updateZoneOverlay");
579 // verify the required parameter 'json' is set
581 throw new ApiException(400, "Missing the required parameter 'json' when calling updateZoneOverlay");
584 startHttpClient(CLIENT);
586 // create path and map variables
587 String path = "/homes/{home_id}/zones/{zone_id}/overlay"
588 .replaceAll("\\{" + "home_id" + "\\}", homeId.toString())
589 .replaceAll("\\{" + "zone_id" + "\\}", zoneId.toString());
591 Request request = CLIENT.newRequest(baseUrl + path).method(HttpMethod.PUT).timeout(timeout,
592 TimeUnit.MILLISECONDS);
594 request.accept("application/json");
595 request.header(HttpHeader.USER_AGENT, "openhab/swagger-java/1.0.0");
597 if (authorizer != null) {
598 authorizer.addAuthorization(request);
601 String serializedBody = gson.toJson(json);
602 request.content(new StringContentProvider(serializedBody), "application/json");
604 ContentResponse response;
606 response = request.send();
607 } catch (Exception e) {
608 throw new IOException(e);
611 int statusCode = response.getStatus();
612 if (statusCode >= HttpStatus.BAD_REQUEST_400) {
613 throw new ApiException(response, "Operation updateZoneOverlay failed with error " + statusCode);
616 Type returnType = new TypeToken<Overlay>() {
618 return gson.fromJson(response.getContentAsString(), returnType);
621 private static void startHttpClient(HttpClient client) {
622 if (!client.isStarted()) {
625 } catch (Exception e) {
626 // nothing we can do here