]> git.basschouten.com Git - openhab-addons.git/blob
b024141e6ea8dd3caa2a73f5b12599b9e361855f
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.sensibo.internal.model;
14
15 import java.util.Arrays;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.Optional;
21
22 import javax.measure.Unit;
23 import javax.measure.quantity.Temperature;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.eclipse.jdt.annotation.NonNullByDefault;
27 import org.openhab.binding.sensibo.internal.SensiboTemperatureUnitConverter;
28 import org.openhab.binding.sensibo.internal.dto.poddetails.ModeCapabilityDTO;
29 import org.openhab.binding.sensibo.internal.dto.poddetails.PodDetailsDTO;
30 import org.openhab.binding.sensibo.internal.dto.poddetails.TemperatureDTO;
31 import org.openhab.core.thing.Thing;
32
33 /**
34  * The {@link SensiboSky} represents a Sensibo Sky unit
35  *
36  * @author Arne Seime - Initial contribution
37  */
38 @NonNullByDefault
39 public class SensiboSky extends Pod {
40     private final String macAddress;
41     private final String firmwareVersion;
42     private final String firmwareType;
43     private final String serialNumber;
44     private final String productModel;
45     private final String roomName;
46     private final Unit<Temperature> temperatureUnit;
47     private final String originalTemperatureUnit;
48     private final Double temperature;
49     private final Double humidity;
50     private final boolean alive;
51     private final Map<String, ModeCapabilityDTO> remoteCapabilities;
52     private Schedule[] schedules = new Schedule[0];
53     private Optional<AcState> acState = Optional.empty();
54     private Optional<Timer> timer = Optional.empty();
55
56     public SensiboSky(final PodDetailsDTO dto) {
57         super(dto.id);
58         this.macAddress = StringUtils.remove(dto.macAddress, ':');
59         this.firmwareVersion = dto.firmwareVersion;
60         this.firmwareType = dto.firmwareType;
61         this.serialNumber = dto.serialNumber;
62         this.originalTemperatureUnit = dto.temperatureUnit;
63         this.temperatureUnit = SensiboTemperatureUnitConverter.parseFromSensiboFormat(dto.temperatureUnit);
64         this.productModel = dto.productModel;
65
66         if (dto.acState != null) {
67             this.acState = Optional.of(new AcState(dto.acState));
68         }
69
70         if (dto.timer != null) {
71             this.timer = Optional.of(new Timer(dto.timer));
72         }
73
74         this.temperature = dto.lastMeasurement.temperature;
75         this.humidity = dto.lastMeasurement.humidity;
76
77         this.alive = dto.isAlive();
78         if (dto.getRemoteCapabilities() != null) {
79             this.remoteCapabilities = dto.getRemoteCapabilities();
80         } else {
81             this.remoteCapabilities = new HashMap<>();
82         }
83         this.roomName = dto.getRoomName();
84
85         if (dto.schedules != null) {
86             schedules = Arrays.stream(dto.schedules).map(Schedule::new).toArray(Schedule[]::new);
87         }
88     }
89
90     public String getOriginalTemperatureUnit() {
91         return originalTemperatureUnit;
92     }
93
94     public String getRoomName() {
95         return roomName;
96     }
97
98     public Schedule[] getSchedules() {
99         return schedules;
100     }
101
102     public String getMacAddress() {
103         return macAddress;
104     }
105
106     public String getFirmwareVersion() {
107         return firmwareVersion;
108     }
109
110     public String getFirmwareType() {
111         return firmwareType;
112     }
113
114     public String getSerialNumber() {
115         return serialNumber;
116     }
117
118     public Unit<Temperature> getTemperatureUnit() {
119         return temperatureUnit;
120     }
121
122     public String getProductModel() {
123         return productModel;
124     }
125
126     public Optional<AcState> getAcState() {
127         return acState;
128     }
129
130     public String getProductName() {
131         switch (productModel) {
132             case "skyv2":
133                 return String.format("Sensibo Sky %s", roomName);
134             default:
135                 return String.format("%s %s", productModel, roomName);
136         }
137     }
138
139     public Double getTemperature() {
140         return temperature;
141     }
142
143     public Double getHumidity() {
144         return humidity;
145     }
146
147     public boolean isAlive() {
148         return alive;
149     }
150
151     public Map<String, ModeCapabilityDTO> getRemoteCapabilities() {
152         return remoteCapabilities;
153     }
154
155     public Optional<ModeCapabilityDTO> getCurrentModeCapabilities() {
156         if (acState.isPresent() && acState.get().getMode() != null) {
157             return Optional.ofNullable(remoteCapabilities.get(acState.get().getMode()));
158         } else {
159             return Optional.empty();
160         }
161     }
162
163     public List<Integer> getTargetTemperatures() {
164         Optional<ModeCapabilityDTO> currentModeCapabilities = getCurrentModeCapabilities();
165         if (currentModeCapabilities.isPresent()) {
166             TemperatureDTO selectedTemperatureRange = currentModeCapabilities.get().temperatures
167                     .get(originalTemperatureUnit);
168             if (selectedTemperatureRange != null) {
169                 return selectedTemperatureRange.validValues;
170             }
171         }
172         return Collections.emptyList();
173     }
174
175     /**
176      * @param newAcState an updated ac state
177      */
178     public void updateAcState(AcState newAcState) {
179         this.acState = Optional.of(newAcState);
180     }
181
182     public Optional<Timer> getTimer() {
183         return timer;
184     }
185
186     public Map<String, String> getThingProperties() {
187         final Map<String, String> properties = new HashMap<>();
188         properties.put(Thing.PROPERTY_VENDOR, "Sensibo");
189         properties.put("podId", id);
190         properties.put(Thing.PROPERTY_MAC_ADDRESS, macAddress);
191         properties.put(Thing.PROPERTY_SERIAL_NUMBER, serialNumber);
192         properties.put(Thing.PROPERTY_MODEL_ID, productModel);
193         properties.put(Thing.PROPERTY_FIRMWARE_VERSION, firmwareVersion);
194         properties.put("firmwareType", firmwareType);
195
196         return properties;
197     }
198 }