]> git.basschouten.com Git - openhab-addons.git/blob
b3f4cdc931af6adfb6a2e5853564b46a2d36ca46
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2021 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.nuki.internal.handler;
14
15 import java.time.Duration;
16 import java.time.Instant;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.nuki.internal.configuration.NukiDeviceConfiguration;
20 import org.openhab.binding.nuki.internal.constants.NukiBindingConstants;
21 import org.openhab.binding.nuki.internal.constants.OpenerAction;
22 import org.openhab.binding.nuki.internal.dataexchange.BridgeLockActionResponse;
23 import org.openhab.binding.nuki.internal.dto.BridgeApiDeviceStateDto;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
29
30 /**
31  * Thing handler for Nuki Opener
32  *
33  * @author Jan Vybíral - Initial contribution
34  */
35 @NonNullByDefault
36 public class NukiOpenerHandler extends AbstractNukiDeviceHandler<NukiDeviceConfiguration> {
37
38     public NukiOpenerHandler(Thing thing) {
39         super(thing);
40     }
41
42     private volatile Instant lastRingAction = Instant.EPOCH;
43
44     @Override
45     public void refreshState(BridgeApiDeviceStateDto state) {
46         updateState(NukiBindingConstants.CHANNEL_OPENER_LOW_BATTERY, state.isBatteryCritical(), OnOffType::from);
47         updateState(NukiBindingConstants.CHANNEL_OPENER_STATE, state.getState(), DecimalType::new);
48         updateState(NukiBindingConstants.CHANNEL_OPENER_MODE, state.getMode(), DecimalType::new);
49         updateState(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_TIMESTAMP, state.getRingactionTimestamp(),
50                 this::toDateTime);
51
52         if (state.getRingactionState() && Duration.between(lastRingAction, Instant.now()).getSeconds() > 30) {
53             triggerChannel(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_STATE, NukiBindingConstants.EVENT_RINGING);
54             lastRingAction = Instant.now();
55         }
56     }
57
58     @Override
59     protected int getDeviceType() {
60         return NukiBindingConstants.DEVICE_OPENER;
61     }
62
63     @Override
64     protected boolean doHandleCommand(ChannelUID channelUID, Command command) {
65         switch (channelUID.getId()) {
66             case NukiBindingConstants.CHANNEL_OPENER_STATE:
67                 if (command instanceof DecimalType) {
68                     OpenerAction action = OpenerAction.fromAction(((DecimalType) command).intValue());
69                     if (action != null) {
70                         return withHttpClient(client -> {
71                             BridgeLockActionResponse response = client.getOpenerAction(configuration.nukiId, action);
72                             return handleResponse(response, channelUID.getAsString(), command.toString());
73                         }, false);
74                     }
75                 }
76                 break;
77         }
78         return false;
79     }
80
81     @Override
82     protected Class<NukiDeviceConfiguration> getConfigurationClass() {
83         return NukiDeviceConfiguration.class;
84     }
85 }