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.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();
74 String deviceStatus = this.deviceStatus;
75 if (deviceStatus.length() >= 6) {
76 batteryLevel = Integer.parseInt(deviceStatus.substring(2, 4), 16);
78 elroStatus = ElroDeviceStatus.FAULT;
79 logger.debug("Could not decode device status: {}", deviceStatus);
84 handler.updateState(ENTRY, UnDefType.UNDEF);
85 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
86 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
87 handler.updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
88 "Device " + deviceId + " is not syncing with K1 hub");
91 handler.updateState(ENTRY, UnDefType.UNDEF);
92 handler.updateState(BATTERY_LEVEL, UnDefType.UNDEF);
93 handler.updateState(LOW_BATTERY, UnDefType.UNDEF);
94 handler.updateStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE, "Device " + deviceId + " has a fault");
97 handler.updateState(ENTRY,
98 ElroDeviceStatus.OPEN.equals(elroStatus) ? OpenClosedType.OPEN : OpenClosedType.CLOSED);
99 handler.updateState(BATTERY_LEVEL, new DecimalType(batteryLevel));
100 handler.updateState(LOW_BATTERY, (batteryLevel < 15) ? OnOffType.ON : OnOffType.OFF);
101 handler.updateStatus(ThingStatus.ONLINE);
106 public void testAlarm() {
111 public void muteAlarm() {
116 public void switchState(boolean state) {