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.verisure.internal.handler;
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
17 import java.util.Collections;
18 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.verisure.internal.dto.VerisureBatteryStatusDTO;
23 import org.openhab.binding.verisure.internal.dto.VerisureDoorWindowsDTO;
24 import org.openhab.binding.verisure.internal.dto.VerisureDoorWindowsDTO.DoorWindow;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.OpenClosedType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.thing.Channel;
29 import org.openhab.core.thing.Thing;
30 import org.openhab.core.thing.ThingStatus;
31 import org.openhab.core.thing.ThingTypeUID;
32 import org.openhab.core.types.State;
33 import org.openhab.core.types.UnDefType;
36 * Handler for the Smart Lock Device thing type that Verisure provides.
38 * @author Jan Gustafsson - Initial contribution
42 public class VerisureDoorWindowThingHandler extends VerisureThingHandler<VerisureDoorWindowsDTO> {
44 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DOORWINDOW);
46 public VerisureDoorWindowThingHandler(Thing thing) {
51 public Class<VerisureDoorWindowsDTO> getVerisureThingClass() {
52 return VerisureDoorWindowsDTO.class;
56 public synchronized void update(VerisureDoorWindowsDTO thing) {
57 updateDoorWindowState(thing);
58 updateStatus(ThingStatus.ONLINE);
61 private void updateDoorWindowState(VerisureDoorWindowsDTO doorWindowJSON) {
62 List<DoorWindow> doorWindowList = doorWindowJSON.getData().getInstallation().getDoorWindows();
63 if (!doorWindowList.isEmpty()) {
64 DoorWindow doorWindow = doorWindowList.get(0);
66 getThing().getChannels().stream().map(Channel::getUID)
67 .filter(channelUID -> isLinked(channelUID) && !channelUID.getId().equals("timestamp"))
68 .forEach(channelUID -> {
69 State state = getValue(channelUID.getId(), doorWindow, doorWindowJSON);
70 updateState(channelUID, state);
73 updateTimeStamp(doorWindow.getReportTime());
74 updateInstallationChannels(doorWindowJSON);
76 logger.debug("DoorWindow list is empty!");
80 public State getValue(String channelId, DoorWindow doorWindow, VerisureDoorWindowsDTO doorWindowJSON) {
83 return "OPEN".equals(doorWindow.getState()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
84 case CHANNEL_LOCATION:
85 String location = doorWindow.getDevice().getArea();
86 return location != null ? new StringType(location) : UnDefType.UNDEF;
87 case CHANNEL_BATTERY_STATUS:
88 VerisureBatteryStatusDTO batteryStatus = doorWindowJSON.getBatteryStatus();
89 if (batteryStatus != null) {
90 String status = batteryStatus.getStatus();
91 if ("CRITICAL".equals(status)) {
92 return OnOffType.from(true);
95 return OnOffType.from(false);
97 return UnDefType.UNDEF;
101 public void updateTriggerChannel(String event) {
102 logger.debug("DoorWindowThingHandler trigger event {}", event);
103 triggerChannel(CHANNEL_DOOR_WINDOW_TRIGGER_CHANNEL, event);