]> git.basschouten.com Git - openhab-addons.git/blob
de47df19fcb0b8ccfe7a9bb68b7b8eaddb131b0c
[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;
14
15 import java.io.IOException;
16
17 import org.openhab.binding.tado.internal.TadoBindingConstants.FanLevel;
18 import org.openhab.binding.tado.internal.TadoBindingConstants.FanSpeed;
19 import org.openhab.binding.tado.internal.TadoBindingConstants.HorizontalSwing;
20 import org.openhab.binding.tado.internal.TadoBindingConstants.HvacMode;
21 import org.openhab.binding.tado.internal.TadoBindingConstants.OperationMode;
22 import org.openhab.binding.tado.internal.TadoBindingConstants.VerticalSwing;
23 import org.openhab.binding.tado.internal.api.ApiException;
24 import org.openhab.binding.tado.internal.api.model.GenericZoneSetting;
25 import org.openhab.binding.tado.internal.api.model.Overlay;
26 import org.openhab.binding.tado.internal.api.model.OverlayTerminationCondition;
27 import org.openhab.binding.tado.internal.api.model.OverlayTerminationConditionType;
28 import org.openhab.binding.tado.internal.builder.TerminationConditionBuilder;
29 import org.openhab.binding.tado.internal.builder.ZoneSettingsBuilder;
30 import org.openhab.binding.tado.internal.builder.ZoneStateProvider;
31 import org.openhab.binding.tado.internal.handler.TadoZoneHandler;
32 import org.openhab.core.thing.Thing;
33
34 /**
35  * Builder for incremental creation of zone overlays.
36  *
37  * @author Dennis Frommknecht - Initial contribution
38  */
39 public class TadoHvacChange {
40     private TadoZoneHandler zoneHandler;
41
42     private boolean followSchedule = false;
43     private TerminationConditionBuilder terminationConditionBuilder;
44     private ZoneSettingsBuilder settingsBuilder;
45
46     public TadoHvacChange(Thing zoneThing) {
47         if (!(zoneThing.getHandler() instanceof TadoZoneHandler)) {
48             throw new IllegalArgumentException("TadoZoneThing expected, but instead got " + zoneThing);
49         }
50
51         this.zoneHandler = (TadoZoneHandler) zoneThing.getHandler();
52         this.terminationConditionBuilder = TerminationConditionBuilder.of(zoneHandler);
53         this.settingsBuilder = ZoneSettingsBuilder.of(zoneHandler);
54     }
55
56     public TadoHvacChange withOperationMode(OperationMode operationMode) {
57         switch (operationMode) {
58             case SCHEDULE:
59                 return followSchedule();
60             case MANUAL:
61                 return activeForever();
62             case TIMER:
63                 return activeFor(null);
64             case UNTIL_CHANGE:
65                 return activeUntilChange();
66             default:
67                 return this;
68         }
69     }
70
71     public TadoHvacChange followSchedule() {
72         followSchedule = true;
73         return this;
74     }
75
76     public TadoHvacChange activeForever() {
77         terminationConditionBuilder.withTerminationType(OverlayTerminationConditionType.MANUAL);
78         return this;
79     }
80
81     public TadoHvacChange activeUntilChange() {
82         terminationConditionBuilder.withTerminationType(OverlayTerminationConditionType.TADO_MODE);
83         return this;
84     }
85
86     public TadoHvacChange activeFor(Integer minutes) {
87         terminationConditionBuilder.withTerminationType(OverlayTerminationConditionType.TIMER);
88         terminationConditionBuilder.withTimerDurationInSeconds(minutes != null ? minutes * 60 : null);
89         return this;
90     }
91
92     public TadoHvacChange withTemperature(float temperatureValue) {
93         settingsBuilder.withTemperature(temperatureValue, zoneHandler.getTemperatureUnit());
94         return this;
95     }
96
97     public TadoHvacChange withHvacMode(HvacMode mode) {
98         settingsBuilder.withMode(mode);
99         return this;
100     }
101
102     public TadoHvacChange withHvacMode(String mode) {
103         return withHvacMode(HvacMode.valueOf(mode.toUpperCase()));
104     }
105
106     public TadoHvacChange withSwing(boolean swingOn) {
107         settingsBuilder.withSwing(swingOn);
108         return this;
109     }
110
111     public TadoHvacChange withFanSpeed(FanSpeed fanSpeed) {
112         settingsBuilder.withFanSpeed(fanSpeed);
113         return this;
114     }
115
116     public TadoHvacChange withFanSpeed(String fanSpeed) {
117         withFanSpeed(FanSpeed.valueOf(fanSpeed.toUpperCase()));
118         return this;
119     }
120
121     public TadoHvacChange withFanLevel(FanLevel fanLevel) {
122         settingsBuilder.withFanLevel(fanLevel);
123         return this;
124     }
125
126     public TadoHvacChange withHorizontalSwing(HorizontalSwing horizontalSwing) {
127         settingsBuilder.withHorizontalSwing(horizontalSwing);
128         return this;
129     }
130
131     public TadoHvacChange withVerticalSwing(VerticalSwing verticalSwing) {
132         settingsBuilder.withVerticalSwing(verticalSwing);
133         return this;
134     }
135
136     public void apply() throws IOException, ApiException {
137         if (followSchedule) {
138             zoneHandler.removeOverlay();
139         } else {
140             Overlay overlay = buildOverlay();
141             zoneHandler.setOverlay(overlay);
142         }
143     }
144
145     private Overlay buildOverlay() throws IOException, ApiException {
146         ZoneStateProvider zoneStateProvider = new ZoneStateProvider(zoneHandler);
147         OverlayTerminationCondition terminationCondition = terminationConditionBuilder.build(zoneStateProvider);
148         GenericZoneSetting setting = settingsBuilder.build(zoneStateProvider, zoneHandler.getZoneCapabilities());
149
150         Overlay overlay = new Overlay();
151         overlay.setTermination(terminationCondition);
152         overlay.setSetting(setting);
153
154         return overlay;
155     }
156
157     public TadoHvacChange withLight(boolean lightOn) {
158         settingsBuilder.withLight(lightOn);
159         return this;
160     }
161 }