]> git.basschouten.com Git - openhab-addons.git/blob
c4b061e0062d1f5349ee1807d343c2eed2d7abee
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.util.Objects;
16 import java.util.Optional;
17 import java.util.stream.Stream;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.tado.internal.api.model.AcModeCapabilities;
22 import org.openhab.binding.tado.internal.api.model.AirConditioningCapabilities;
23 import org.openhab.binding.tado.internal.api.model.ControlDevice;
24 import org.openhab.binding.tado.internal.api.model.GenericZoneCapabilities;
25 import org.openhab.binding.tado.internal.api.model.TadoSystemType;
26 import org.openhab.binding.tado.internal.api.model.Zone;
27
28 /**
29  * The {@link CapabilitiesSupport} class checks which type of channels are needed in a thing that is to be built around
30  * the given capabilities argument, and the (optional) zone argument. It iterates over each of the capabilities
31  * argument's mode specific sub-capabilities to determine the maximum super set of all sub-capabilities. And it checks
32  * the capabilities of the optional zone argument too.
33  *
34  * @author Andrew Fiddian-Green - Initial contribution
35  */
36 @NonNullByDefault
37 public class CapabilitiesSupport {
38     private final TadoSystemType type;
39     private boolean light;
40     private boolean swing;
41     private boolean fanLevel;
42     private boolean fanSpeed;
43     private boolean verticalSwing;
44     private boolean horizontalSwing;
45     private boolean batteryLowAlarm;
46
47     public CapabilitiesSupport(GenericZoneCapabilities capabilities, Optional<Zone> zoneOptional) {
48         type = capabilities.getType();
49
50         if (zoneOptional.isPresent()) {
51             Zone zone = zoneOptional.get();
52             if (zone.getDevices() != null) {
53                 batteryLowAlarm = zone.getDevices().stream().map(ControlDevice::getBatteryState)
54                         .filter(Objects::nonNull).count() > 0;
55             }
56         }
57
58         if (!(capabilities instanceof AirConditioningCapabilities)) {
59             return;
60         }
61
62         AirConditioningCapabilities acCapabilities = (AirConditioningCapabilities) capabilities;
63
64         // @formatter:off
65         Stream<@Nullable AcModeCapabilities> allCapabilities = Stream.of(
66                acCapabilities.getCOOL(),
67                acCapabilities.getDRY(),
68                acCapabilities.getHEAT(),
69                acCapabilities.getFAN(),
70                acCapabilities.getAUTO());
71         // @formatter:on
72
73         // iterate over all mode capability elements and build the superset of their inner capabilities
74         allCapabilities.forEach(e -> {
75             if (e != null) {
76                 light |= e.getLight() != null ? e.getLight().size() > 0 : false;
77                 swing |= e.getSwings() != null ? e.getSwings().size() > 0 : false;
78                 fanLevel |= e.getFanLevel() != null ? e.getFanLevel().size() > 0 : false;
79                 fanSpeed |= e.getFanSpeeds() != null ? e.getFanSpeeds().size() > 0 : false;
80                 verticalSwing |= e.getVerticalSwing() != null ? e.getVerticalSwing().size() > 0 : false;
81                 horizontalSwing |= e.getHorizontalSwing() != null ? e.getHorizontalSwing().size() > 0 : false;
82             }
83         });
84     }
85
86     public boolean fanLevel() {
87         return fanLevel;
88     }
89
90     public boolean fanSpeed() {
91         return fanSpeed;
92     }
93
94     public boolean horizontalSwing() {
95         return horizontalSwing;
96     }
97
98     public boolean light() {
99         return light;
100     }
101
102     public boolean swing() {
103         return swing;
104     }
105
106     public boolean verticalSwing() {
107         return verticalSwing;
108     }
109
110     public boolean acPower() {
111         return type == TadoSystemType.AIR_CONDITIONING;
112     }
113
114     public boolean heatingPower() {
115         return type == TadoSystemType.HEATING;
116     }
117
118     public boolean currentTemperature() {
119         return (type == TadoSystemType.AIR_CONDITIONING) || (type == TadoSystemType.HEATING);
120     }
121
122     public boolean humidity() {
123         return (type == TadoSystemType.AIR_CONDITIONING) || (type == TadoSystemType.HEATING);
124     }
125
126     public boolean batteryLowAlarm() {
127         return batteryLowAlarm;
128     }
129
130     public boolean openWindow() {
131         return (type == TadoSystemType.AIR_CONDITIONING) || (type == TadoSystemType.HEATING);
132     }
133 }