]> git.basschouten.com Git - openhab-addons.git/blob
d6d35896571c9e1190246460d4b96919f81b6ccf
[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 static org.openhab.binding.tado.internal.api.TadoApiTypeUtils.temperature;
16
17 import java.io.IOException;
18
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.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.HeatingZoneSetting;
28 import org.openhab.binding.tado.internal.api.model.Power;
29 import org.openhab.binding.tado.internal.api.model.TadoSystemType;
30 import org.openhab.binding.tado.internal.api.model.TemperatureObject;
31
32 /**
33  * Builder for incremental creation of heating zone settings.
34  *
35  * @author Dennis Frommknecht - Initial contribution
36  */
37 public class HeatingZoneSettingsBuilder extends ZoneSettingsBuilder {
38     private static final float DEFAULT_TEMPERATURE_C = 22.0f;
39     private static final float DEFAULT_TEMPERATURE_F = 72.0f;
40
41     @Override
42     public ZoneSettingsBuilder withSwing(boolean swingOn) {
43         throw new IllegalArgumentException("Heating zones don't support SWING");
44     }
45
46     @Override
47     public ZoneSettingsBuilder withLight(boolean lightOn) {
48         throw new IllegalArgumentException("Heating zones don't support LIGHT");
49     }
50
51     @Override
52     public ZoneSettingsBuilder withFanSpeed(FanSpeed fanSpeed) {
53         throw new IllegalArgumentException("Heating zones don't support FAN SPEED");
54     }
55
56     @Override
57     public ZoneSettingsBuilder withFanLevel(FanLevel fanLevel) {
58         throw new IllegalArgumentException("Heating zones don't support FAN LEVEL");
59     }
60
61     @Override
62     public ZoneSettingsBuilder withHorizontalSwing(HorizontalSwing horizontalSwing) {
63         throw new IllegalArgumentException("Heating zones don't support HORIZONTAL SWING");
64     }
65
66     @Override
67     public ZoneSettingsBuilder withVerticalSwing(VerticalSwing verticalSwing) {
68         throw new IllegalArgumentException("Heating zones don't support VERTICAL SWING");
69     }
70
71     @Override
72     public GenericZoneSetting build(ZoneStateProvider zoneStateProvider, GenericZoneCapabilities capabilities)
73             throws IOException, ApiException {
74         if (mode == HvacMode.OFF) {
75             return heatingSetting(false);
76         }
77
78         HeatingZoneSetting setting = heatingSetting(true);
79
80         if (temperature != null) {
81             setting.setTemperature(temperature(temperature, temperatureUnit));
82         }
83
84         addMissingSettingParts(setting, zoneStateProvider);
85
86         return setting;
87     }
88
89     private void addMissingSettingParts(HeatingZoneSetting setting, ZoneStateProvider zoneStateProvider)
90             throws IOException, ApiException {
91         if (setting.getTemperature() == null) {
92             TemperatureObject temperatureObject = getCurrentOrDefaultTemperature(zoneStateProvider);
93             setting.setTemperature(temperatureObject);
94         }
95     }
96
97     private TemperatureObject getCurrentOrDefaultTemperature(ZoneStateProvider zoneStateProvider)
98             throws IOException, ApiException {
99         HeatingZoneSetting zoneSetting = (HeatingZoneSetting) zoneStateProvider.getZoneState().getSetting();
100
101         if (zoneSetting != null && zoneSetting.getTemperature() != null) {
102             return truncateTemperature(zoneSetting.getTemperature());
103         }
104
105         return buildDefaultTemperatureObject(DEFAULT_TEMPERATURE_C, DEFAULT_TEMPERATURE_F);
106     }
107
108     private HeatingZoneSetting heatingSetting(boolean powerOn) {
109         HeatingZoneSetting setting = new HeatingZoneSetting();
110         setting.setType(TadoSystemType.HEATING);
111         setting.setPower(powerOn ? Power.ON : Power.OFF);
112         return setting;
113     }
114 }