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.draytonwiser.internal.handler;
15 import static org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.*;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.BatteryLevel;
20 import org.openhab.binding.draytonwiser.internal.DraytonWiserBindingConstants.SignalStrength;
21 import org.openhab.binding.draytonwiser.internal.api.DraytonWiserApiException;
22 import org.openhab.binding.draytonwiser.internal.handler.RoomStatHandler.RoomStatData;
23 import org.openhab.binding.draytonwiser.internal.model.DeviceDTO;
24 import org.openhab.binding.draytonwiser.internal.model.DraytonWiserDTO;
25 import org.openhab.binding.draytonwiser.internal.model.RoomStatDTO;
26 import org.openhab.core.library.types.DecimalType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.QuantityType;
29 import org.openhab.core.library.types.StringType;
30 import org.openhab.core.library.unit.SIUnits;
31 import org.openhab.core.library.unit.Units;
32 import org.openhab.core.thing.Thing;
33 import org.openhab.core.types.Command;
34 import org.openhab.core.types.State;
35 import org.openhab.core.types.UnDefType;
38 * The {@link RoomStatHandler} is responsible for handling commands, which are sent to one of the channels.
40 * @author Andrew Schofield - Initial contribution
41 * @author Hilbrand Bouwkamp - Simplified handler to handle null data
44 public class RoomStatHandler extends DraytonWiserThingHandler<RoomStatData> {
46 private String serialNumber = "";
48 public RoomStatHandler(final Thing thing) {
53 public void initialize() {
55 serialNumber = getConfig().get("serialNumber").toString();
59 protected void handleCommand(final String channelId, final Command command) throws DraytonWiserApiException {
60 if (command instanceof OnOffType && CHANNEL_DEVICE_LOCKED.equals(channelId)) {
61 setDeviceLocked(OnOffType.ON.equals(command));
66 protected void refresh() {
67 updateState(CHANNEL_CURRENT_TEMPERATURE, this::getTemperature);
68 updateState(CHANNEL_CURRENT_HUMIDITY, this::getHumidity);
69 updateState(CHANNEL_CURRENT_SETPOINT, this::getSetPoint);
70 updateState(CHANNEL_CURRENT_SIGNAL_RSSI, this::getSignalRSSI);
71 updateState(CHANNEL_CURRENT_SIGNAL_LQI, this::getSignalLQI);
72 updateState(CHANNEL_CURRENT_BATTERY_VOLTAGE, this::getBatteryVoltage);
73 updateState(CHANNEL_CURRENT_WISER_SIGNAL_STRENGTH, this::getWiserSignalStrength);
74 updateState(CHANNEL_CURRENT_SIGNAL_STRENGTH, this::getSignalStrength);
75 updateState(CHANNEL_CURRENT_WISER_BATTERY_LEVEL, this::getWiserBatteryLevel);
76 updateState(CHANNEL_CURRENT_BATTERY_LEVEL, this::getBatteryLevel);
77 updateState(CHANNEL_ZIGBEE_CONNECTED, this::getZigbeeConnected);
78 updateState(CHANNEL_DEVICE_LOCKED, this::getDeviceLocked);
82 protected @Nullable RoomStatData collectData(final DraytonWiserDTO domainDTOProxy) {
83 final RoomStatDTO roomStat = domainDTOProxy.getRoomStat(serialNumber);
84 final DeviceDTO device = roomStat == null ? null : domainDTOProxy.getExtendedDeviceProperties(roomStat.getId());
86 return roomStat == null || device == null ? null : new RoomStatData(roomStat, device);
89 private State getSetPoint() {
90 return new QuantityType<>(getData().roomStat.getSetPoint() / 10.0, SIUnits.CELSIUS);
93 private State getHumidity() {
94 final Integer humidity = getData().roomStat.getMeasuredHumidity();
96 return humidity == null ? UnDefType.UNDEF : new QuantityType<>(humidity, Units.PERCENT);
99 private State getTemperature() {
100 final int fullScaleTemp = getData().roomStat.getMeasuredTemperature();
102 return OFFLINE_TEMPERATURE == fullScaleTemp ? UnDefType.UNDEF
103 : new QuantityType<>(fullScaleTemp / 10.0, SIUnits.CELSIUS);
106 private State getSignalRSSI() {
107 final Integer rssi = getData().device.getRssi();
108 return rssi == null ? UnDefType.UNDEF : new QuantityType<>(rssi, Units.DECIBEL_MILLIWATTS);
111 private State getSignalLQI() {
112 final Integer lqi = getData().device.getLqi();
113 return lqi == null ? UnDefType.UNDEF : new DecimalType(lqi);
116 private State getWiserSignalStrength() {
117 return new StringType(getData().device.getDisplayedSignalStrength());
120 private State getSignalStrength() {
121 return SignalStrength.toSignalStrength(getData().device.getDisplayedSignalStrength());
124 private State getBatteryVoltage() {
125 final Integer voltage = getData().device.getBatteryVoltage();
126 return voltage == null ? UnDefType.UNDEF : new QuantityType<>(voltage / 10.0, Units.VOLT);
129 private State getWiserBatteryLevel() {
130 return new StringType(getData().device.getBatteryLevel());
133 private State getBatteryLevel() {
134 return BatteryLevel.toBatteryLevel(getData().device.getBatteryLevel());
137 private State getZigbeeConnected() {
138 return OnOffType.from(OFFLINE_TEMPERATURE != getData().roomStat.getMeasuredTemperature());
141 private State getDeviceLocked() {
142 return getData().device.getDeviceLockEnabled() == null ? UnDefType.UNDEF
143 : OnOffType.from(getData().device.getDeviceLockEnabled());
146 private void setDeviceLocked(final boolean state) throws DraytonWiserApiException {
147 getApi().setDeviceLocked(getData().device.getId(), state);
150 static class RoomStatData {
151 public final RoomStatDTO roomStat;
152 public final DeviceDTO device;
154 public RoomStatData(final RoomStatDTO roomStat, final DeviceDTO device) {
155 this.roomStat = roomStat;
156 this.device = device;