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