2 * Copyright (c) 2010-2023 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 * @author Christian Hoefler - Door sensor integration
33 * @author Jan Vybíral - Refactoring, added more channels
36 public class NukiSmartLockHandler extends AbstractNukiDeviceHandler<NukiSmartLockConfiguration> {
38 public NukiSmartLockHandler(Thing thing, boolean readOnly) {
39 super(thing, readOnly);
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(),
48 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_BATTERY_CHARGING, state.getBatteryCharging(),
50 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_LOW_BATTERY, state.isBatteryCritical(), OnOffType::from);
51 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_KEYPAD_LOW_BATTERY, state.getKeypadBatteryCritical(),
53 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_STATE, state.getState(), DecimalType::new);
54 updateState(NukiBindingConstants.CHANNEL_SMARTLOCK_DOOR_STATE, state.getDoorsensorState(), DecimalType::new);
58 protected int getDeviceType() {
59 return this.configuration.deviceType;
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;
69 if (command == OnOffType.OFF) {
70 action = configuration.unlatch ? SmartLockAction.UNLATCH : SmartLockAction.UNLOCK;
72 action = SmartLockAction.LOCK;
75 withHttpClient(client -> {
76 BridgeLockActionResponse bridgeLockActionResponse = client
77 .getSmartLockAction(configuration.nukiId, action, getDeviceType());
78 handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
84 case NukiBindingConstants.CHANNEL_SMARTLOCK_STATE:
85 if (command instanceof DecimalType decimalCommand) {
86 SmartLockAction action = SmartLockAction.fromAction(decimalCommand.intValue());
88 withHttpClient(client -> {
89 BridgeLockActionResponse bridgeLockActionResponse = client
90 .getSmartLockAction(configuration.nukiId, action, getDeviceType());
91 handleResponse(bridgeLockActionResponse, channelUID.getAsString(), command.toString());
101 protected Class<NukiSmartLockConfiguration> getConfigurationClass() {
102 return NukiSmartLockConfiguration.class;