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