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.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;
32 * Handler for the User Presence Device thing type that Verisure provides.
34 * @author Jan Gustafsson - Initial contribution
38 public class VerisureUserPresenceThingHandler extends VerisureThingHandler<VerisureUserPresencesDTO> {
40 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Set.of(THING_TYPE_USERPRESENCE);
42 public VerisureUserPresenceThingHandler(Thing thing) {
47 public Class<VerisureUserPresencesDTO> getVerisureThingClass() {
48 return VerisureUserPresencesDTO.class;
52 public synchronized void update(VerisureUserPresencesDTO thing) {
53 updateUserPresenceState(thing);
54 updateStatus(ThingStatus.ONLINE);
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);
67 updateTimeStamp(userTracking.getCurrentLocationTimestamp());
68 updateInstallationChannels(userPresenceJSON);
70 logger.debug("UserTrackingList is empty!");
74 public State getValue(String channelId, UserTracking userTracking) {
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());
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;
94 return UnDefType.UNDEF;
98 public void updateTriggerChannel(String event) {