]> git.basschouten.com Git - openhab-addons.git/blob
9a7571f3ad40aa129e50e682984496ee500614a7
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.verisure.internal.handler;
14
15 import static org.openhab.binding.verisure.internal.VerisureBindingConstants.*;
16
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Set;
20
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;
34
35 /**
36  * Handler for the Smart Lock Device thing type that Verisure provides.
37  *
38  * @author Jan Gustafsson - Initial contribution
39  *
40  */
41 @NonNullByDefault
42 public class VerisureDoorWindowThingHandler extends VerisureThingHandler<VerisureDoorWindowsDTO> {
43
44     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_DOORWINDOW);
45
46     public VerisureDoorWindowThingHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public Class<VerisureDoorWindowsDTO> getVerisureThingClass() {
52         return VerisureDoorWindowsDTO.class;
53     }
54
55     @Override
56     public synchronized void update(VerisureDoorWindowsDTO thing) {
57         updateDoorWindowState(thing);
58         updateStatus(ThingStatus.ONLINE);
59     }
60
61     private void updateDoorWindowState(VerisureDoorWindowsDTO doorWindowJSON) {
62         List<DoorWindow> doorWindowList = doorWindowJSON.getData().getInstallation().getDoorWindows();
63         if (!doorWindowList.isEmpty()) {
64             DoorWindow doorWindow = doorWindowList.get(0);
65
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);
71
72                     });
73             updateTimeStamp(doorWindow.getReportTime());
74             updateInstallationChannels(doorWindowJSON);
75         } else {
76             logger.debug("DoorWindow list is empty!");
77         }
78     }
79
80     public State getValue(String channelId, DoorWindow doorWindow, VerisureDoorWindowsDTO doorWindowJSON) {
81         switch (channelId) {
82             case CHANNEL_STATE:
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);
93                     }
94                 }
95                 return OnOffType.from(false);
96         }
97         return UnDefType.UNDEF;
98     }
99
100     @Override
101     public void updateTriggerChannel(String event) {
102         logger.debug("DoorWindowThingHandler trigger event {}", event);
103         triggerChannel(CHANNEL_DOOR_WINDOW_TRIGGER_CHANNEL, event);
104     }
105 }