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.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.config.core.Configuration;
26 import org.openhab.core.library.types.IncreaseDecreaseType;
27 import org.openhab.core.library.types.OnOffType;
28 import org.openhab.core.library.types.PercentType;
29 import org.openhab.core.thing.Bridge;
30 import org.openhab.core.thing.ChannelUID;
31 import org.openhab.core.thing.Thing;
32 import org.openhab.core.thing.ThingStatus;
33 import org.openhab.core.thing.ThingStatusDetail;
34 import org.openhab.core.thing.binding.BaseThingHandler;
35 import org.openhab.core.thing.binding.BridgeHandler;
36 import org.openhab.core.types.Command;
37 import org.openhab.core.types.RefreshType;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
42 * The {@link DaliDeviceHandler} handles commands for things of type Device and Group.
44 * @author Robert Schmid - Initial contribution
47 public class DaliDeviceHandler extends BaseThingHandler {
48 private final Logger logger = LoggerFactory.getLogger(DaliDeviceHandler.class);
49 protected @Nullable Integer targetId;
50 protected @Nullable Integer readDeviceTargetId;
52 public DaliDeviceHandler(Thing thing) {
57 public void initialize() {
58 Bridge bridge = getBridge();
61 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
63 updateStatus(ThingStatus.ONLINE);
66 final Configuration conf = this.thing.getConfiguration();
67 targetId = ((BigDecimal) conf.get(TARGET_ID)).intValueExact();
68 // Reading from group addresses does not work generally, so if a fallback device id is
69 // defined, use that instead when reading the current state
70 if (conf.get(READ_DEVICE_TARGET_ID) != null) {
71 readDeviceTargetId = ((BigDecimal) this.thing.getConfiguration().get(READ_DEVICE_TARGET_ID))
74 readDeviceTargetId = null;
79 public void handleCommand(ChannelUID channelUID, Command command) {
81 if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())
82 || CHANNEL_DIM_IMMEDIATELY.equals(channelUID.getId())) {
84 if (THING_TYPE_DEVICE.equals(this.thing.getThingTypeUID())
85 || THING_TYPE_DEVICE_DT8.equals(this.thing.getThingTypeUID())) {
86 address = DaliAddress.createShortAddress(targetId);
87 } else if (THING_TYPE_GROUP.equals(this.thing.getThingTypeUID())
88 || THING_TYPE_GROUP_DT8.equals(this.thing.getThingTypeUID())) {
89 address = DaliAddress.createGroupAddress(targetId);
91 throw new DaliException("unknown device type");
94 boolean queryDeviceState = false;
96 if (command instanceof PercentType percentCommand) {
97 byte dimmValue = (byte) ((percentCommand.floatValue() * DALI_SWITCH_100_PERCENT) / 100);
98 // A dimm value of zero is handled correctly by DALI devices, i.e. they are turned off
99 getBridgeHandler().sendCommand(new DaliDAPCCommand(address, dimmValue));
100 } else if (command instanceof OnOffType onOffCommand) {
101 if (onOffCommand == OnOffType.ON) {
102 getBridgeHandler().sendCommand(new DaliDAPCCommand(address, (byte) DALI_SWITCH_100_PERCENT));
104 getBridgeHandler().sendCommand(DaliStandardCommand.createOffCommand(address));
106 } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
107 if (CHANNEL_DIM_AT_FADE_RATE.equals(channelUID.getId())) {
108 if (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) {
109 getBridgeHandler().sendCommand(DaliStandardCommand.createUpCommand(address));
111 getBridgeHandler().sendCommand(DaliStandardCommand.createDownCommand(address));
114 if (increaseDecreaseCommand == IncreaseDecreaseType.INCREASE) {
115 getBridgeHandler().sendCommand(DaliStandardCommand.createStepUpCommand(address));
117 getBridgeHandler().sendCommand(DaliStandardCommand.createStepDownCommand(address));
120 queryDeviceState = true;
121 } else if (command instanceof RefreshType) {
122 queryDeviceState = true;
125 if (queryDeviceState) {
126 DaliAddress readAddress = address;
127 if (readDeviceTargetId != null) {
128 readAddress = DaliAddress.createShortAddress(readDeviceTargetId);
131 .sendCommandWithResponse(DaliStandardCommand.createQueryActualLevelCommand(readAddress),
132 DaliResponse.NumericMask.class)
133 .thenAccept(response -> {
134 if (response != null && !response.mask) {
135 Integer value = response.value != null ? response.value : 0;
136 int percentValue = (int) (value.floatValue() * 100 / DALI_SWITCH_100_PERCENT);
137 updateState(channelUID, new PercentType(percentValue));
139 }).exceptionally(e -> {
140 logger.warn("Error querying device status: {}", e.getMessage());
145 } catch (DaliException e) {
146 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
150 protected DaliserverBridgeHandler getBridgeHandler() throws DaliException {
151 Bridge bridge = this.getBridge();
152 if (bridge == null) {
153 throw new DaliException("No bridge was found");
156 BridgeHandler handler = bridge.getHandler();
157 if (handler == null) {
158 throw new DaliException("No handler was found");
161 return (DaliserverBridgeHandler) handler;