]> git.basschouten.com Git - openhab-addons.git/blob
9e3d81fb8521594e62b6e52c1a02b57275997a80
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.nuki.internal.configuration.NukiSmartLockConfiguration;
17 import org.openhab.binding.nuki.internal.constants.NukiBindingConstants;
18 import org.openhab.binding.nuki.internal.constants.SmartLockAction;
19 import org.openhab.binding.nuki.internal.dataexchange.BridgeLockActionResponse;
20 import org.openhab.binding.nuki.internal.dto.BridgeApiDeviceStateDto;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26
27 /**
28  * The {@link NukiSmartLockHandler} is responsible for handling commands, which are
29  * sent to one of the channels.
30  *
31  * @author Markus Katter - Initial contribution
32  * @contributer Christian Hoefler - Door sensor integration
33  * @contributer Jan Vybíral - Refactoring, added more channels
34  */
35 @NonNullByDefault
36 public class NukiSmartLockHandler extends AbstractNukiDeviceHandler<NukiSmartLockConfiguration> {
37
38     public NukiSmartLockHandler(Thing thing) {
39         super(thing);
40     }
41
42     @Override
43     public void initialize() {
44         super.initialize();
45     }
46
47     @Override
48     public void refreshState(BridgeApiDeviceStateDto state) {
49         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOCK,
50                 state.getState() == NukiBindingConstants.LOCK_STATES_LOCKED, OnOffType::from);
51         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_BATTERY_LEVEL, state.getBatteryChargeState(),
52                 DecimalType::new);
53         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_BATTERY_CHARGING, state.getBatteryCharging(),
54                 OnOffType::from);
55         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOW_BATTERY, state.isBatteryCritical(), OnOffType::from);
56         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_KEYPAD_LOW_BATTERY, state.getKeypadBatteryCritical(),
57                 OnOffType::from);
58         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_STATE, state.getState(), DecimalType::new);
59         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_DOOR_STATE, state.getDoorsensorState(), DecimalType::new);
60     }
61
62     @Override
63     protected int getDeviceType() {
64         return NukiBindingConstants.DEVICE_SMART_LOCK;
65     }
66
67     @Override
68     protected boolean doHandleCommand(ChannelUID channelUID, Command command) {
69         switch (channelUID.getId()) {
70             case NukiBindingConstants.CHANNEL_SMARTLOCK_LOCK:
71                 if (command instanceof OnOffType) {
72                     final SmartLockAction action;
73
74                     if (command == OnOffType.OFF) {
75                         action = configuration.unlatch ? SmartLockAction.UNLATCH : SmartLockAction.UNLOCK;
76                     } else {
77                         action = SmartLockAction.LOCK;
78                     }
79
80                     withHttpClient(client -> {
81                         BridgeLockActionResponse bridgeLockActionResponse = client
82                                 .getSmartLockAction(configuration.nukiId, action);
83                         handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
84                     });
85
86                     return true;
87                 }
88                 break;
89             case NukiBindingConstants.CHANNEL_SMARTLOCK_STATE:
90                 if (command instanceof DecimalType) {
91                     DecimalType cmd = (DecimalType) command;
92                     SmartLockAction action = SmartLockAction.fromAction(cmd.intValue());
93                     if (action != null) {
94                         withHttpClient(client -> {
95                             BridgeLockActionResponse bridgeLockActionResponse = client
96                                     .getSmartLockAction(configuration.nukiId, action);
97                             handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
98                         });
99                     }
100                     return true;
101                 }
102         }
103         return false;
104     }
105
106     @Override
107     protected Class<NukiSmartLockConfiguration> getConfigurationClass() {
108         return NukiSmartLockConfiguration.class;
109     }
110 }