]> git.basschouten.com Git - openhab-addons.git/blob
a37ccaa1651555adfeabcfe6144c04256f26f9cd
[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_VARSTATE;
16
17 import java.math.BigDecimal;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.openhab.binding.lutron.internal.config.SysvarConfig;
22 import org.openhab.binding.lutron.internal.protocol.LutronCommandType;
23 import org.openhab.core.library.types.DecimalType;
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 getting/setting sysvar state for HomeWorks QS
35  *
36  * @author Bob Adair - Initial contribution
37  *
38  */
39 @NonNullByDefault
40 public class SysvarHandler extends LutronHandler {
41     private static final Integer ACTION_GETSETSYSVAR = 1;
42
43     private final Logger logger = LoggerFactory.getLogger(SysvarHandler.class);
44
45     private @Nullable SysvarConfig config;
46     private int integrationId;
47
48     public SysvarHandler(Thing thing) {
49         super(thing);
50     }
51
52     @Override
53     public int getIntegrationId() {
54         SysvarConfig config = this.config;
55         if (config != null) {
56             return config.integrationId;
57         } else {
58             throw new IllegalStateException("handler not initialized");
59         }
60     }
61
62     @Override
63     public void initialize() {
64         SysvarConfig config = getThing().getConfiguration().as(SysvarConfig.class);
65         this.config = config;
66         if (config.integrationId <= 0) {
67             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId configured");
68         } else {
69             integrationId = config.integrationId;
70             logger.debug("Initializing Sysvar handler for integration ID {}", integrationId);
71             initDeviceState();
72         }
73     }
74
75     @Override
76     protected void initDeviceState() {
77         logger.debug("Initializing handler state for sysvar id {}", integrationId);
78         Bridge bridge = getBridge();
79         if (bridge == null) {
80             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
81         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
82             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
83             querySysvar(ACTION_GETSETSYSVAR); // handleUpdate() will set thing status to online when response arrives
84         } else {
85             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
86         }
87     }
88
89     @Override
90     public void channelLinked(ChannelUID channelUID) {
91         if (channelUID.getId().equals(CHANNEL_VARSTATE)) {
92             // Refresh state when new item is linked.
93             querySysvar(ACTION_GETSETSYSVAR);
94         }
95     }
96
97     @Override
98     public void handleCommand(ChannelUID channelUID, Command command) {
99         if (channelUID.getId().equals(CHANNEL_VARSTATE)) {
100             if (command instanceof Number) {
101                 int state = ((Number) command).intValue();
102                 sysvar(ACTION_GETSETSYSVAR, state);
103             }
104         }
105     }
106
107     @Override
108     public void handleUpdate(LutronCommandType type, String... parameters) {
109         if (type == LutronCommandType.SYSVAR && parameters.length > 1
110                 && ACTION_GETSETSYSVAR.toString().equals(parameters[0])) {
111             BigDecimal state = new BigDecimal(parameters[1]);
112             if (getThing().getStatus() == ThingStatus.UNKNOWN) {
113                 updateStatus(ThingStatus.ONLINE);
114             }
115             updateState(CHANNEL_VARSTATE, new DecimalType(state));
116         }
117     }
118 }