]> git.basschouten.com Git - openhab-addons.git/blob
e55f7ea24a613acb4de75bedb53d4e966b5273d6
[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             updateDeviceName(bridgeHandler);
57             refreshChannels(bridgeHandler);
58         }
59     }
60
61     @Override
62     public void dispose() {
63         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
64
65         if (bridgeHandler != null) {
66             bridgeHandler.unsetDeviceHandler(deviceId, this);
67         }
68     }
69
70     /**
71      * Get the bridge handler for this thing handler.
72      *
73      * @return {@link ElroConnectsBridgeHandler}, null if no bridge handler set
74      */
75     protected @Nullable ElroConnectsBridgeHandler getBridgeHandler() {
76         Bridge bridge = getBridge();
77         if (bridge == null) {
78             String msg = String.format("@text/offline.no-bridge [ \"%d\" ]", deviceId);
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
80             return null;
81         }
82
83         ElroConnectsBridgeHandler bridgeHandler = (ElroConnectsBridgeHandler) bridge.getHandler();
84         if (bridgeHandler == null) {
85             String msg = String.format("@text/offline.no-bridge-handler [ \"%d\" ]", deviceId);
86             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
87             return null;
88         }
89
90         return bridgeHandler;
91     }
92
93     @Override
94     public void handleCommand(ChannelUID channelUID, Command command) {
95         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
96         if (bridgeHandler != null) {
97             if (command instanceof RefreshType) {
98                 refreshChannels(bridgeHandler);
99             }
100         }
101     }
102
103     /**
104      * Update thing properties.
105      *
106      * @param bridgeHandler
107      */
108     protected void updateProperties(ElroConnectsBridgeHandler bridgeHandler) {
109         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
110         if (device != null) {
111             Map<String, String> properties = new HashMap<>();
112             properties.put("deviceType", ElroConnectsUtil.stringOrEmpty(device.getDeviceType()));
113             thing.setProperties(properties);
114         }
115     }
116
117     protected void updateDeviceName(ElroConnectsBridgeHandler bridgeHandler) {
118         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
119         String deviceName = thing.getLabel();
120         if ((device != null) && (deviceName != null)) {
121             device.updateDeviceName(deviceName);
122         }
123     }
124
125     /**
126      * Refresh all thing channels.
127      *
128      * @param bridgeHandler
129      */
130     protected void refreshChannels(ElroConnectsBridgeHandler bridgeHandler) {
131         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
132         if (device != null) {
133             device.updateState();
134         }
135     }
136
137     @Override
138     public void updateState(String channelID, State state) {
139         super.updateState(channelID, state);
140     }
141
142     @Override
143     public void updateStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail,
144             @Nullable String description) {
145         super.updateStatus(thingStatus, thingStatusDetail, description);
146     }
147
148     @Override
149     public void updateStatus(ThingStatus thingStatus) {
150         super.updateStatus(thingStatus);
151     }
152
153     /**
154      * Method to be called when an alarm event is received from the K1 hub. This should trigger a trigger channel. The
155      * method should be implemented in subclasses for the appropriate trigger channel if it applies.
156      */
157     public void triggerAlarm() {
158         // nothing by default
159     }
160 }