]> git.basschouten.com Git - openhab-addons.git/blob
075ec780ed1a456a263781da5d61234b826a6253
[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.List;
18 import java.util.Set;
19
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;
33
34 /**
35  * Handler for the Smart Lock Device thing type that Verisure provides.
36  *
37  * @author Jan Gustafsson - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 public class VerisureDoorWindowThingHandler extends VerisureThingHandler<VerisureDoorWindowsDTO> {
42
43     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_DOORWINDOW);
44
45     public VerisureDoorWindowThingHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public Class<VerisureDoorWindowsDTO> getVerisureThingClass() {
51         return VerisureDoorWindowsDTO.class;
52     }
53
54     @Override
55     public synchronized void update(VerisureDoorWindowsDTO thing) {
56         updateDoorWindowState(thing);
57         updateStatus(ThingStatus.ONLINE);
58     }
59
60     private void updateDoorWindowState(VerisureDoorWindowsDTO doorWindowJSON) {
61         List<DoorWindow> doorWindowList = doorWindowJSON.getData().getInstallation().getDoorWindows();
62         if (!doorWindowList.isEmpty()) {
63             DoorWindow doorWindow = doorWindowList.get(0);
64
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);
70
71                     });
72             updateTimeStamp(doorWindow.getReportTime());
73             updateInstallationChannels(doorWindowJSON);
74         } else {
75             logger.debug("DoorWindow list is empty!");
76         }
77     }
78
79     public State getValue(String channelId, DoorWindow doorWindow, VerisureDoorWindowsDTO doorWindowJSON) {
80         switch (channelId) {
81             case CHANNEL_STATE:
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);
92                     }
93                 }
94                 return OnOffType.from(false);
95         }
96         return UnDefType.UNDEF;
97     }
98
99     @Override
100     public void updateTriggerChannel(String event) {
101         logger.debug("DoorWindowThingHandler trigger event {}", event);
102         triggerChannel(CHANNEL_DOOR_WINDOW_TRIGGER_CHANNEL, event);
103     }
104 }