]> git.basschouten.com Git - openhab-addons.git/blob
13c64d93219fd846a4737e2cf9a25308ffc5f466
[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.FanSpeed;
20 import org.openhab.binding.tado.internal.TadoBindingConstants.HvacMode;
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.HeatingZoneSetting;
25 import org.openhab.binding.tado.internal.api.model.Power;
26 import org.openhab.binding.tado.internal.api.model.TadoSystemType;
27 import org.openhab.binding.tado.internal.api.model.TemperatureObject;
28
29 /**
30  * Builder for incremental creation of heating zone settings.
31  *
32  * @author Dennis Frommknecht - Initial contribution
33  */
34 public class HeatingZoneSettingsBuilder extends ZoneSettingsBuilder {
35     private static final float DEFAULT_TEMPERATURE_C = 22.0f;
36     private static final float DEFAULT_TEMPERATURE_F = 72.0f;
37
38     @Override
39     public ZoneSettingsBuilder withSwing(boolean swingOn) {
40         throw new IllegalArgumentException("Heating zones don't support SWING");
41     }
42
43     @Override
44     public ZoneSettingsBuilder withFanSpeed(FanSpeed fanSpeed) {
45         throw new IllegalArgumentException("Heating zones don't support FAN SPEED");
46     }
47
48     @Override
49     public GenericZoneSetting build(ZoneStateProvider zoneStateProvider, GenericZoneCapabilities capabilities)
50             throws IOException, ApiException {
51         if (mode == HvacMode.OFF) {
52             return heatingSetting(false);
53         }
54
55         HeatingZoneSetting setting = heatingSetting(true);
56
57         if (temperature != null) {
58             setting.setTemperature(temperature(temperature, temperatureUnit));
59         }
60
61         addMissingSettingParts(setting, zoneStateProvider);
62
63         return setting;
64     }
65
66     private void addMissingSettingParts(HeatingZoneSetting setting, ZoneStateProvider zoneStateProvider)
67             throws IOException, ApiException {
68         if (setting.getTemperature() == null) {
69             TemperatureObject temperatureObject = getCurrentOrDefaultTemperature(zoneStateProvider);
70             setting.setTemperature(temperatureObject);
71         }
72     }
73
74     private TemperatureObject getCurrentOrDefaultTemperature(ZoneStateProvider zoneStateProvider)
75             throws IOException, ApiException {
76         HeatingZoneSetting zoneSetting = (HeatingZoneSetting) zoneStateProvider.getZoneState().getSetting();
77
78         if (zoneSetting != null && zoneSetting.getTemperature() != null) {
79             return truncateTemperature(zoneSetting.getTemperature());
80         }
81
82         return buildDefaultTemperatureObject(DEFAULT_TEMPERATURE_C, DEFAULT_TEMPERATURE_F);
83     }
84
85     private HeatingZoneSetting heatingSetting(boolean powerOn) {
86         HeatingZoneSetting setting = new HeatingZoneSetting();
87         setting.setType(TadoSystemType.HEATING);
88         setting.setPower(powerOn ? Power.ON : Power.OFF);
89         return setting;
90     }
91 }