]> git.basschouten.com Git - openhab-addons.git/blob
149d09bf3611213b4febdd94a0ea2aa778c4ae0e
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.tradfri.internal.handler;
14
15 import static org.openhab.binding.tradfri.internal.TradfriBindingConstants.*;
16
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;
29
30 import com.google.gson.JsonElement;
31
32 /**
33  * The {@link TradfriAirPurifierHandler} is responsible for handling commands and status updates
34  * for Starkvind Air Purifiers.
35  *
36  * @author Vivien Boistuaud - Initial contribution
37  */
38 @NonNullByDefault
39 public class TradfriAirPurifierHandler extends TradfriThingHandler {
40
41     private final Logger logger = LoggerFactory.getLogger(TradfriAirPurifierHandler.class);
42
43     public TradfriAirPurifierHandler(Thing thing) {
44         super(thing);
45     }
46
47     @Override
48     public void handleCommand(ChannelUID channelUID, Command command) {
49         if (active) {
50             if (command instanceof RefreshType) {
51                 TradfriCoapClient coapClient = this.coapClient;
52                 if (coapClient != null) {
53                     logger.debug("Refreshing channel {}", channelUID);
54                     coapClient.asyncGet(this);
55                 } else {
56                     logger.debug("coapClient is null!");
57                 }
58                 return;
59             }
60
61             switch (channelUID.getId()) {
62                 case CHANNEL_FAN_MODE:
63                     handleFanModeCommand(command);
64                     break;
65                 case CHANNEL_DISABLE_LED:
66                     handleDisableLed(command);
67                     break;
68                 case CHANNEL_LOCK_BUTTON:
69                     handleLockButton(command);
70                     break;
71                 default:
72                     logger.error("Unknown channel UID {}", channelUID);
73             }
74         }
75     }
76
77     private void handleFanModeCommand(Command command) {
78         if (command instanceof Number) {
79             set(new TradfriAirPurifierData().setFanMode((Number) command).getJsonString());
80         } else {
81             logger.debug("Cannot handle command '{}' of type {} for channel '{}'", command, command.getClass(),
82                     CHANNEL_FAN_MODE);
83         }
84     }
85
86     private void handleDisableLed(Command command) {
87         if (command instanceof OnOffType) {
88             set(new TradfriAirPurifierData().setDisableLed((OnOffType) command).getJsonString());
89         } else {
90             logger.debug("Cannot handle command '{}' of type {} for channel '{}'", command, command.getClass(),
91                     CHANNEL_DISABLE_LED);
92         }
93     }
94
95     private void handleLockButton(Command command) {
96         if (command instanceof OnOffType) {
97             set(new TradfriAirPurifierData().setLockPhysicalButton((OnOffType) command).getJsonString());
98         } else {
99             logger.debug("Cannot handle command '{}' of type {} for channel '{}'", command, command.getClass(),
100                     CHANNEL_DISABLE_LED);
101         }
102     }
103
104     @Override
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);
109
110             State fanMode = state.getFanMode();
111             if (fanMode != null) {
112                 updateState(CHANNEL_FAN_MODE, fanMode);
113             }
114
115             State fanSpeed = state.getFanSpeed();
116             if (fanSpeed != null) {
117                 updateState(CHANNEL_FAN_SPEED, fanSpeed);
118             }
119
120             State disableLed = state.getDisableLed();
121             if (disableLed != null) {
122                 updateState(CHANNEL_DISABLE_LED, disableLed);
123             }
124
125             State lockPhysicalButton = state.getLockPhysicalButton();
126             if (lockPhysicalButton != null) {
127                 updateState(CHANNEL_LOCK_BUTTON, lockPhysicalButton);
128             }
129
130             State airQualityPm25 = state.getAirQualityPM25();
131             if (airQualityPm25 != null) {
132                 updateState(CHANNEL_AIR_QUALITY_PM25, airQualityPm25);
133             }
134
135             State airQualityRating = state.getAirQualityRating();
136             if (airQualityRating != null) {
137                 updateState(CHANNEL_AIR_QUALITY_RATING, airQualityRating);
138             }
139
140             State nextFilterCheckTTL = state.getNextFilterCheckTTL();
141             if (nextFilterCheckTTL != null) {
142                 updateState(CHANNEL_FILTER_CHECK_NEXT, nextFilterCheckTTL);
143             }
144
145             State filterCheckAlarm = state.getFilterCheckAlarm();
146             if (filterCheckAlarm != null) {
147                 updateState(CHANNEL_FILTER_CHECK_ALARM, filterCheckAlarm);
148             }
149
150             State filterUptime = state.getFilterUptime();
151             if (filterUptime != null) {
152                 updateState(CHANNEL_FILTER_UPTIME, filterUptime);
153             }
154
155             logger.debug(
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());
161         }
162     }
163 }