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