2 * Copyright (c) 2010-2020 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_OCCUPANCYSTATUS;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.lutron.internal.protocol.LutronCommandType;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
31 * @author Allan Tong - Initial contribution
32 * @author Bob Adair - Added initDeviceState method
35 public class OccupancySensorHandler extends LutronHandler {
36 private static final String OCCUPIED_STATE_UPDATE = "2";
37 private static final String STATE_OCCUPIED = "3";
38 private static final String STATE_UNOCCUPIED = "4";
40 private final Logger logger = LoggerFactory.getLogger(OccupancySensorHandler.class);
42 private int integrationId;
44 public OccupancySensorHandler(Thing thing) {
49 public void handleCommand(ChannelUID channelUID, Command command) {
53 public void initialize() {
54 Number id = (Number) getThing().getConfiguration().get("integrationId");
56 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId");
59 integrationId = id.intValue();
60 logger.debug("Initializing Occupancy Sensor handler for integration ID {}", id);
66 protected void initDeviceState() {
67 logger.debug("Initializing device state for Occupancy Sensor {}", getIntegrationId());
68 Bridge bridge = getBridge();
70 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
71 } else if (bridge.getStatus() == ThingStatus.ONLINE) {
72 updateStatus(ThingStatus.ONLINE); // can't poll this device, so assume it is online if the bridge is
74 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
79 public int getIntegrationId() {
80 return this.integrationId;
84 public void handleUpdate(LutronCommandType type, String... parameters) {
85 if (type == LutronCommandType.DEVICE && parameters.length == 2 && OCCUPIED_STATE_UPDATE.equals(parameters[0])) {
86 if (STATE_OCCUPIED.equals(parameters[1])) {
87 updateState(CHANNEL_OCCUPANCYSTATUS, OnOffType.ON);
88 } else if (STATE_UNOCCUPIED.equals(parameters[1])) {
89 updateState(CHANNEL_OCCUPANCYSTATUS, OnOffType.OFF);