]> git.basschouten.com Git - openhab-addons.git/blob
32d7f465c771e448153a514ff8217b7a52c27107
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.luxom.internal.handler;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.concurrent.atomic.AtomicReference;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.luxom.internal.LuxomBindingConstants;
22 import org.openhab.binding.luxom.internal.handler.config.LuxomThingDimmerConfig;
23 import org.openhab.binding.luxom.internal.handler.util.PercentageConverter;
24 import org.openhab.binding.luxom.internal.protocol.LuxomAction;
25 import org.openhab.binding.luxom.internal.protocol.LuxomCommand;
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.types.Command;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38 /**
39  * The {@link LuxomDimmerHandler} is responsible for handling commands, which are
40  * sent to one of the channels.
41  *
42  * @author Kris Jespers - Initial contribution
43  */
44 @NonNullByDefault
45 public class LuxomDimmerHandler extends LuxomThingHandler {
46     private final Logger logger = LoggerFactory.getLogger(LuxomDimmerHandler.class);
47
48     public LuxomDimmerHandler(Thing thing) {
49         super(thing);
50     }
51
52     private @Nullable LuxomThingDimmerConfig config;
53     private final AtomicReference<Integer> lastLightLevel = new AtomicReference<>(0);
54
55     @Override
56     public void initialize() {
57         super.initialize();
58         config = getConfig().as(LuxomThingDimmerConfig.class);
59
60         logger.debug("Initializing Switch handler for address {}", getAddress());
61
62         initDeviceState();
63     }
64
65     @Override
66     protected void initDeviceState() {
67         logger.debug("Initializing device state for Switch {}", getAddress());
68         @Nullable
69         Bridge bridge = getBridge();
70         if (bridge == null) {
71             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
72         } else if (ThingStatus.ONLINE.equals(bridge.getStatus())) {
73             if (config != null && config.doesNotReply) {
74                 logger.debug("Switch {} will not reply, so always keeping it ONLINE", getAddress());
75                 updateStatus(ThingStatus.ONLINE);
76             } else {
77                 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "@text/status.awaiting-initial-response");
78                 ping(); // handleUpdate() will set thing status to online when response arrives
79             }
80         } else {
81             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
82         }
83     }
84
85     @Override
86     public void handleCommand(ChannelUID channelUID, Command command) {
87         logger.debug("dimmer at address {} received command {} for {}", getAddress(), command.toFullString(),
88                 channelUID);
89         if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())) {
90             if (OnOffType.ON.equals(command)) {
91                 set();
92             } else if (OnOffType.OFF.equals(command)) {
93                 clear();
94             }
95         } else if (LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId()) && config != null) {
96             if (command instanceof Number number) {
97                 int level = number.intValue();
98                 logger.trace("dimmer at address {} just setting dimmer level", getAddress());
99                 dim(level);
100             } else if (command instanceof IncreaseDecreaseType increaseDecreaseCommand) {
101                 int currentValue = lastLightLevel.get();
102                 int newValue;
103                 if (IncreaseDecreaseType.INCREASE.equals(increaseDecreaseCommand)) {
104                     newValue = currentValue + config.stepPercentage;
105                     // round down to step multiple
106                     newValue = newValue - newValue % config.stepPercentage;
107                     logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
108                     dim(newValue);
109                 } else {
110                     newValue = currentValue - config.stepPercentage;
111                     // round up to step multiple
112                     newValue = newValue + newValue % config.stepPercentage;
113                     logger.trace("dimmer at address {} just increasing dimmer level", getAddress());
114                     dim(Math.max(newValue, 0));
115                 }
116             } else if (OnOffType.ON.equals(command)) {
117                 if (config.onToLast) {
118                     dim(lastLightLevel.get());
119                 } else {
120                     dim(config.onLevel.intValue());
121                 }
122             } else if (OnOffType.OFF.equals(command)) {
123                 dim(0);
124             }
125         }
126     }
127
128     @Override
129     public void handleCommandComingFromBridge(LuxomCommand command) {
130         updateStatus(ThingStatus.ONLINE);
131         if (LuxomAction.CLEAR_RESPONSE.equals(command.getAction())) {
132             updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.OFF);
133         } else if (LuxomAction.SET_RESPONSE.equals(command.getAction())) {
134             updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.ON);
135         } else if (LuxomAction.DATA_RESPONSE.equals(command.getAction())) {
136             int percentage = PercentageConverter.getPercentage(command.getData());
137
138             lastLightLevel.set(percentage);
139             updateState(LuxomBindingConstants.CHANNEL_BRIGHTNESS, new PercentType(percentage));
140         }
141     }
142
143     @Override
144     public void channelLinked(ChannelUID channelUID) {
145         logger.debug("dimmer at address {} linked to channel {}", getAddress(), channelUID);
146         if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())
147                 || LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId())) {
148             // Refresh state when new item is linked.
149             if (config != null && !config.doesNotReply) {
150                 ping();
151             }
152         }
153     }
154
155     /**
156      * example : *A,0,2,2B;*Z,057;
157      */
158     private void dim(int percentage) {
159         logger.debug("dimming dimmer at address {} to {} %", getAddress(), percentage);
160         List<CommandExecutionSpecification> commands = new ArrayList<>(3);
161         if (percentage == 0) {
162             commands.add(new CommandExecutionSpecification(LuxomAction.CLEAR.getCommand() + ",0," + getAddress()));
163         } else {
164             commands.add(new CommandExecutionSpecification(LuxomAction.SET.getCommand() + ",0," + getAddress()));
165         }
166         commands.add(new CommandExecutionSpecification(LuxomAction.DATA.getCommand() + ",0," + getAddress()));
167         commands.add(new CommandExecutionSpecification(
168                 LuxomAction.DATA_BYTE.getCommand() + ",0" + PercentageConverter.getHexRepresentation(percentage)));
169
170         sendCommands(commands);
171     }
172 }