2 * Copyright (c) 2010-2022 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.nuki.internal.handler;
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;
28 * The {@link NukiSmartLockHandler} is responsible for handling commands, which are
29 * sent to one of the channels.
31 * @author Markus Katter - Initial contribution
32 * @contributer Christian Hoefler - Door sensor integration
33 * @contributer Jan Vybíral - Refactoring, added more channels
36 public class NukiSmartLockHandler extends AbstractNukiDeviceHandler<NukiSmartLockConfiguration> {
38 public NukiSmartLockHandler(Thing thing) {
43 public void initialize() {
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(),
53 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_BATTERY_CHARGING, state.getBatteryCharging(),
55 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOW_BATTERY, state.isBatteryCritical(), OnOffType::from);
56 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_KEYPAD_LOW_BATTERY, state.getKeypadBatteryCritical(),
58 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_STATE, state.getState(), DecimalType::new);
59 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_DOOR_STATE, state.getDoorsensorState(), DecimalType::new);
63 protected int getDeviceType() {
64 return this.configuration.deviceType;
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;
74 if (command == OnOffType.OFF) {
75 action = configuration.unlatch ? SmartLockAction.UNLATCH : SmartLockAction.UNLOCK;
77 action = SmartLockAction.LOCK;
80 withHttpClient(client -> {
81 BridgeLockActionResponse bridgeLockActionResponse = client
82 .getSmartLockAction(configuration.nukiId, action, getDeviceType());
83 handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
89 case NukiBindingConstants.CHANNEL_SMARTLOCK_STATE:
90 if (command instanceof DecimalType) {
91 DecimalType cmd = (DecimalType) command;
92 SmartLockAction action = SmartLockAction.fromAction(cmd.intValue());
94 withHttpClient(client -> {
95 BridgeLockActionResponse bridgeLockActionResponse = client
96 .getSmartLockAction(configuration.nukiId, action, getDeviceType());
97 handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
107 protected Class<NukiSmartLockConfiguration> getConfigurationClass() {
108 return NukiSmartLockConfiguration.class;