]> git.basschouten.com Git - openhab-addons.git/blob
79ace7284f9586a202c1cf54d866a5b637188e14
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2022 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.elroconnects.internal.handler;
14
15 import java.util.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.openhab.binding.elroconnects.internal.devices.ElroConnectsDevice;
21 import org.openhab.binding.elroconnects.internal.util.ElroConnectsUtil;
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.thing.binding.BaseThingHandler;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.openhab.core.types.State;
31
32 /**
33  * The {@link ElroConnectsDeviceHandler} represents the thing handler for an ELRO Connects device.
34  *
35  * @author Mark Herwege - Initial contribution
36  */
37 @NonNullByDefault
38 public class ElroConnectsDeviceHandler extends BaseThingHandler {
39
40     protected int deviceId;
41
42     public ElroConnectsDeviceHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public void initialize() {
48         ElroConnectsDeviceConfiguration config = getConfigAs(ElroConnectsDeviceConfiguration.class);
49         deviceId = config.deviceId;
50
51         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
52
53         if (bridgeHandler != null) {
54             bridgeHandler.setDeviceHandler(deviceId, this);
55             updateProperties(bridgeHandler);
56             refreshChannels(bridgeHandler);
57         }
58     }
59
60     @Override
61     public void dispose() {
62         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
63
64         if (bridgeHandler != null) {
65             bridgeHandler.unsetDeviceHandler(deviceId, this);
66         }
67     }
68
69     /**
70      * Get the bridge handler for this thing handler.
71      *
72      * @return {@link ElroConnectsBridgeHandler}, null if no bridge handler set
73      */
74     protected @Nullable ElroConnectsBridgeHandler getBridgeHandler() {
75         Bridge bridge = getBridge();
76         if (bridge == null) {
77             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
78                     "No bridge defined for device " + String.valueOf(deviceId));
79             return null;
80         }
81
82         ElroConnectsBridgeHandler bridgeHandler = (ElroConnectsBridgeHandler) bridge.getHandler();
83         if (bridgeHandler == null) {
84             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
85                     "No bridge handler defined for device " + String.valueOf(deviceId));
86             return null;
87         }
88
89         return bridgeHandler;
90     }
91
92     @Override
93     public void handleCommand(ChannelUID channelUID, Command command) {
94         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
95         if (bridgeHandler != null) {
96             if (command instanceof RefreshType) {
97                 refreshChannels(bridgeHandler);
98             }
99         }
100     }
101
102     /**
103      * Update thing properties.
104      *
105      * @param bridgeHandler
106      */
107     protected void updateProperties(ElroConnectsBridgeHandler bridgeHandler) {
108         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
109         if (device != null) {
110             Map<String, String> properties = new HashMap<>();
111             properties.put("deviceType", ElroConnectsUtil.stringOrEmpty(device.getDeviceType()));
112             thing.setProperties(properties);
113         }
114     }
115
116     /**
117      * Refresh all thing channels.
118      *
119      * @param bridgeHandler
120      */
121     protected void refreshChannels(ElroConnectsBridgeHandler bridgeHandler) {
122         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
123         if (device != null) {
124             device.updateState();
125         }
126     }
127
128     @Override
129     public void updateState(String channelID, State state) {
130         super.updateState(channelID, state);
131     }
132
133     @Override
134     public void updateStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail,
135             @Nullable String description) {
136         super.updateStatus(thingStatus, thingStatusDetail, description);
137     }
138
139     @Override
140     public void updateStatus(ThingStatus thingStatus) {
141         super.updateStatus(thingStatus);
142     }
143
144     /**
145      * Method to be called when an alarm event is received from the K1 hub. This should trigger a trigger channel. The
146      * method should be implemented in subclasses for the appropriate trigger channel if it applies.
147      */
148     public void triggerAlarm() {
149         // nothing by default
150     }
151 }