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.handler;
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.tradfri.internal.TradfriCoapClient;
19 import org.openhab.binding.tradfri.internal.model.TradfriAirPurifierData;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.openhab.core.types.State;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.google.gson.JsonElement;
33 * The {@link TradfriAirPurifierHandler} is responsible for handling commands and status updates
34 * for Starkvind Air Purifiers.
36 * @author Vivien Boistuaud - Initial contribution
39 public class TradfriAirPurifierHandler extends TradfriThingHandler {
41 private final Logger logger = LoggerFactory.getLogger(TradfriAirPurifierHandler.class);
43 public TradfriAirPurifierHandler(Thing thing) {
48 public void handleCommand(ChannelUID channelUID, Command command) {
50 if (command instanceof RefreshType) {
51 TradfriCoapClient coapClient = this.coapClient;
52 if (coapClient != null) {
53 logger.debug("Refreshing channel {}", channelUID);
54 coapClient.asyncGet(this);
56 logger.debug("coapClient is null!");
61 switch (channelUID.getId()) {
62 case CHANNEL_FAN_MODE:
63 handleFanModeCommand(command);
65 case CHANNEL_DISABLE_LED:
66 handleDisableLed(command);
68 case CHANNEL_LOCK_BUTTON:
69 handleLockButton(command);
72 logger.error("Unknown channel UID {}", channelUID);
77 private void handleFanModeCommand(Command command) {
78 if (command instanceof Number) {
79 set(new TradfriAirPurifierData().setFanMode((Number) command).getJsonString());
81 logger.debug("Cannot handle command '{}' of type {} for channel '{}'", command, command.getClass(),
86 private void handleDisableLed(Command command) {
87 if (command instanceof OnOffType) {
88 set(new TradfriAirPurifierData().setDisableLed((OnOffType) command).getJsonString());
90 logger.debug("Cannot handle command '{}' of type {} for channel '{}'", command, command.getClass(),
95 private void handleLockButton(Command command) {
96 if (command instanceof OnOffType) {
97 set(new TradfriAirPurifierData().setLockPhysicalButton((OnOffType) command).getJsonString());
99 logger.debug("Cannot handle command '{}' of type {} for channel '{}'", command, command.getClass(),
100 CHANNEL_DISABLE_LED);
105 public void onUpdate(JsonElement data) {
106 if (active && !(data.isJsonNull())) {
107 TradfriAirPurifierData state = new TradfriAirPurifierData(data);
108 updateStatus(state.getReachabilityStatus() ? ThingStatus.ONLINE : ThingStatus.OFFLINE);
110 State fanMode = state.getFanMode();
111 if (fanMode != null) {
112 updateState(CHANNEL_FAN_MODE, fanMode);
115 State fanSpeed = state.getFanSpeed();
116 if (fanSpeed != null) {
117 updateState(CHANNEL_FAN_SPEED, fanSpeed);
120 State disableLed = state.getDisableLed();
121 if (disableLed != null) {
122 updateState(CHANNEL_DISABLE_LED, disableLed);
125 State lockPhysicalButton = state.getLockPhysicalButton();
126 if (lockPhysicalButton != null) {
127 updateState(CHANNEL_LOCK_BUTTON, lockPhysicalButton);
130 State airQualityPm25 = state.getAirQualityPM25();
131 if (airQualityPm25 != null) {
132 updateState(CHANNEL_AIR_QUALITY_PM25, airQualityPm25);
135 State airQualityRating = state.getAirQualityRating();
136 if (airQualityRating != null) {
137 updateState(CHANNEL_AIR_QUALITY_RATING, airQualityRating);
140 State nextFilterCheckTTL = state.getNextFilterCheckTTL();
141 if (nextFilterCheckTTL != null) {
142 updateState(CHANNEL_FILTER_CHECK_NEXT, nextFilterCheckTTL);
145 State filterCheckAlarm = state.getFilterCheckAlarm();
146 if (filterCheckAlarm != null) {
147 updateState(CHANNEL_FILTER_CHECK_ALARM, filterCheckAlarm);
150 State filterUptime = state.getFilterUptime();
151 if (filterUptime != null) {
152 updateState(CHANNEL_FILTER_UPTIME, filterUptime);
156 "Updating thing for airPurifierId {} to state {fanMode: {}, fanSpeed: {}, disableLed: {}, lockButton: {}, airQualityPm25: {}, airQualityRating: {}, nextFilterCheckTTL: {}, filterCheckAlarm: {}, filterUptime: {}, firmwareVersion: {}, modelId: {}, vendor: {}}",
157 state.getDeviceId(), state.getFanMode(), state.getFanSpeed(), state.getDisableLed(),
158 state.getLockPhysicalButton(), state.getAirQualityPM25(), state.getAirQualityRating(),
159 state.getNextFilterCheckTTL(), state.getFilterCheckAlarm(), state.getFilterUptime(),
160 state.getFirmwareVersion(), state.getModelId(), state.getVendor());