]> git.basschouten.com Git - openhab-addons.git/blob
a6b11377a912f06a511b053328ba432fafcbbfaa
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.tado.internal.builder;
14
15 import java.io.IOException;
16
17 import org.openhab.binding.tado.internal.TadoBindingConstants.FanSpeed;
18 import org.openhab.binding.tado.internal.TadoBindingConstants.HvacMode;
19 import org.openhab.binding.tado.internal.TadoBindingConstants.TemperatureUnit;
20 import org.openhab.binding.tado.internal.api.ApiException;
21 import org.openhab.binding.tado.internal.api.model.GenericZoneCapabilities;
22 import org.openhab.binding.tado.internal.api.model.GenericZoneSetting;
23 import org.openhab.binding.tado.internal.api.model.TemperatureObject;
24 import org.openhab.binding.tado.internal.handler.TadoZoneHandler;
25
26 /**
27  * Base class for zone settings builder.
28  *
29  * @author Dennis Frommknecht - Initial contribution
30  */
31 public abstract class ZoneSettingsBuilder {
32     public static ZoneSettingsBuilder of(TadoZoneHandler zoneHandler) {
33         switch (zoneHandler.getZoneType()) {
34             case HEATING:
35                 return new HeatingZoneSettingsBuilder();
36             case AIR_CONDITIONING:
37                 return new AirConditioningZoneSettingsBuilder();
38             case HOT_WATER:
39                 return new HotWaterZoneSettingsBuilder();
40             default:
41                 throw new IllegalArgumentException("Zone type " + zoneHandler.getZoneType() + " unknown");
42         }
43     }
44
45     protected HvacMode mode = null;
46     protected Float temperature = null;
47     protected TemperatureUnit temperatureUnit = TemperatureUnit.CELSIUS;
48     protected Boolean swing = null;
49     protected FanSpeed fanSpeed = null;
50
51     public ZoneSettingsBuilder withMode(HvacMode mode) {
52         this.mode = mode;
53         return this;
54     }
55
56     public ZoneSettingsBuilder withTemperature(Float temperature, TemperatureUnit temperatureUnit) {
57         this.temperature = temperature;
58         this.temperatureUnit = temperatureUnit;
59         return this;
60     }
61
62     public ZoneSettingsBuilder withSwing(boolean swingOn) {
63         this.swing = swingOn;
64         return this;
65     }
66
67     public ZoneSettingsBuilder withFanSpeed(FanSpeed fanSpeed) {
68         this.fanSpeed = fanSpeed;
69         return this;
70     }
71
72     public abstract GenericZoneSetting build(ZoneStateProvider zoneStateProvider, GenericZoneCapabilities capabilities)
73             throws IOException, ApiException;
74
75     protected TemperatureObject truncateTemperature(TemperatureObject temperature) {
76         if (temperature == null) {
77             return null;
78         }
79
80         TemperatureObject temperatureObject = new TemperatureObject();
81         if (temperatureUnit == TemperatureUnit.FAHRENHEIT) {
82             temperatureObject.setFahrenheit(temperature.getFahrenheit());
83         } else {
84             temperatureObject.setCelsius(temperature.getCelsius());
85         }
86
87         return temperatureObject;
88     }
89
90     protected TemperatureObject buildDefaultTemperatureObject(float temperatureCelsius, float temperatureFahrenheit) {
91         TemperatureObject temperatureObject = new TemperatureObject();
92
93         if (temperatureUnit == TemperatureUnit.FAHRENHEIT) {
94             temperatureObject.setFahrenheit(temperatureFahrenheit);
95         } else {
96             temperatureObject.setCelsius(temperatureCelsius);
97         }
98
99         return temperatureObject;
100     }
101 }