]> git.basschouten.com Git - openhab-addons.git/blob
de041bffe6836098931ca50a60e5c9b3252c3c58
[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.nest.internal.wwn.handler;
14
15 import static org.openhab.binding.nest.internal.wwn.WWNBindingConstants.*;
16 import static org.openhab.core.thing.Thing.PROPERTY_FIRMWARE_VERSION;
17 import static org.openhab.core.types.RefreshType.REFRESH;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.nest.internal.wwn.dto.WWNSmokeDetector;
22 import org.openhab.binding.nest.internal.wwn.dto.WWNSmokeDetector.BatteryHealth;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.types.Command;
27 import org.openhab.core.types.State;
28 import org.openhab.core.types.UnDefType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The smoke detector handler, it handles the data from WWN for the smoke detector.
34  *
35  * @author David Bennett - Initial contribution
36  * @author Wouter Born - Handle channel refresh command
37  */
38 @NonNullByDefault
39 public class WWNSmokeDetectorHandler extends WWNBaseHandler<WWNSmokeDetector> {
40     private final Logger logger = LoggerFactory.getLogger(WWNSmokeDetectorHandler.class);
41
42     public WWNSmokeDetectorHandler(Thing thing) {
43         super(thing, WWNSmokeDetector.class);
44     }
45
46     @Override
47     protected State getChannelState(ChannelUID channelUID, WWNSmokeDetector smokeDetector) {
48         switch (channelUID.getId()) {
49             case CHANNEL_CO_ALARM_STATE:
50                 return getAsStringTypeOrNull(smokeDetector.getCoAlarmState());
51             case CHANNEL_LAST_CONNECTION:
52                 return getAsDateTimeTypeOrNull(smokeDetector.getLastConnection());
53             case CHANNEL_LAST_MANUAL_TEST_TIME:
54                 return getAsDateTimeTypeOrNull(smokeDetector.getLastManualTestTime());
55             case CHANNEL_LOW_BATTERY:
56                 return getAsOnOffTypeOrNull(smokeDetector.getBatteryHealth() == null ? null
57                         : smokeDetector.getBatteryHealth() == BatteryHealth.REPLACE);
58             case CHANNEL_MANUAL_TEST_ACTIVE:
59                 return getAsOnOffTypeOrNull(smokeDetector.isManualTestActive());
60             case CHANNEL_SMOKE_ALARM_STATE:
61                 return getAsStringTypeOrNull(smokeDetector.getSmokeAlarmState());
62             case CHANNEL_UI_COLOR_STATE:
63                 return getAsStringTypeOrNull(smokeDetector.getUiColorState());
64             default:
65                 logger.error("Unsupported channelId '{}'", channelUID.getId());
66                 return UnDefType.UNDEF;
67         }
68     }
69
70     /**
71      * Handles any incoming command requests.
72      */
73     @Override
74     public void handleCommand(ChannelUID channelUID, Command command) {
75         if (REFRESH.equals(command)) {
76             WWNSmokeDetector lastUpdate = getLastUpdate();
77             if (lastUpdate != null) {
78                 updateState(channelUID, getChannelState(channelUID, lastUpdate));
79             }
80         }
81     }
82
83     @Override
84     protected void update(@Nullable WWNSmokeDetector oldSmokeDetector, WWNSmokeDetector smokeDetector) {
85         logger.debug("Updating {}", getThing().getUID());
86
87         updateLinkedChannels(oldSmokeDetector, smokeDetector);
88         updateProperty(PROPERTY_FIRMWARE_VERSION, smokeDetector.getSoftwareVersion());
89
90         ThingStatus newStatus = smokeDetector.isOnline() == null ? ThingStatus.UNKNOWN
91                 : smokeDetector.isOnline() ? ThingStatus.ONLINE : ThingStatus.OFFLINE;
92         if (newStatus != thing.getStatus()) {
93             updateStatus(newStatus);
94         }
95     }
96 }