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