]> git.basschouten.com Git - openhab-addons.git/blob
effc3338de29afdeed24c6919d6c3bfa03bd8590
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 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, boolean readOnly) {
39         super(thing, readOnly);
40     }
41
42     @Override
43     public void refreshState(BridgeApiDeviceStateDto state) {
44         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOCK,
45                 state.getState() == NukiBindingConstants.LOCK_STATES_LOCKED, OnOffType::from);
46         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_BATTERY_LEVEL, state.getBatteryChargeState(),
47                 DecimalType::new);
48         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_BATTERY_CHARGING, state.getBatteryCharging(),
49                 OnOffType::from);
50         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOW_BATTERY, state.isBatteryCritical(), OnOffType::from);
51         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_KEYPAD_LOW_BATTERY, state.getKeypadBatteryCritical(),
52                 OnOffType::from);
53         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_STATE, state.getState(), DecimalType::new);
54         updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_DOOR_STATE, state.getDoorsensorState(), DecimalType::new);
55     }
56
57     @Override
58     protected int getDeviceType() {
59         return this.configuration.deviceType;
60     }
61
62     @Override
63     protected boolean doHandleCommand(ChannelUID channelUID, Command command) {
64         switch (channelUID.getId()) {
65             case NukiBindingConstants.CHANNEL_SMARTLOCK_LOCK:
66                 if (command instanceof OnOffType) {
67                     final SmartLockAction action;
68
69                     if (command == OnOffType.OFF) {
70                         action = configuration.unlatch ? SmartLockAction.UNLATCH : SmartLockAction.UNLOCK;
71                     } else {
72                         action = SmartLockAction.LOCK;
73                     }
74
75                     withHttpClient(client -> {
76                         BridgeLockActionResponse bridgeLockActionResponse = client
77                                 .getSmartLockAction(configuration.nukiId, action, getDeviceType());
78                         handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
79                     });
80
81                     return true;
82                 }
83                 break;
84             case NukiBindingConstants.CHANNEL_SMARTLOCK_STATE:
85                 if (command instanceof DecimalType decimalCommand) {
86                     SmartLockAction action = SmartLockAction.fromAction(decimalCommand.intValue());
87                     if (action != null) {
88                         withHttpClient(client -> {
89                             BridgeLockActionResponse bridgeLockActionResponse = client
90                                     .getSmartLockAction(configuration.nukiId, action, getDeviceType());
91                             handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
92                         });
93                     }
94                     return true;
95                 }
96         }
97         return false;
98     }
99
100     @Override
101     protected Class<NukiSmartLockConfiguration> getConfigurationClass() {
102         return NukiSmartLockConfiguration.class;
103     }
104 }