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.dali.internal.handler;
15 import static org.openhab.binding.dali.internal.DaliBindingConstants.*;
17 import java.math.BigDecimal;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.dali.internal.protocol.DaliAddress;
22 import org.openhab.binding.dali.internal.protocol.DaliDAPCCommand;
23 import org.openhab.binding.dali.internal.protocol.DaliResponse;
24 import org.openhab.binding.dali.internal.protocol.DaliStandardCommand;
25 import org.openhab.core.library.types.IncreaseDecreaseType;
26 import org.openhab.core.library.types.OnOffType;
27 import org.openhab.core.library.types.PercentType;
28 import org.openhab.core.thing.Bridge;
29 import org.openhab.core.thing.ChannelUID;
30 import org.openhab.core.thing.Thing;
31 import org.openhab.core.thing.ThingStatus;
32 import org.openhab.core.thing.ThingStatusDetail;
33 import org.openhab.core.thing.binding.BaseThingHandler;
34 import org.openhab.core.thing.binding.BridgeHandler;
35 import org.openhab.core.types.Command;
36 import org.openhab.core.types.RefreshType;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * The {@link DaliDeviceHandler} handles commands for things of type Device and Group.
43 * @author Robert Schmid - Initial contribution
46 public class DaliDeviceHandler extends BaseThingHandler {
47 private final Logger logger = LoggerFactory.getLogger(DaliDeviceHandler.class);
48 private @Nullable Integer targetId;
50 public DaliDeviceHandler(Thing thing) {
55 public void initialize() {
56 Bridge bridge = getBridge();
59 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
61 updateStatus(ThingStatus.ONLINE);
64 targetId = ((BigDecimal) this.thing.getConfiguration().get(TARGET_ID)).intValueExact();
68 public void handleCommand(ChannelUID channelUID, Command command) {
70 if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())
71 || CHANNEL_DIM_IMMEDIATELY.equals(channelUID.getId())) {
73 if (THING_TYPE_DEVICE.equals(this.thing.getThingTypeUID())) {
74 address = DaliAddress.createShortAddress(targetId);
75 } else if (THING_TYPE_GROUP.equals(this.thing.getThingTypeUID())) {
76 address = DaliAddress.createGroupAddress(targetId);
78 throw new DaliException("unknown device type");
81 boolean queryDeviceState = false;
83 if (command instanceof PercentType) {
84 byte dimmValue = (byte) ((((PercentType) command).floatValue() * DALI_SWITCH_100_PERCENT) / 100);
85 // A dimm value of zero is handled correctly by DALI devices, i.e. they are turned off
86 getBridgeHandler().sendCommand(new DaliDAPCCommand(address, dimmValue));
87 } else if (command instanceof OnOffType) {
88 if ((OnOffType) command == OnOffType.ON) {
89 getBridgeHandler().sendCommand(new DaliDAPCCommand(address, (byte) DALI_SWITCH_100_PERCENT));
91 getBridgeHandler().sendCommand(DaliStandardCommand.createOffCommand(address));
93 } else if (command instanceof IncreaseDecreaseType) {
94 if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())) {
95 if ((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE) {
96 getBridgeHandler().sendCommand(DaliStandardCommand.createUpCommand(address));
98 getBridgeHandler().sendCommand(DaliStandardCommand.createDownCommand(address));
101 if ((IncreaseDecreaseType) command == IncreaseDecreaseType.INCREASE) {
102 getBridgeHandler().sendCommand(DaliStandardCommand.createStepUpCommand(address));
104 getBridgeHandler().sendCommand(DaliStandardCommand.createStepDownCommand(address));
107 queryDeviceState = true;
108 } else if (command instanceof RefreshType) {
109 queryDeviceState = true;
112 if (queryDeviceState) {
114 .sendCommandWithResponse(DaliStandardCommand.createQueryActualLevelCommand(address),
115 DaliResponse.NumericMask.class)
116 .thenAccept(response -> {
117 if (response != null && !response.mask) {
118 Integer value = response.value != null ? response.value : 0;
119 int percentValue = (int) (value.floatValue() * 100 / DALI_SWITCH_100_PERCENT);
120 updateState(channelUID, new PercentType(percentValue));
122 }).exceptionally(e -> {
123 logger.warn("Error querying device status: {}", e.getMessage());
128 } catch (DaliException e) {
129 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
133 protected DaliserverBridgeHandler getBridgeHandler() throws DaliException {
134 Bridge bridge = this.getBridge();
135 if (bridge == null) {
136 throw new DaliException("No bridge was found");
139 BridgeHandler handler = bridge.getHandler();
140 if (handler == null) {
141 throw new DaliException("No handler was found");
144 return (DaliserverBridgeHandler) handler;