2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.somneo.internal.model;
15 import java.time.LocalTime;
16 import java.time.ZonedDateTime;
18 import javax.measure.quantity.Time;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.core.library.types.DateTimeType;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.OnOffType;
25 import org.openhab.core.library.types.PercentType;
26 import org.openhab.core.library.types.QuantityType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.library.unit.Units;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
32 import com.google.gson.annotations.SerializedName;
35 * This class represents the audio state from the API.
37 * @author Michael Myrcik - Initial contribution
40 public class AlarmSettingsData {
42 private static final int POWER_WAKE_ENABLED = 255;
43 private static final int POWER_WAKE_DISABLED = 0;
45 @SerializedName("prfnr")
46 private @Nullable Integer position;
48 @SerializedName("prfvs")
49 private @Nullable Boolean configured;
51 @SerializedName("prfen")
52 private @Nullable Boolean enabled;
64 @SerializedName("daynm")
65 private @Nullable Integer repeatDay;
67 @SerializedName("almhr")
68 private @Nullable Integer hour;
70 @SerializedName("almmn")
71 private @Nullable Integer minute;
74 * Brightness range from 0 to 25.
76 @SerializedName("curve")
77 private @Nullable Integer sunriseBrightness;
79 @SerializedName("durat")
80 private @Nullable Integer sunriseDurationInMin;
82 @SerializedName("ctype")
83 private @Nullable Integer sunriseSchema;
85 @SerializedName("snddv")
86 private @Nullable String soundSource;
88 @SerializedName("sndch")
89 private @Nullable String soundChannel;
91 @SerializedName("sndlv")
92 private @Nullable Integer soundVolume;
94 @SerializedName("pwrsz")
95 private @Nullable Integer powerWake;
97 @SerializedName("pszhr")
98 private @Nullable Integer powerWakeHour;
100 @SerializedName("pszmn")
101 private @Nullable Integer powerWakeMinute;
103 public void setPosition(Integer position) {
104 this.position = position;
107 public State getConfiguredState() {
108 final Boolean configured = this.configured;
109 if (configured == null) {
110 return UnDefType.NULL;
112 return OnOffType.from(configured);
115 public void setConfigured(Boolean configured) {
116 this.configured = configured;
119 public State getEnabledState() {
120 final Boolean enabled = this.enabled;
121 if (enabled == null) {
122 return UnDefType.NULL;
124 return OnOffType.from(enabled);
127 public void setEnabled(boolean enabled) {
128 this.enabled = enabled;
131 public void setEnabledState(OnOffType enabled) {
132 setEnabled(OnOffType.ON.equals(enabled));
135 public void setAlarmTime(LocalTime time) {
136 Integer powerWakeDelay = getPowerWakeDelay();
137 if (powerWakeDelay == null) {
140 this.hour = time.getHour();
141 this.minute = time.getMinute();
143 final Integer powerWake = this.powerWake;
144 if (powerWake != null && powerWake.intValue() == POWER_WAKE_ENABLED) {
145 LocalTime powerWakeTime = time.plusMinutes(powerWakeDelay);
146 this.powerWakeHour = powerWakeTime.getHour();
147 this.powerWakeMinute = powerWakeTime.getMinute();
151 public void setAlarmTime(DateTimeType timeState) {
152 final ZonedDateTime zonedTime = timeState.getZonedDateTime();
153 final LocalTime time = LocalTime.of(zonedTime.getHour(), zonedTime.getMinute());
160 public State getRepeatDayState() {
161 final Integer repeatDay = this.repeatDay;
162 if (repeatDay == null) {
163 return UnDefType.NULL;
165 return new DecimalType(repeatDay);
168 public void setRepeatDay(int days) {
169 this.repeatDay = days;
172 public void setRepeatDayState(DecimalType days) {
173 setRepeatDay(days.intValue());
176 public State getAlarmTimeState() {
177 final Integer hour = this.hour;
179 return UnDefType.NULL;
181 final Integer minute = this.minute;
182 if (minute == null) {
183 return UnDefType.NULL;
185 final String alarmTimeString = String.format("%02d:%02d:00", hour, minute);
186 if (alarmTimeString == null) {
187 return UnDefType.NULL;
189 return DateTimeType.valueOf(alarmTimeString);
192 public State getPowerWakeState() {
193 final Integer powerWake = this.powerWake;
194 if (powerWake == null) {
195 return UnDefType.NULL;
197 return OnOffType.from((int) powerWake == POWER_WAKE_ENABLED);
200 public void setPowerWakeState(OnOffType state) {
201 if (OnOffType.ON.equals(state)) {
202 powerWake = POWER_WAKE_ENABLED;
203 powerWakeHour = hour;
204 powerWakeMinute = minute;
206 powerWake = POWER_WAKE_DISABLED;
212 public @Nullable Integer getPowerWakeDelay() {
213 if (OnOffType.OFF.equals(getPowerWakeState())) {
214 return Integer.valueOf(0);
216 final Integer hour = this.hour;
220 final Integer minute = this.minute;
221 if (minute == null) {
224 final Integer powerWakeHour = this.powerWakeHour;
225 if (powerWakeHour == null) {
228 final Integer powerWakeMinute = this.powerWakeMinute;
229 if (powerWakeMinute == null) {
232 int delay = powerWakeMinute - minute;
233 if (!powerWakeHour.equals(hour)) {
234 // Simplify the algorithm, as the delta cannot be greater than 59 minutes.
240 public void setPowerWakeDelay(int minutes) {
241 final Integer hour = this.hour;
245 final Integer minute = this.minute;
246 if (minute == null) {
250 LocalTime localTime = LocalTime.of(hour, minute);
251 localTime = localTime.plusMinutes(minutes);
253 this.powerWake = POWER_WAKE_ENABLED;
254 this.powerWakeHour = localTime.getHour();
255 this.powerWakeMinute = localTime.getMinute();
258 public void setPowerWakeDelayState(QuantityType<Time> time) {
259 setPowerWakeDelay(time.intValue());
262 public State getPowerWakeDelayState() {
263 final Integer delay = getPowerWakeDelay();
265 return UnDefType.NULL;
267 return new QuantityType<>(delay, Units.MINUTE);
270 public State getSunriseBrightness() {
271 final Integer sunriseBrightness = this.sunriseBrightness;
272 if (sunriseBrightness == null) {
273 return UnDefType.NULL;
275 return new PercentType(sunriseBrightness * 4);
278 public void setSunriseBrightnessState(PercentType percent) {
279 sunriseBrightness = percent.intValue() / 4;
282 public State getSunriseDurationInMin() {
283 final Integer sunriseDurationInMin = this.sunriseDurationInMin;
284 if (sunriseDurationInMin == null) {
285 return UnDefType.NULL;
287 return new QuantityType<>(sunriseDurationInMin, Units.MINUTE);
290 public void setSunriseDurationState(QuantityType<Time> duration) {
291 sunriseDurationInMin = duration.intValue();
294 public State getSunriseSchema() {
295 final Integer sunriseSchema = this.sunriseSchema;
296 if (sunriseSchema == null) {
297 return UnDefType.NULL;
299 return new DecimalType(sunriseSchema);
302 public void setSunriseSchemaState(DecimalType schema) {
303 this.sunriseSchema = schema.intValue();
306 public State getSound() {
307 final String soundSource = this.soundSource;
308 if (soundSource == null) {
309 return UnDefType.NULL;
311 final String suffix = "off".equals(soundSource) ? "" : "-" + soundChannel;
312 return new StringType(soundSource + suffix);
315 public void setAlarmSoundState(StringType sound) {
316 final String[] values = sound.toFullString().split("-");
317 soundSource = values[0];
318 soundChannel = values.length == 1 ? "" : values[1];
321 public State getSoundVolume() {
322 final Integer soundVolume = this.soundVolume;
323 if (soundVolume == null) {
324 return UnDefType.NULL;
326 return new PercentType(soundVolume * 4);
329 public void setAlarmVolumeState(PercentType volume) {
330 soundVolume = volume.intValue() / 4;
333 public static AlarmSettingsData withDefaultValues(int position) {
334 final AlarmSettingsData data = new AlarmSettingsData();
335 data.position = position;
336 data.configured = false;
337 data.enabled = false;
340 data.powerWake = POWER_WAKE_DISABLED;
341 data.powerWakeHour = 0;
342 data.powerWakeMinute = 0;
343 data.sunriseSchema = 0;
344 data.sunriseBrightness = 20;
345 data.sunriseDurationInMin = 30;
346 data.repeatDay = 254;
347 data.soundSource = "wus";
348 data.soundChannel = "1";
349 data.soundVolume = 12;