]> git.basschouten.com Git - openhab-addons.git/blob
4c1c431bd55eb73234fcbb37348a945aed855286
[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_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.SysvarCommand;
23 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
24 import org.openhab.core.library.types.DecimalType;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.types.Command;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Handler responsible for getting/setting sysvar state for HomeWorks QS
36  *
37  * @author Bob Adair - Initial contribution
38  *
39  */
40 @NonNullByDefault
41 public class SysvarHandler extends LutronHandler {
42     private final Logger logger = LoggerFactory.getLogger(SysvarHandler.class);
43
44     private @Nullable SysvarConfig config;
45     private int integrationId;
46
47     public SysvarHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     public int getIntegrationId() {
53         SysvarConfig config = this.config;
54         if (config != null) {
55             return config.integrationId;
56         } else {
57             throw new IllegalStateException("handler not initialized");
58         }
59     }
60
61     @Override
62     public void initialize() {
63         SysvarConfig config = getThing().getConfiguration().as(SysvarConfig.class);
64         this.config = config;
65         if (config.integrationId <= 0) {
66             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId configured");
67         } else {
68             integrationId = config.integrationId;
69             logger.debug("Initializing Sysvar handler for integration ID {}", integrationId);
70             initDeviceState();
71         }
72     }
73
74     @Override
75     protected void initDeviceState() {
76         logger.debug("Initializing handler state for sysvar id {}", integrationId);
77         Bridge bridge = getBridge();
78         if (bridge == null) {
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
80         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
81             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
82             querySysvar(SysvarCommand.ACTION_GETSETSYSVAR);
83             // 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(SysvarCommand.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(SysvarCommand.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                 && SysvarCommand.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 }