2 * Copyright (c) 2010-2020 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.nest.internal.handler;
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;
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;
32 * The smoke detector handler, it handles the data from Nest for the smoke detector.
34 * @author David Bennett - Initial contribution
35 * @author Wouter Born - Handle channel refresh command
38 public class NestSmokeDetectorHandler extends NestBaseHandler<SmokeDetector> {
39 private final Logger logger = LoggerFactory.getLogger(NestSmokeDetectorHandler.class);
41 public NestSmokeDetectorHandler(Thing thing) {
42 super(thing, SmokeDetector.class);
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());
64 logger.error("Unsupported channelId '{}'", channelUID.getId());
65 return UnDefType.UNDEF;
70 * Handles any incoming command requests.
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));
83 protected void update(SmokeDetector oldSmokeDetector, SmokeDetector smokeDetector) {
84 logger.debug("Updating {}", getThing().getUID());
86 updateLinkedChannels(oldSmokeDetector, smokeDetector);
87 updateProperty(PROPERTY_FIRMWARE_VERSION, smokeDetector.getSoftwareVersion());
89 ThingStatus newStatus = smokeDetector.isOnline() == null ? ThingStatus.UNKNOWN
90 : smokeDetector.isOnline() ? ThingStatus.ONLINE : ThingStatus.OFFLINE;
91 if (newStatus != thing.getStatus()) {
92 updateStatus(newStatus);