2 * Copyright (c) 2010-2021 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;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.nuki.internal.configuration.NukiDeviceConfiguration;
20 import org.openhab.binding.nuki.internal.constants.NukiBindingConstants;
21 import org.openhab.binding.nuki.internal.constants.OpenerAction;
22 import org.openhab.binding.nuki.internal.dataexchange.BridgeLockActionResponse;
23 import org.openhab.binding.nuki.internal.dto.BridgeApiDeviceStateDto;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.types.Command;
31 * Thing handler for Nuki Opener
33 * @author Jan Vybíral - Initial contribution
36 public class NukiOpenerHandler extends AbstractNukiDeviceHandler<NukiDeviceConfiguration> {
38 public NukiOpenerHandler(Thing thing) {
42 private volatile Instant lastRingAction = Instant.EPOCH;
45 public void refreshState(BridgeApiDeviceStateDto state) {
46 updateState(NukiBindingConstants.CHANNEL_OPENER_LOW_BATTERY, state.isBatteryCritical(), OnOffType::from);
47 updateState(NukiBindingConstants.CHANNEL_OPENER_STATE, state.getState(), DecimalType::new);
48 updateState(NukiBindingConstants.CHANNEL_OPENER_MODE, state.getMode(), DecimalType::new);
49 updateState(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_TIMESTAMP, state.getRingactionTimestamp(),
52 if (state.getRingactionState() && Duration.between(lastRingAction, Instant.now()).getSeconds() > 30) {
53 triggerChannel(NukiBindingConstants.CHANNEL_OPENER_RING_ACTION_STATE, NukiBindingConstants.EVENT_RINGING);
54 lastRingAction = Instant.now();
59 protected int getDeviceType() {
60 return NukiBindingConstants.DEVICE_OPENER;
64 protected boolean doHandleCommand(ChannelUID channelUID, Command command) {
65 switch (channelUID.getId()) {
66 case NukiBindingConstants.CHANNEL_OPENER_STATE:
67 if (command instanceof DecimalType) {
68 OpenerAction action = OpenerAction.fromAction(((DecimalType) command).intValue());
70 return withHttpClient(client -> {
71 BridgeLockActionResponse response = client.getOpenerAction(configuration.nukiId, action);
72 return handleResponse(response, channelUID.getAsString(), command.toString());
82 protected Class<NukiDeviceConfiguration> getConfigurationClass() {
83 return NukiDeviceConfiguration.class;