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.elroconnects.internal.devices;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
16 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceStatus;
20 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
21 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
22 import org.openhab.core.library.types.DecimalType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.QuantityType;
25 import org.openhab.core.library.unit.Units;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingStatusDetail;
28 import org.openhab.core.types.UnDefType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * The {@link ElroConnectsDeviceTemperatureSensor} is representing an ELRO Connects temperature and humidity sensor
36 * @author Mark Herwege - Initial contribution
39 public class ElroConnectsDeviceTemperatureSensor extends ElroConnectsDevice {
41 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceTemperatureSensor.class);
43 public ElroConnectsDeviceTemperatureSensor(int deviceId, ElroConnectsBridgeHandler bridge) {
44 super(deviceId, bridge);
48 public void updateState() {
49 ElroConnectsDeviceHandler handler = getHandler();
50 if (handler == null) {
54 ElroDeviceStatus elroStatus = ElroDeviceStatus.NORMAL;
55 int signalStrength = 0;
59 String deviceStatus = this.deviceStatus;
60 if (deviceStatus.length() >= 8) {
61 signalStrength = Integer.parseInt(deviceStatus.substring(0, 2), 16);
62 signalStrength = (signalStrength > 4) ? 4 : ((signalStrength < 0) ? 0 : signalStrength);
63 batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
64 temperature = Byte.parseByte(deviceStatus.substring(4, 6), 16);
65 humidity = Integer.parseInt(deviceStatus.substring(6, 8));
67 elroStatus = ElroDeviceStatus.FAULT;
68 logger.debug("Could not decode device status: {}", deviceStatus);
73 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
74 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
75 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
76 handler.updateState(TEMPERATURE, UnDefType.UNDEF);
77 handler.updateState(HUMIDITY, UnDefType.UNDEF);
78 String msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
79 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
82 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
83 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
84 handler.updateState(LOW_BATTERY, (batteryLevel < 15) ? OnOffType.ON : OnOffType.OFF);
85 handler.updateState(TEMPERATURE, new QuantityType<>(temperature, CELSIUS));
86 handler.updateState(HUMIDITY, new QuantityType<>(humidity, Units.PERCENT));
87 handler.updateStatus(ThingStatus.ONLINE);
92 public void testAlarm() {
97 public void muteAlarm() {
102 public void switchState(boolean state) {