]> git.basschouten.com Git - openhab-addons.git/blob
34088f0b31d7982d02d9767f656d660c94468e98
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 batteryLevel = 0;
56         int temperature = 0;
57         int humidity = 0;
58         String deviceStatus = this.deviceStatus;
59         if (deviceStatus.length() >= 8) {
60             batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
61             temperature = Byte.parseByte(deviceStatus.substring(4, 6), 16);
62             humidity = Integer.parseInt(deviceStatus.substring(6, 8));
63         } else {
64             elroStatus = ElroDeviceStatus.FAULT;
65             logger.debug("Could not decode device status: {}", deviceStatus);
66         }
67
68         switch (elroStatus) {
69             case FAULT:
70                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
71                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
72                 handler.updateState(TEMPERATURE, UnDefType.UNDEF);
73                 handler.updateState(HUMIDITY, UnDefType.UNDEF);
74                 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
75                 break;
76             default:
77                 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
78                 handler.updateState(LOW_BATTERY, (batteryLevel < 15) ? OnOffType.ON : OnOffType.OFF);
79                 handler.updateState(TEMPERATURE, new QuantityType<>(temperature, CELSIUS));
80                 handler.updateState(HUMIDITY, new QuantityType<>(humidity, Units.PERCENT));
81                 handler.updateStatus(ThingStatus.ONLINE);
82         }
83     }
84
85     @Override
86     public void testAlarm() {
87         // nothing
88     }
89
90     @Override
91     public void muteAlarm() {
92         // nothing
93     }
94
95     @Override
96     public void switchState(boolean state) {
97         // nothing
98     }
99 }