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.Collections;
18 import java.util.List;
21 import org.eclipse.jdt.annotation.NonNullByDefault;
22 import org.openhab.binding.verisure.internal.dto.VerisureUserPresencesDTO;
23 import org.openhab.binding.verisure.internal.dto.VerisureUserPresencesDTO.UserTracking;
24 import org.openhab.core.library.types.StringType;
25 import org.openhab.core.thing.Channel;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingTypeUID;
29 import org.openhab.core.types.State;
30 import org.openhab.core.types.UnDefType;
33 * Handler for the User Presence Device thing type that Verisure provides.
35 * @author Jan Gustafsson - Initial contribution
39 public class VerisureUserPresenceThingHandler extends VerisureThingHandler<VerisureUserPresencesDTO> {
41 public static final Set<ThingTypeUID> SUPPORTED_THING_TYPES = Collections.singleton(THING_TYPE_USERPRESENCE);
43 public VerisureUserPresenceThingHandler(Thing thing) {
48 public Class<VerisureUserPresencesDTO> getVerisureThingClass() {
49 return VerisureUserPresencesDTO.class;
53 public synchronized void update(VerisureUserPresencesDTO thing) {
54 updateUserPresenceState(thing);
55 updateStatus(ThingStatus.ONLINE);
58 private void updateUserPresenceState(VerisureUserPresencesDTO userPresenceJSON) {
59 List<UserTracking> userTrackingList = userPresenceJSON.getData().getInstallation().getUserTrackings();
60 if (!userTrackingList.isEmpty()) {
61 UserTracking userTracking = userTrackingList.get(0);
62 getThing().getChannels().stream().map(Channel::getUID)
63 .filter(channelUID -> isLinked(channelUID) && !channelUID.getId().equals("timestamp"))
64 .forEach(channelUID -> {
65 State state = getValue(channelUID.getId(), userTracking);
66 updateState(channelUID, state);
68 updateTimeStamp(userTracking.getCurrentLocationTimestamp());
69 updateInstallationChannels(userPresenceJSON);
71 logger.debug("UserTrackingList is empty!");
75 public State getValue(String channelId, UserTracking userTracking) {
77 case CHANNEL_USER_NAME:
78 String name = userTracking.getName();
79 return name != null ? new StringType(name) : UnDefType.NULL;
80 case CHANNEL_USER_LOCATION_STATUS:
81 String currentLocation = userTracking.getCurrentLocationName();
82 return currentLocation != null ? new StringType(currentLocation)
83 : new StringType(userTracking.getCurrentLocationId());
85 String status = userTracking.getStatus();
86 return status != null ? new StringType(status) : UnDefType.NULL;
87 case CHANNEL_WEBACCOUNT:
88 String webAccount = userTracking.getWebAccount();
89 return webAccount != null ? new StringType(webAccount) : UnDefType.NULL;
90 case CHANNEL_USER_DEVICE_NAME:
91 String deviceName = userTracking.getDeviceName();
92 return deviceName != null ? new StringType(deviceName) : UnDefType.NULL;
95 return UnDefType.UNDEF;
99 public void updateTriggerChannel(String event) {