]> git.basschouten.com Git - openhab-addons.git/blob
4dfaa1af9c17f632c1dd73960ae9f72e094d37df
[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_OCCUPANCYSTATUS;
16
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;
28
29 /**
30  *
31  * @author Allan Tong - Initial contribution
32  * @author Bob Adair - Added initDeviceState method
33  */
34 @NonNullByDefault
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";
39
40     private final Logger logger = LoggerFactory.getLogger(OccupancySensorHandler.class);
41
42     private int integrationId;
43
44     public OccupancySensorHandler(Thing thing) {
45         super(thing);
46     }
47
48     @Override
49     public void handleCommand(ChannelUID channelUID, Command command) {
50     }
51
52     @Override
53     public void initialize() {
54         Number id = (Number) getThing().getConfiguration().get("integrationId");
55         if (id == null) {
56             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId");
57             return;
58         }
59         integrationId = id.intValue();
60         logger.debug("Initializing Occupancy Sensor handler for integration ID {}", id);
61
62         initDeviceState();
63     }
64
65     @Override
66     protected void initDeviceState() {
67         logger.debug("Initializing device state for Occupancy Sensor {}", getIntegrationId());
68         Bridge bridge = getBridge();
69         if (bridge == null) {
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
73         } else {
74             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
75         }
76     }
77
78     @Override
79     public int getIntegrationId() {
80         return this.integrationId;
81     }
82
83     @Override
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);
90             }
91         }
92     }
93 }