2 * Copyright (c) 2010-2022 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.*;
19 import java.util.stream.Collectors;
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;
34 * The {@link ElroConnectsDeviceMotionSensor} is representing an ELRO Connects Motion Sensor device.
36 * @author Mark Herwege - Initial contribution
39 public class ElroConnectsDeviceMotionSensor extends ElroConnectsDevice {
41 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceMotionSensor.class);
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";
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);
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));
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));
65 public void updateState() {
66 ElroConnectsDeviceHandler handler = getHandler();
67 if (handler == null) {
71 ElroDeviceStatus elroStatus = getStatus();
72 int signalStrength = 0;
74 String deviceStatus = this.deviceStatus;
75 if (deviceStatus.length() >= 6) {
76 signalStrength = Integer.parseInt(deviceStatus.substring(0, 2), 16);
77 signalStrength = (signalStrength > 4) ? 4 : ((signalStrength < 0) ? 0 : signalStrength);
78 batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
80 elroStatus = ElroDeviceStatus.FAULT;
81 logger.debug("Could not decode device status: {}", deviceStatus);
86 handler.updateState(MOTION, UnDefType.UNDEF);
87 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
88 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
89 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
90 String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
91 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
94 handler.updateState(MOTION, UnDefType.UNDEF);
95 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
96 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
97 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
98 msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
99 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
102 handler.updateState(MOTION,
103 ElroDeviceStatus.TRIGGERED.equals(elroStatus) ? OnOffType.ON : OnOffType.OFF);
104 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
105 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
106 handler.updateState(LOW_BATTERY, (batteryLevel < 15) ? OnOffType.ON : OnOffType.OFF);
107 handler.updateStatus(ThingStatus.ONLINE);
112 public void testAlarm() {
117 public void muteAlarm() {
122 public void switchState(boolean state) {