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 java.time.Duration;
16 import java.time.Instant;
17 import java.util.Objects;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.nuki.internal.configuration.NukiDeviceConfiguration;
21 import org.openhab.binding.nuki.internal.constants.NukiBindingConstants;
22 import org.openhab.binding.nuki.internal.constants.OpenerAction;
23 import org.openhab.binding.nuki.internal.dataexchange.BridgeLockActionResponse;
24 import org.openhab.binding.nuki.internal.dto.BridgeApiDeviceStateDto;
25 import org.openhab.core.library.types.DecimalType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.thing.ChannelUID;
28 import org.openhab.core.thing.Thing;
29 import org.openhab.core.types.Command;
32 * Thing handler for Nuki Opener
34 * @author Jan Vybíral - Initial contribution
37 public class NukiOpenerHandler extends AbstractNukiDeviceHandler<NukiDeviceConfiguration> {
39 public NukiOpenerHandler(Thing thing, boolean readOnly) {
40 super(thing, readOnly);
43 private volatile Instant lastRingAction = Instant.EPOCH;
46 public void refreshState(BridgeApiDeviceStateDto state) {
47 updateState(NukiBindingConstants.CHANNEL_OPENER_LOW_BATTERY, state.isBatteryCritical(), OnOffType::from);
48 updateState(NukiBindingConstants.CHANNEL_OPENER_STATE, state.getState(), DecimalType::new);
49 updateState(NukiBindingConstants.CHANNEL_OPENER_MODE, state.getMode(), DecimalType::new);
50 updateState(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_TIMESTAMP, state.getRingactionTimestamp(),
53 if (Objects.equals(state.getRingactionState(), true)
54 && Duration.between(lastRingAction, Instant.now()).getSeconds() > 30) {
55 triggerChannel(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_STATE, NukiBindingConstants.EVENT_RINGING);
56 lastRingAction = Instant.now();
61 protected int getDeviceType() {
62 return NukiBindingConstants.DEVICE_OPENER;
66 protected boolean doHandleCommand(ChannelUID channelUID, Command command) {
67 switch (channelUID.getId()) {
68 case NukiBindingConstants.CHANNEL_OPENER_STATE:
69 if (command instanceof DecimalType decimalCommand) {
70 OpenerAction action = OpenerAction.fromAction(decimalCommand.intValue());
72 return withHttpClient(client -> {
73 BridgeLockActionResponse response = client.getOpenerAction(configuration.nukiId, action);
74 return handleResponse(response, channelUID.getAsString(), command.toString());
84 protected Class<NukiDeviceConfiguration> getConfigurationClass() {
85 return NukiDeviceConfiguration.class;