]> git.basschouten.com Git - openhab-addons.git/blob
249d1c03b6c9a9c91f45022dbc10c893f2ce2333
[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_OCCUPANCYSTATUS;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.lutron.internal.protocol.DeviceCommand;
19 import org.openhab.binding.lutron.internal.protocol.lip.LutronCommandType;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  *
32  * @author Allan Tong - Initial contribution
33  * @author Bob Adair - Added initDeviceState method
34  */
35 @NonNullByDefault
36 public class OccupancySensorHandler extends LutronHandler {
37     private final Logger logger = LoggerFactory.getLogger(OccupancySensorHandler.class);
38
39     private int integrationId;
40
41     public OccupancySensorHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     public void handleCommand(ChannelUID channelUID, Command command) {
47     }
48
49     @Override
50     public void initialize() {
51         Number id = (Number) getThing().getConfiguration().get("integrationId");
52         if (id == null) {
53             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No integrationId");
54             return;
55         }
56         integrationId = id.intValue();
57         logger.debug("Initializing Occupancy Sensor handler for integration ID {}", id);
58
59         initDeviceState();
60     }
61
62     @Override
63     protected void initDeviceState() {
64         logger.debug("Initializing device state for Occupancy Sensor {}", getIntegrationId());
65         Bridge bridge = getBridge();
66         if (bridge == null) {
67             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
68         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
69             updateStatus(ThingStatus.ONLINE); // can't poll this device, so assume it is online if the bridge is
70         } else {
71             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
72         }
73     }
74
75     @Override
76     public int getIntegrationId() {
77         return this.integrationId;
78     }
79
80     @Override
81     public void handleUpdate(LutronCommandType type, String... parameters) {
82         if (type == LutronCommandType.DEVICE && parameters.length == 2
83                 && DeviceCommand.OCCUPIED_STATE_COMPONENT.equals(parameters[0])) {
84             if (DeviceCommand.STATE_OCCUPIED.equals(parameters[1])) {
85                 updateState(CHANNEL_OCCUPANCYSTATUS, OnOffType.ON);
86             } else if (DeviceCommand.STATE_UNOCCUPIED.equals(parameters[1])) {
87                 updateState(CHANNEL_OCCUPANCYSTATUS, OnOffType.OFF);
88             }
89         }
90     }
91 }