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.tradfri.internal.model;
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
17 import javax.measure.quantity.Dimensionless;
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.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import com.google.gson.JsonElement;
32 import com.google.gson.JsonPrimitive;
35 * The {@link TradfriAirPurifierData} class is a Java wrapper for the raw JSON data about the air purifier state.
37 * @author Vivien Boistuaud - Initial contribution
40 public class TradfriAirPurifierData extends TradfriDeviceData {
42 private final Logger logger = LoggerFactory.getLogger(TradfriAirPurifierData.class);
44 public TradfriAirPurifierData() {
48 public TradfriAirPurifierData(JsonElement json) {
49 super(AIR_PURIFIER, json);
52 public String getJsonString() {
53 return root.toString();
56 public @Nullable DecimalType getFanMode() {
57 JsonElement fanMode = attributes.get(FAN_MODE);
58 if (fanMode != null) {
59 int modeValue = fanMode.getAsInt();
60 if (AIR_PURIFIER_FANMODE.contains(modeValue)) {
61 return new DecimalType(modeValue);
63 logger.debug("Invalid speedMode is '{}': unknown value", modeValue);
71 public TradfriAirPurifierData setFanMode(Number speedValue) {
72 int speed = speedValue.intValue();
73 if (AIR_PURIFIER_FANMODE.contains(speed)) {
74 attributes.add(FAN_MODE, new JsonPrimitive(speed));
76 logger.debug("Could not set fanMode to '{}': unknown value", speed);
81 public @Nullable DecimalType getFanSpeed() {
82 JsonElement fanSpeed = attributes.get(FAN_SPEED);
83 if (fanSpeed != null) {
84 int speedValue = fanSpeed.getAsInt();
85 return new DecimalType(speedValue);
91 public @Nullable OnOffType getDisableLed() {
92 JsonElement ledOnOff = attributes.get(LED_DISABLE);
93 if (ledOnOff != null) {
94 boolean ledStatus = ledOnOff.getAsInt() != 0;
95 return OnOffType.from(ledStatus);
101 public TradfriAirPurifierData setDisableLed(OnOffType disableOnOff) {
102 attributes.add(LED_DISABLE, new JsonPrimitive(OnOffType.ON.equals(disableOnOff) ? 1 : 0));
106 public @Nullable OnOffType getLockPhysicalButton() {
107 JsonElement lockPhysicalButton = attributes.get(LOCK_PHYSICAL_BUTTON);
108 if (lockPhysicalButton != null) {
109 boolean isLocked = lockPhysicalButton.getAsInt() != 0;
110 return OnOffType.from(isLocked);
116 public TradfriAirPurifierData setLockPhysicalButton(OnOffType lockPhysicalButton) {
117 attributes.add(LOCK_PHYSICAL_BUTTON, new JsonPrimitive(OnOffType.ON.equals(lockPhysicalButton) ? 1 : 0));
121 public @Nullable State getAirQualityPM25() {
122 JsonElement airQuality = attributes.get(AIR_QUALITY);
123 if (airQuality != null) {
124 int pm25InPpm = airQuality.getAsInt();
125 if (pm25InPpm != AIR_PURIFIER_AIR_QUALITY_UNDEFINED) {
126 return new QuantityType<Dimensionless>(pm25InPpm, Units.PARTS_PER_MILLION);
128 return UnDefType.UNDEF;
135 public @Nullable State getAirQualityRating() {
136 State pm25State = getAirQualityPM25();
137 if (pm25State != null) {
138 if (pm25State instanceof Number number) {
139 int pm25Value = number.intValue();
140 int qualityRating = 1;
142 if (pm25Value >= AIR_PURIFIER_AIR_QUALITY_BAD) {
144 } else if (pm25Value >= AIR_PURIFIER_AIR_QUALITY_OK) {
148 return new DecimalType(qualityRating);
150 return UnDefType.UNDEF;
156 public @Nullable QuantityType<Time> getNextFilterCheckTTL() {
157 JsonElement nextFilterCheckTTL = attributes.get(NEXT_FILTER_CHECK);
158 if (nextFilterCheckTTL != null) {
159 int remainingMinutes = nextFilterCheckTTL.getAsInt();
160 return new QuantityType<Time>(remainingMinutes, Units.MINUTE);
166 public @Nullable OnOffType getFilterCheckAlarm() {
167 QuantityType<Time> ttl = getNextFilterCheckTTL();
169 int ttlValue = ttl.intValue();
170 return ttlValue < 0 ? OnOffType.ON : OnOffType.OFF;
176 public @Nullable QuantityType<Time> getFilterUptime() {
177 JsonElement filterUptime = attributes.get(FILTER_UPTIME);
178 if (filterUptime != null) {
179 int filterUptimeMinutes = filterUptime.getAsInt();
180 return new QuantityType<Time>(filterUptimeMinutes, Units.MINUTE);