]> git.basschouten.com Git - openhab-addons.git/blob
4362458d00a6fdd2698ffd3dd5cc7bb67a27f119
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2020 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.LutronCommandType;
21 import org.openhab.core.library.types.OnOffType;
22 import org.openhab.core.thing.Bridge;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.thing.ThingStatus;
26 import org.openhab.core.thing.ThingStatusDetail;
27 import org.openhab.core.types.Command;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * Handler responsible for communicating with a switch.
33  *
34  * @author Allan Tong - Initial contribution
35  * @author Bob Adair - Added initDeviceState method
36  */
37 @NonNullByDefault
38 public class SwitchHandler extends LutronHandler {
39     private static final Integer ACTION_ZONELEVEL = 1;
40
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(ACTION_ZONELEVEL); // handleUpdate() will set thing status to online when response arrives
71         } else {
72             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
73         }
74     }
75
76     @Override
77     public void handleCommand(ChannelUID channelUID, Command command) {
78         if (channelUID.getId().equals(CHANNEL_SWITCH)) {
79             if (command.equals(OnOffType.ON)) {
80                 output(ACTION_ZONELEVEL, 100);
81             } else if (command.equals(OnOffType.OFF)) {
82                 output(ACTION_ZONELEVEL, 0);
83             }
84         }
85     }
86
87     @Override
88     public int getIntegrationId() {
89         return this.integrationId;
90     }
91
92     @Override
93     public void handleUpdate(LutronCommandType type, String... parameters) {
94         if (type == LutronCommandType.OUTPUT && parameters.length > 1
95                 && ACTION_ZONELEVEL.toString().equals(parameters[0])) {
96             BigDecimal level = new BigDecimal(parameters[1]);
97             if (getThing().getStatus() == ThingStatus.UNKNOWN) {
98                 updateStatus(ThingStatus.ONLINE);
99             }
100             postCommand(CHANNEL_SWITCH, level.compareTo(BigDecimal.ZERO) == 0 ? OnOffType.OFF : OnOffType.ON);
101         }
102     }
103
104     @Override
105     public void channelLinked(ChannelUID channelUID) {
106         if (channelUID.getId().equals(CHANNEL_SWITCH)) {
107             // Refresh state when new item is linked.
108             queryOutput(ACTION_ZONELEVEL);
109         }
110     }
111 }