]> git.basschouten.com Git - openhab-addons.git/blob
d7fae4458ed56b41e36785a38485c36e92a09b71
[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.VerisureUserPresencesDTO;
22 import org.openhab.binding.verisure.internal.dto.VerisureUserPresencesDTO.UserTracking;
23 import org.openhab.core.library.types.StringType;
24 import org.openhab.core.thing.Channel;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.thing.ThingStatus;
27 import org.openhab.core.thing.ThingTypeUID;
28 import org.openhab.core.types.State;
29 import org.openhab.core.types.UnDefType;
30
31 /**
32  * Handler for the User Presence Device thing type that Verisure provides.
33  *
34  * @author Jan Gustafsson - Initial contribution
35  *
36  */
37 @NonNullByDefault
38 public class VerisureUserPresenceThingHandler extends VerisureThingHandler<VerisureUserPresencesDTO> {
39
40     public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_USERPRESENCE);
41
42     public VerisureUserPresenceThingHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public Class<VerisureUserPresencesDTO> getVerisureThingClass() {
48         return VerisureUserPresencesDTO.class;
49     }
50
51     @Override
52     public synchronized void update(VerisureUserPresencesDTO thing) {
53         updateUserPresenceState(thing);
54         updateStatus(ThingStatus.ONLINE);
55     }
56
57     private void updateUserPresenceState(VerisureUserPresencesDTO userPresenceJSON) {
58         List<UserTracking> userTrackingList = userPresenceJSON.getData().getInstallation().getUserTrackings();
59         if (!userTrackingList.isEmpty()) {
60             UserTracking userTracking = userTrackingList.get(0);
61             getThing().getChannels().stream().map(Channel::getUID)
62                     .filter(channelUID -> isLinked(channelUID) && !"timestamp".equals(channelUID.getId()))
63                     .forEach(channelUID -> {
64                         State state = getValue(channelUID.getId(), userTracking);
65                         updateState(channelUID, state);
66                     });
67             updateTimeStamp(userTracking.getCurrentLocationTimestamp());
68             updateInstallationChannels(userPresenceJSON);
69         } else {
70             logger.debug("UserTrackingList is empty!");
71         }
72     }
73
74     public State getValue(String channelId, UserTracking userTracking) {
75         switch (channelId) {
76             case CHANNEL_USER_NAME:
77                 String name = userTracking.getName();
78                 return name != null ? new StringType(name) : UnDefType.NULL;
79             case CHANNEL_USER_LOCATION_STATUS:
80                 String currentLocation = userTracking.getCurrentLocationName();
81                 return currentLocation != null ? new StringType(currentLocation)
82                         : new StringType(userTracking.getCurrentLocationId());
83             case CHANNEL_STATUS:
84                 String status = userTracking.getStatus();
85                 return status != null ? new StringType(status) : UnDefType.NULL;
86             case CHANNEL_WEBACCOUNT:
87                 String webAccount = userTracking.getWebAccount();
88                 return webAccount != null ? new StringType(webAccount) : UnDefType.NULL;
89             case CHANNEL_USER_DEVICE_NAME:
90                 String deviceName = userTracking.getDeviceName();
91                 return deviceName != null ? new StringType(deviceName) : UnDefType.NULL;
92
93         }
94         return UnDefType.UNDEF;
95     }
96
97     @Override
98     public void updateTriggerChannel(String event) {
99     }
100 }