]> git.basschouten.com Git - openhab-addons.git/blob
cefb45798391f3bfbc55a5da61752ae7a0a1d746
[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 org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.luxom.internal.LuxomBindingConstants;
17 import org.openhab.binding.luxom.internal.protocol.LuxomAction;
18 import org.openhab.binding.luxom.internal.protocol.LuxomCommand;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * The {@link LuxomSwitchHandler} is responsible for handling commands, which are
31  * sent to one of the channels.
32  *
33  * @author Kris Jespers - Initial contribution
34  */
35 @NonNullByDefault
36 public class LuxomSwitchHandler extends LuxomThingHandler {
37     private final Logger logger = LoggerFactory.getLogger(LuxomSwitchHandler.class);
38
39     public LuxomSwitchHandler(Thing thing) {
40         super(thing);
41     }
42
43     @Override
44     public void initialize() {
45         super.initialize();
46
47         logger.debug("Initializing Switch handler for address {}", getAddress());
48
49         initDeviceState();
50     }
51
52     @Override
53     protected void initDeviceState() {
54         logger.debug("Initializing device state for Switch {}", getAddress());
55         Bridge bridge = getBridge();
56         if (bridge == null) {
57             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
58         } else if (ThingStatus.ONLINE.equals(bridge.getStatus())) {
59             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "@text/status.awaiting-initial-response");
60             ping(); // handleUpdate() will set thing status to online when response arrives
61         } else {
62             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
63         }
64     }
65
66     @Override
67     public void handleCommand(ChannelUID channelUID, Command command) {
68         logger.debug("switch at address {} received command {} for {}", getAddress(), command.toFullString(),
69                 channelUID);
70         if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())) {
71             if (OnOffType.ON.equals(command)) {
72                 set();
73                 ping(); // to make sure we know the current state
74             } else if (OnOffType.OFF.equals(command)) {
75                 clear();
76                 ping(); // to make sure we know the current state
77             }
78         }
79     }
80
81     @Override
82     public void handleCommandComingFromBridge(LuxomCommand command) {
83         if (LuxomAction.CLEAR_RESPONSE.equals(command.getAction())) {
84             updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.OFF);
85             updateStatus(ThingStatus.ONLINE);
86         } else if (LuxomAction.SET_RESPONSE.equals(command.getAction())) {
87             updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.ON);
88             updateStatus(ThingStatus.ONLINE);
89         }
90     }
91
92     @Override
93     public void channelLinked(ChannelUID channelUID) {
94         logger.debug("switch at address {} linked to channel {}", getAddress(), channelUID);
95         if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())
96                 || LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId())) {
97             // Refresh state when new item is linked.
98             ping();
99         }
100     }
101 }