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.Duration;
16 import java.time.LocalTime;
17 import java.util.List;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.library.types.QuantityType;
22 import org.openhab.core.library.unit.Units;
23 import org.openhab.core.types.State;
24 import org.openhab.core.types.UnDefType;
26 import com.google.gson.annotations.SerializedName;
29 * This class represents the alarm state from the API.
31 * @author Michael Myrcik - Initial contribution
33 public class AlarmStateData {
35 private static final int POWER_WAKE_ENABLED = 255;
37 @SerializedName("prfen")
38 private List<Boolean> enabled;
40 @SerializedName("prfvs")
41 private List<Boolean> configured;
43 @SerializedName("pwrsv")
44 private List<Integer> powerWake;
46 public @NonNull State getEnabledState(int position) {
47 final List<Boolean> enabled = this.enabled;
48 if (enabled == null) {
49 return UnDefType.NULL;
51 return OnOffType.from(enabled.get(position - 1));
54 public @NonNull State getConfiguredState(int position) {
55 final List<Boolean> configured = this.configured;
56 if (configured == null) {
57 return UnDefType.NULL;
59 return OnOffType.from(configured.get(position - 1));
62 public @NonNull State getPowerWakeState(int position) {
63 final List<Integer> powerWake = this.powerWake;
64 if (powerWake == null) {
65 return UnDefType.NULL;
69 return OnOffType.from((int) powerWake.get(position) == POWER_WAKE_ENABLED);
72 public @NonNull State getPowerWakeDelayState(int position, LocalTime alarmTime) {
73 final State powerWakeState = getPowerWakeState(position);
74 if (UnDefType.NULL.equals(powerWakeState)) {
75 return UnDefType.NULL;
77 if (OnOffType.OFF.equals(powerWakeState)) {
78 return QuantityType.valueOf(0, Units.MINUTE);
80 if (alarmTime == null) {
81 return UnDefType.NULL;
84 final LocalTime powerWakeTime = getPowerWakeTime(position);
85 if (powerWakeTime == null) {
86 return UnDefType.NULL;
89 Duration delay = Duration.between(alarmTime, powerWakeTime);
91 return UnDefType.NULL;
94 return QuantityType.valueOf(delay.toMinutes(), Units.MINUTE);
97 private LocalTime getPowerWakeTime(int position) {
98 final List<Integer> powerWake = this.powerWake;
99 if (powerWake == null) {
104 final Integer hour = powerWake.get(position + 1);
105 final Integer minute = powerWake.get(position + 2);
106 return LocalTime.of(hour, minute);
109 public int getAlarmCount() {
110 final List<Boolean> configured = this.configured;
111 if (configured == null) {
114 return configured.size();