]> git.basschouten.com Git - openhab-addons.git/blob
437434f21d3884242103f4e6f065b1dfa3febb71
[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_GROUPSTATE;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.lutron.internal.config.OGroupConfig;
19 import org.openhab.binding.lutron.internal.protocol.GroupCommand;
20 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
21 import org.openhab.core.library.types.StringType;
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.openhab.core.types.RefreshType;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Handler responsible for communicating occupancy group states.
34  *
35  * @author Bob Adair - Initial contribution
36  */
37 @NonNullByDefault
38 public class OGroupHandler extends LutronHandler {
39     private static final String STATE_OCCUPIED = "OCCUPIED";
40     private static final String STATE_UNOCCUPIED = "UNOCCUPIED";
41     private static final String STATE_UNKNOWN = "UNKNOWN";
42
43     private final Logger logger = LoggerFactory.getLogger(OGroupHandler.class);
44
45     private @NonNullByDefault({}) OGroupConfig config;
46
47     public OGroupHandler(Thing thing) {
48         super(thing);
49     }
50
51     @Override
52     public int getIntegrationId() {
53         if (this.config == null) {
54             throw new IllegalStateException("handler not initialized");
55         }
56         return config.integrationId;
57     }
58
59     @Override
60     public void initialize() {
61         config = getConfigAs(OGroupConfig.class);
62         if (config.integrationId <= 0) {
63             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
64                     "No valid integrationId configured");
65             return;
66         }
67         logger.debug("Initializing Occupancy Group handler for integration ID {}", getIntegrationId());
68         initDeviceState();
69     }
70
71     @Override
72     protected void initDeviceState() {
73         logger.debug("Initializing device state for Occupancy Group {}", getIntegrationId());
74         Bridge bridge = getBridge();
75         if (bridge == null) {
76             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
77         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
78             updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "Awaiting initial response");
79             queryGroup(GroupCommand.ACTION_GROUPSTATE);
80             // handleUpdate() will set thing status to online when response arrives
81         } else {
82             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
83         }
84     }
85
86     @Override
87     public void channelLinked(ChannelUID channelUID) {
88         if (channelUID.getId().equals(CHANNEL_GROUPSTATE)) {
89             // Refresh state when new item is linked.
90             queryGroup(GroupCommand.ACTION_GROUPSTATE);
91         }
92     }
93
94     @Override
95     public void handleCommand(ChannelUID channelUID, Command command) {
96         if (channelUID.getId().equals(CHANNEL_GROUPSTATE)) {
97             if (command instanceof RefreshType) {
98                 queryGroup(GroupCommand.ACTION_GROUPSTATE);
99             }
100         }
101     }
102
103     @Override
104     public void handleUpdate(LutronCommandType type, String... parameters) {
105         int state;
106
107         if (type == LutronCommandType.GROUP && parameters.length > 1
108                 && GroupCommand.ACTION_GROUPSTATE.toString().equals(parameters[0])) {
109             try {
110                 state = Integer.parseInt(parameters[1]);
111             } catch (NumberFormatException e) {
112                 logger.debug("Error parsing response parameter: {}", e.getMessage());
113                 return;
114             }
115             if (getThing().getStatus() == ThingStatus.UNKNOWN) {
116                 updateStatus(ThingStatus.ONLINE);
117             }
118             if (state == GroupCommand.STATE_GRP_OCCUPIED) {
119                 updateState(CHANNEL_GROUPSTATE, new StringType(STATE_OCCUPIED));
120             } else if (state == GroupCommand.STATE_GRP_UNOCCUPIED) {
121                 updateState(CHANNEL_GROUPSTATE, new StringType(STATE_UNOCCUPIED));
122             } else if (state == GroupCommand.STATE_GRP_UNKNOWN) {
123                 updateState(CHANNEL_GROUPSTATE, new StringType(STATE_UNKNOWN));
124             } else {
125                 logger.debug("Invalid occupancy state received: {}", state);
126                 updateState(CHANNEL_GROUPSTATE, new StringType(STATE_UNKNOWN));
127             }
128         }
129     }
130 }