]> git.basschouten.com Git - openhab-addons.git/blob
8a59367dc67c5ec11559e86404560e4b25decebe
[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.lutron.internal.handler;
14
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.CHANNEL_SWITCH;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.openhab.binding.lutron.internal.protocol.OutputCommand;
21 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
22 import org.openhab.binding.lutron.internal.protocol.lip.TargetType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.Bridge;
25 import org.openhab.core.thing.ChannelUID;
26 import org.openhab.core.thing.Thing;
27 import org.openhab.core.thing.ThingStatus;
28 import org.openhab.core.thing.ThingStatusDetail;
29 import org.openhab.core.types.Command;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * Handler responsible for communicating with a switch.
35  *
36  * @author Allan Tong - Initial contribution
37  * @author Bob Adair - Added initDeviceState method
38  */
39 @NonNullByDefault
40 public class SwitchHandler extends LutronHandler {
41     private final Logger logger = LoggerFactory.getLogger(SwitchHandler.class);
42
43     private int integrationId;
44
45     public SwitchHandler(Thing thing) {
46         super(thing);
47     }
48
49     @Override
50     public void initialize() {
51         Number id = (Number) getThing().getConfiguration().get("integrationId");
52         if (id == null) {
53             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId");
54             return;
55         }
56         integrationId = id.intValue();
57         logger.debug("Initializing Switch handler for integration ID {}", id);
58
59         initDeviceState();
60     }
61
62     @Override
63     protected void initDeviceState() {
64         logger.debug("Initializing device state for Switch {}", getIntegrationId());
65         Bridge bridge = getBridge();
66         if (bridge == null) {
67             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
68         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
69             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
70             queryOutput(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL);
71             // handleUpdate() will set thing status to online when response arrives
72         } else {
73             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
74         }
75     }
76
77     @Override
78     public void handleCommand(ChannelUID channelUID, Command command) {
79         if (channelUID.getId().equals(CHANNEL_SWITCH)) {
80             if (command.equals(OnOffType.ON)) {
81                 output(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL, 100, null, null);
82             } else if (command.equals(OnOffType.OFF)) {
83                 output(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL, 0, null, null);
84             }
85         }
86     }
87
88     @Override
89     public int getIntegrationId() {
90         return this.integrationId;
91     }
92
93     @Override
94     public void handleUpdate(LutronCommandType type, String... parameters) {
95         if (type == LutronCommandType.OUTPUT && parameters.length > 1
96                 && OutputCommand.ACTION_ZONELEVEL.toString().equals(parameters[0])) {
97             BigDecimal level = new BigDecimal(parameters[1]);
98             if (getThing().getStatus() == ThingStatus.UNKNOWN) {
99                 updateStatus(ThingStatus.ONLINE);
100             }
101             postCommand(CHANNEL_SWITCH, OnOffType.from(level.compareTo(BigDecimal.ZERO) != 0));
102         }
103     }
104
105     @Override
106     public void channelLinked(ChannelUID channelUID) {
107         if (channelUID.getId().equals(CHANNEL_SWITCH)) {
108             // Refresh state when new item is linked.
109             queryOutput(TargetType.SWITCH, OutputCommand.ACTION_ZONELEVEL);
110         }
111     }
112 }