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.*;
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.library.types.OpenClosedType;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.types.UnDefType;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link ElroConnectsDeviceEntrySensor} is representing a generic ELRO Connects Entry Sensor device.
37 * @author Mark Herwege - Initial contribution
40 public class ElroConnectsDeviceEntrySensor extends ElroConnectsDevice {
42 private final Logger logger = LoggerFactory.getLogger(ElroConnectsDeviceEntrySensor.class);
45 private static final String STAT_OPEN = "55";
46 private static final String STAT_STILL_OPEN = "66";
47 private static final String STAT_FAULT = "11";
48 private static final String STAT_NORMAL = "AA";
50 private static final Set<String> T_OPEN = Set.of(STAT_OPEN, STAT_STILL_OPEN);
51 private static final Set<String> T_FAULT = Set.of(STAT_FAULT);
52 private static final Set<String> T_NORMAL = Set.of(STAT_NORMAL);
54 private static final Map<ElroDeviceStatus, Set<String>> DEVICE_STATUS_MAP = Map.ofEntries(
55 Map.entry(ElroDeviceStatus.NORMAL, T_NORMAL), Map.entry(ElroDeviceStatus.OPEN, T_OPEN),
56 Map.entry(ElroDeviceStatus.FAULT, T_FAULT));
58 public ElroConnectsDeviceEntrySensor(int deviceId, ElroConnectsBridgeHandler bridge) {
59 super(deviceId, bridge);
60 statusMap = DEVICE_STATUS_MAP.entrySet().stream()
61 .flatMap(e -> e.getValue().stream().map(v -> Map.entry(v, e.getKey())))
62 .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));
66 public void updateState() {
67 ElroConnectsDeviceHandler handler = getHandler();
68 if (handler == null) {
72 ElroDeviceStatus elroStatus = getStatus();
73 int signalStrength = 0;
75 String deviceStatus = this.deviceStatus;
76 if (deviceStatus.length() >= 6) {
77 signalStrength = Integer.parseInt(deviceStatus.substring(0, 2), 16);
78 signalStrength = (signalStrength > 4) ? 4 : ((signalStrength < 0) ? 0 : signalStrength);
79 batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
81 elroStatus = ElroDeviceStatus.FAULT;
82 logger.debug("Could not decode device status: {}", deviceStatus);
87 handler.updateState(ENTRY, UnDefType.UNDEF);
88 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
89 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
90 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
91 String msg = String.format("@text/offline.device-not-syncing [ \"%d\" ]", deviceId);
92 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, msg);
95 handler.updateState(ENTRY, UnDefType.UNDEF);
96 handler.updateState(SIGNAL_STRENGTH, UnDefType.UNDEF);
97 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
98 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
99 msg = String.format("@text/offline.device-fault [ \"%d\" ]", deviceId);
100 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, msg);
103 handler.updateState(ENTRY,
104 ElroDeviceStatus.OPEN.equals(elroStatus) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
105 handler.updateState(SIGNAL_STRENGTH, new DecimalType(signalStrength));
106 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
107 handler.updateState(LOW_BATTERY, OnOffType.from(batteryLevel < 15));
108 handler.updateStatus(ThingStatus.ONLINE);
113 public void testAlarm() {
118 public void muteAlarm() {
123 public void switchState(boolean state) {