]> git.basschouten.com Git - openhab-addons.git/blob
6efcc016eb64692cdc4b0ba1353197f1d6ac869f
[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
17 import java.util.Map;
18 import java.util.Set;
19 import java.util.stream.Collectors;
20
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceStatus;
23 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsBridgeHandler;
24 import org.openhab.binding.elroconnects.internal.handler.ElroConnectsDeviceHandler;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.UnDefType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * The {@link ElroConnectsDeviceMotionSensor} is representing an ELRO Connects Motion Sensor device.
35  *
36  * @author Mark Herwege - Initial contribution
37  */
38 @NonNullByDefault
39 public class ElroConnectsDeviceMotionSensor extends ElroConnectsDevice {
40
41     private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceMotionSensor.class);
42
43     // device states
44     private static final String STAT_TRIGGERED = "55";
45     private static final String STAT_TEARED = "A0";
46     private static final String STAT_FAULT = "11";
47     private static final String STAT_NORMAL = "AA";
48
49     private static final Set<String> T_TRIGGERED = Set.of(STAT_TEARED, STAT_TRIGGERED);
50     private static final Set<String> T_FAULT = Set.of(STAT_FAULT);
51     private static final Set<String> T_NORMAL = Set.of(STAT_NORMAL);
52
53     private static final Map<ElroDeviceStatus, Set<String>> DEVICE_STATUS_MAP = Map.ofEntries(
54             Map.entry(ElroDeviceStatus.NORMAL, T_NORMAL), Map.entry(ElroDeviceStatus.TRIGGERED, T_TRIGGERED),
55             Map.entry(ElroDeviceStatus.FAULT, T_FAULT));
56
57     public ElroConnectsDeviceMotionSensor(int deviceId, ElroConnectsBridgeHandler bridge) {
58         super(deviceId, bridge);
59         statusMap = DEVICE_STATUS_MAP.entrySet().stream()
60                 .flatMap(e -> e.getValue().stream().map(v -> Map.entry(v, e.getKey())))
61                 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
62     }
63
64     @Override
65     public void updateState() {
66         ElroConnectsDeviceHandler handler = getHandler();
67         if (handler == null) {
68             return;
69         }
70
71         ElroDeviceStatus elroStatus = getStatus();
72         int batteryLevel = 0;
73         String deviceStatus = this.deviceStatus;
74         if (deviceStatus.length() >= 6) {
75             batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
76         } else {
77             elroStatus = ElroDeviceStatus.FAULT;
78             logger.debug("Could not decode device status: {}", deviceStatus);
79         }
80
81         switch (elroStatus) {
82             case UNDEF:
83                 handler.updateState(MOTION, UnDefType.UNDEF);
84                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
85                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
86                 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
87                         "Device " + deviceId + " is not syncing with K1 hub");
88                 break;
89             case FAULT:
90                 handler.updateState(MOTION, UnDefType.UNDEF);
91                 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
92                 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
93                 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
94                 break;
95             default:
96                 handler.updateState(MOTION,
97                         ElroDeviceStatus.TRIGGERED.equals(elroStatus) ? OnOffType.ON : OnOffType.OFF);
98                 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
99                 handler.updateState(LOW_BATTERY, (batteryLevel < 15) ? OnOffType.ON : OnOffType.OFF);
100                 handler.updateStatus(ThingStatus.ONLINE);
101         }
102     }
103
104     @Override
105     public void testAlarm() {
106         // nothing
107     }
108
109     @Override
110     public void muteAlarm() {
111         // nothing
112     }
113
114     @Override
115     public void switchState(boolean state) {
116         // nothing
117     }
118 }