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