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