2 * Copyright (c) 2010-2020 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.VerisureDoorWindowsDTO;
23 import org.openhab.binding.verisure.internal.dto.VerisureDoorWindowsDTO.DoorWindow;
24 import org.openhab.core.library.types.OpenClosedType;
25 import org.openhab.core.library.types.StringType;
26 import org.openhab.core.thing.Channel;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingTypeUID;
30 import org.openhab.core.types.State;
31 import org.openhab.core.types.UnDefType;
34 * Handler for the Smart Lock Device thing type that Verisure provides.
36 * @author Jan Gustafsson - Initial contribution
40 public class VerisureDoorWindowThingHandler extends VerisureThingHandler<VerisureDoorWindowsDTO> {
42 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DOORWINDOW);
44 public VerisureDoorWindowThingHandler(Thing thing) {
49 public Class<VerisureDoorWindowsDTO> getVerisureThingClass() {
50 return VerisureDoorWindowsDTO.class;
54 public synchronized void update(VerisureDoorWindowsDTO thing) {
55 updateDoorWindowState(thing);
56 updateStatus(ThingStatus.ONLINE);
59 private void updateDoorWindowState(VerisureDoorWindowsDTO doorWindowJSON) {
60 List<DoorWindow> doorWindowList = doorWindowJSON.getData().getInstallation().getDoorWindows();
61 if (!doorWindowList.isEmpty()) {
62 DoorWindow doorWindow = doorWindowList.get(0);
64 getThing().getChannels().stream().map(Channel::getUID)
65 .filter(channelUID -> isLinked(channelUID) && !channelUID.getId().equals("timestamp"))
66 .forEach(channelUID -> {
67 State state = getValue(channelUID.getId(), doorWindow);
68 updateState(channelUID, state);
71 updateTimeStamp(doorWindow.getReportTime());
72 updateInstallationChannels(doorWindowJSON);
74 logger.debug("DoorWindow list is empty!");
78 public State getValue(String channelId, DoorWindow doorWindow) {
81 return "OPEN".equals(doorWindow.getState()) ? OpenClosedType.OPEN : OpenClosedType.CLOSED;
82 case CHANNEL_LOCATION:
83 String location = doorWindow.getDevice().getArea();
84 return location != null ? new StringType(location) : UnDefType.UNDEF;
86 return UnDefType.UNDEF;
90 public void updateTriggerChannel(String event) {
91 logger.debug("DoorWindowThingHandler trigger event {}", event);
92 triggerChannel(CHANNEL_DOOR_WINDOW_TRIGGER_CHANNEL, event);