2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.lutron.internal.handler;
15 import static org.openhab.binding.lutron.internal.LutronBindingConstants.CHANNEL_GROUPSTATE;
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;
33 * Handler responsible for communicating occupancy group states.
35 * @author Bob Adair - Initial contribution
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";
43 private final Logger logger = LoggerFactory.getLogger(OGroupHandler.class);
45 private @NonNullByDefault({}) OGroupConfig config;
47 public OGroupHandler(Thing thing) {
52 public int getIntegrationId() {
53 if (this.config == null) {
54 throw new IllegalStateException("handler not initialized");
56 return config.integrationId;
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");
67 logger.debug("Initializing Occupancy Group handler for integration ID {}", getIntegrationId());
72 protected void initDeviceState() {
73 logger.debug("Initializing device state for Occupancy Group {}", getIntegrationId());
74 Bridge bridge = getBridge();
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
82 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
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);
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);
104 public void handleUpdate(LutronCommandType type, String... parameters) {
107 if (type == LutronCommandType.GROUP && parameters.length > 1
108 && GroupCommand.ACTION_GROUPSTATE.toString().equals(parameters[0])) {
110 state = Integer.parseInt(parameters[1]);
111 } catch (NumberFormatException e) {
112 logger.debug("Error parsing response parameter: {}", e.getMessage());
115 if (getThing().getStatus() == ThingStatus.UNKNOWN) {
116 updateStatus(ThingStatus.ONLINE);
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));
125 logger.debug("Invalid occupancy state received: {}", state);
126 updateState(CHANNEL_GROUPSTATE, new StringType(STATE_UNKNOWN));