]> git.basschouten.com Git - openhab-addons.git/blob
c09554a4f25a0b8bd4bc8f985a2df22b33be2a08
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.netatmo.internal.handler.capability;
14
15 import static org.openhab.binding.netatmo.internal.NetatmoBindingConstants.*;
16 import static org.openhab.binding.netatmo.internal.utils.ChannelTypeUtils.*;
17
18 import java.time.ZonedDateTime;
19 import java.util.ArrayList;
20 import java.util.List;
21 import java.util.stream.Stream;
22
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.eclipse.jdt.annotation.Nullable;
25 import org.openhab.binding.netatmo.internal.api.data.EventType;
26 import org.openhab.binding.netatmo.internal.api.data.ModuleType;
27 import org.openhab.binding.netatmo.internal.api.dto.Event;
28 import org.openhab.binding.netatmo.internal.api.dto.HomeDataModule;
29 import org.openhab.binding.netatmo.internal.api.dto.HomeEvent;
30 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
31 import org.openhab.binding.netatmo.internal.api.dto.WebhookEvent;
32 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
33 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
34 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
35 import org.openhab.core.library.types.OnOffType;
36 import org.openhab.core.thing.ChannelUID;
37 import org.openhab.core.thing.ThingUID;
38 import org.openhab.core.types.Command;
39 import org.openhab.core.types.StateOption;
40 import org.openhab.core.types.UnDefType;
41
42 /**
43  * {@link PersonCapability} gives the ability to handle Person specifics
44  *
45  * @author GaĆ«l L'hopital - Initial contribution
46  *
47  */
48 @NonNullByDefault
49 public class PersonCapability extends HomeSecurityThingCapability {
50     private final ChannelUID cameraChannelUID;
51     private @Nullable ZonedDateTime lastEventTime;
52
53     public PersonCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider,
54             List<ChannelHelper> channelHelpers) {
55         super(handler, descriptionProvider, channelHelpers);
56         this.cameraChannelUID = new ChannelUID(thing.getUID(), GROUP_PERSON_LAST_EVENT, CHANNEL_EVENT_CAMERA_ID);
57     }
58
59     @Override
60     protected void beforeNewData() {
61         super.beforeNewData();
62         getSecurityCapability().ifPresent(cap -> {
63             Stream<HomeDataModule> cameras = cap.getModules().values().stream()
64                     .filter(module -> module.getType() == ModuleType.WELCOME);
65             descriptionProvider.setStateOptions(cameraChannelUID,
66                     cameras.map(p -> new StateOption(p.getId(), p.getName())).toList());
67         });
68     }
69
70     @Override
71     public void handleCommand(String channelName, Command command) {
72         if ((command instanceof OnOffType) && CHANNEL_PERSON_AT_HOME.equals(channelName)) {
73             getSecurityCapability().ifPresent(cap -> cap.setPersonAway(handler.getId(), OnOffType.OFF.equals(command)));
74         }
75     }
76
77     @Override
78     protected void updateWebhookEvent(WebhookEvent event) {
79         super.updateWebhookEvent(event);
80
81         ThingUID thingUid = thing.getUID();
82
83         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_SUBTYPE),
84                 event.getSubTypeDescription().map(d -> toStringType(d)).orElse(UnDefType.NULL));
85
86         final String message = event.getName();
87         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_MESSAGE),
88                 message == null || message.isBlank() ? UnDefType.NULL : toStringType(message));
89
90         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_TIME),
91                 toDateTimeType(event.getTime()));
92
93         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_SNAPSHOT),
94                 toRawType(event.getSnapshotUrl()));
95
96         handler.updateState(cameraChannelUID, toStringType(event.getCameraId()));
97     }
98
99     @Override
100     public void updateEvent(Event event) {
101         super.updateEvent(event);
102         EventType eventType = event.getEventType();
103         ZonedDateTime localLast = lastEventTime;
104         ZonedDateTime eventTime = event.getTime();
105         if ((localLast != null && !eventTime.isAfter(localLast)) || !eventType.validFor(ModuleType.PERSON)) {
106             return; // ignore incoming events if they are deprecated
107         }
108         lastEventTime = eventTime;
109         handler.triggerChannel(CHANNEL_HOME_EVENT,
110                 event.getSubTypeDescription().map(st -> st.name()).orElse(event.getEventType().name()));
111     }
112
113     @Override
114     public List<NAObject> updateReadings() {
115         List<NAObject> result = new ArrayList<>();
116         getSecurityCapability().ifPresent(cap -> {
117             HomeEvent event = cap.getLastPersonEvent(handler.getId());
118             if (event != null) {
119                 result.add(event);
120             }
121         });
122         return result;
123     }
124 }