]> git.basschouten.com Git - openhab-addons.git/blob
4c559bcebaf307601209c7ac5688b37ce2311670
[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.util.List;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.openhab.binding.netatmo.internal.api.dto.NAObject;
22 import org.openhab.binding.netatmo.internal.api.dto.WebhookEvent;
23 import org.openhab.binding.netatmo.internal.handler.CommonInterface;
24 import org.openhab.binding.netatmo.internal.handler.channelhelper.ChannelHelper;
25 import org.openhab.binding.netatmo.internal.providers.NetatmoDescriptionProvider;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.ThingUID;
28 import org.openhab.core.types.UnDefType;
29
30 /**
31  * {@link AlarmEventCapability} gives the ability to handle Alarm modules events
32  *
33  * @author GaĆ«l L'hopital - Initial contribution
34  *
35  */
36 @NonNullByDefault
37 public class AlarmEventCapability extends HomeSecurityThingCapability {
38
39     public AlarmEventCapability(CommonInterface handler, NetatmoDescriptionProvider descriptionProvider,
40             List<ChannelHelper> channelHelpers) {
41         super(handler, descriptionProvider, channelHelpers);
42     }
43
44     @Override
45     protected void updateWebhookEvent(WebhookEvent event) {
46         super.updateWebhookEvent(event);
47
48         final ThingUID thingUid = handler.getThing().getUID();
49         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_TYPE),
50                 toStringType(event.getEventType()));
51         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_TIME),
52                 toDateTimeType(event.getTime()));
53         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_SUBTYPE),
54                 event.getSubTypeDescription().map(d -> toStringType(d)).orElse(UnDefType.NULL));
55         final String message = event.getName();
56         handler.updateState(new ChannelUID(thingUid, GROUP_LAST_EVENT, CHANNEL_EVENT_MESSAGE),
57                 message == null || message.isBlank() ? UnDefType.NULL : toStringType(message));
58     }
59
60     @Override
61     public List<NAObject> updateReadings() {
62         return getSecurityCapability().map(cap -> cap.getDeviceLastEvent(handler.getId(), moduleType.apiName))
63                 .map(event -> List.of((NAObject) event)).orElse(List.of());
64     }
65 }