]> git.basschouten.com Git - openhab-addons.git/blob
5e9524967f350712580fc532e67d63b7f42c9c85
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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.elroconnects.internal.devices;
14
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
16 import static org.openhab.core.library.unit.SIUnits.CELSIUS;
17
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;
31
32 /**
33  * The {@link ElroConnectsDeviceTemperatureSensor} is representing an ELRO Connects temperature and humidity sensor
34  * device.
35  *
36  * @author Mark Herwege - Initial contribution
37  */
38 @NonNullByDefault
39 public class ElroConnectsDeviceTemperatureSensor extends ElroConnectsDevice {
40
41     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceTemperatureSensor.class);
42
43     public ElroConnectsDeviceTemperatureSensor(int deviceId, ElroConnectsBridgeHandler bridge) {
44         super(deviceId, bridge);
45     }
46
47     @Override
48     public void updateState() {
49         ElroConnectsDeviceHandler handler = getHandler();
50         if (handler == null) {
51             return;
52         }
53
54         ElroDeviceStatus elroStatus = ElroDeviceStatus.NORMAL;
55         int signalStrength = 0;
56         int batteryLevel = 0;
57         int temperature = 0;
58         int humidity = 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));
66         } else {
67             elroStatus = ElroDeviceStatus.FAULT;
68             logger.debug("Could not decode device status: {}", deviceStatus);
69         }
70
71         switch (elroStatus) {
72             case FAULT:
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);
80                 break;
81             default:
82                 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
83                 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
84                 handler.updateState(LOW_BATTERY, OnOffType.from(batteryLevel < 15));
85                 handler.updateState(TEMPERATURE, new QuantityType<>(temperature, CELSIUS));
86                 handler.updateState(HUMIDITY, new QuantityType<>(humidity, Units.PERCENT));
87                 handler.updateStatus(ThingStatus.ONLINE);
88         }
89     }
90
91     @Override
92     public void testAlarm() {
93         // nothing
94     }
95
96     @Override
97     public void muteAlarm() {
98         // nothing
99     }
100
101     @Override
102     public void switchState(boolean state) {
103         // nothing
104     }
105 }