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.nest.internal.wwn.handler;
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;
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;
33 * The smoke detector handler, it handles the data from WWN for the smoke detector.
35 * @author David Bennett - Initial contribution
36 * @author Wouter Born - Handle channel refresh command
39 public class WWNSmokeDetectorHandler extends WWNBaseHandler<WWNSmokeDetector> {
40 private final Logger logger = LoggerFactory.getLogger(WWNSmokeDetectorHandler.class);
42 public WWNSmokeDetectorHandler(Thing thing) {
43 super(thing, WWNSmokeDetector.class);
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());
65 logger.error("Unsupported channelId '{}'", channelUID.getId());
66 return UnDefType.UNDEF;
71 * Handles any incoming command requests.
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));
84 protected void update(@Nullable WWNSmokeDetector oldSmokeDetector, WWNSmokeDetector smokeDetector) {
85 logger.debug("Updating {}", getThing().getUID());
87 updateLinkedChannels(oldSmokeDetector, smokeDetector);
88 updateProperty(PROPERTY_FIRMWARE_VERSION, smokeDetector.getSoftwareVersion());
90 ThingStatus newStatus = smokeDetector.isOnline() == null ? ThingStatus.UNKNOWN
91 : smokeDetector.isOnline() ? ThingStatus.ONLINE : ThingStatus.OFFLINE;
92 if (newStatus != thing.getStatus()) {
93 updateStatus(newStatus);