]> git.basschouten.com Git - openhab-addons.git/blob
7b975cf48584f02d462a01cbf71ea984ec78f5a5
[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.elroconnects.internal.handler;
14
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
16
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.ElroDeviceType;
23 import org.openhab.binding.elroconnects.internal.devices.ElroConnectsDevice;
24 import org.openhab.binding.elroconnects.internal.util.ElroConnectsUtil;
25 import org.openhab.core.thing.Bridge;
26 import org.openhab.core.thing.ChannelUID;
27 import org.openhab.core.thing.Thing;
28 import org.openhab.core.thing.ThingStatus;
29 import org.openhab.core.thing.ThingStatusDetail;
30 import org.openhab.core.thing.ThingStatusInfo;
31 import org.openhab.core.thing.binding.BaseThingHandler;
32 import org.openhab.core.types.Command;
33 import org.openhab.core.types.RefreshType;
34 import org.openhab.core.types.State;
35
36 /**
37  * The {@link ElroConnectsDeviceHandler} represents the thing handler for an ELRO Connects device.
38  *
39  * @author Mark Herwege - Initial contribution
40  */
41 @NonNullByDefault
42 public class ElroConnectsDeviceHandler extends BaseThingHandler {
43
44     protected int deviceId;
45
46     public ElroConnectsDeviceHandler(Thing thing) {
47         super(thing);
48     }
49
50     @Override
51     public void initialize() {
52         ElroConnectsDeviceConfiguration config = getConfigAs(ElroConnectsDeviceConfiguration.class);
53         deviceId = config.deviceId;
54
55         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
56         if (bridgeHandler == null) {
57             // Thing status has already been updated in getBridgeHandler()
58             return;
59         }
60
61         if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
62             ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
63             if (device != null) {
64                 ElroDeviceType deviceType = TYPE_MAP.get(device.getDeviceType());
65                 if ((deviceType == null) || !thing.getThingTypeUID().equals(THING_TYPE_MAP.get(deviceType))) {
66                     String msg = String.format("@text/offline.invalid-device-type [ \"%s\" ]", deviceType);
67                     updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
68                 } else {
69                     bridgeHandler.setDeviceHandler(deviceId, this);
70                     updateProperties(bridgeHandler);
71                     updateDeviceName(bridgeHandler);
72                     refreshChannels(bridgeHandler);
73                 }
74             } else {
75                 String msg = String.format("@text/offline.invalid-device-id [ \"%d\" ]", deviceId);
76                 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
77             }
78         } else {
79             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
80         }
81     }
82
83     @Override
84     public void dispose() {
85         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
86
87         if (bridgeHandler != null) {
88             bridgeHandler.unsetDeviceHandler(deviceId, this);
89         }
90     }
91
92     /**
93      * Get the bridge handler for this thing handler.
94      *
95      * @return {@link ElroConnectsBridgeHandler}, null if no bridge handler set
96      */
97     protected @Nullable ElroConnectsBridgeHandler getBridgeHandler() {
98         Bridge bridge = getBridge();
99         if (bridge == null) {
100             String msg = String.format("@text/offline.no-bridge [ \"%d\" ]", deviceId);
101             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
102             return null;
103         }
104
105         ElroConnectsBridgeHandler bridgeHandler = (ElroConnectsBridgeHandler) bridge.getHandler();
106         if (bridgeHandler == null) {
107             String msg = String.format("@text/offline.no-bridge-handler [ \"%d\" ]", deviceId);
108             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
109             return null;
110         }
111
112         return bridgeHandler;
113     }
114
115     @Override
116     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
117         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
118             initialize();
119         } else {
120             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
121         }
122     }
123
124     @Override
125     public void handleCommand(ChannelUID channelUID, Command command) {
126         ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
127         if (bridgeHandler != null) {
128             if (command instanceof RefreshType) {
129                 refreshChannels(bridgeHandler);
130             }
131         }
132     }
133
134     /**
135      * Update thing properties.
136      *
137      * @param bridgeHandler
138      */
139     protected void updateProperties(ElroConnectsBridgeHandler bridgeHandler) {
140         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
141         if (device != null) {
142             Map<String, String> properties = new HashMap<>();
143             properties.put("deviceType", ElroConnectsUtil.stringOrEmpty(device.getDeviceType()));
144             thing.setProperties(properties);
145         }
146     }
147
148     protected void updateDeviceName(ElroConnectsBridgeHandler bridgeHandler) {
149         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
150         String deviceName = thing.getLabel();
151         if ((device != null) && (deviceName != null)) {
152             device.updateDeviceName(deviceName);
153         }
154     }
155
156     /**
157      * Refresh all thing channels.
158      *
159      * @param bridgeHandler
160      */
161     protected void refreshChannels(ElroConnectsBridgeHandler bridgeHandler) {
162         ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
163         if (device != null) {
164             device.updateState();
165         }
166     }
167
168     @Override
169     public void updateState(String channelID, State state) {
170         super.updateState(channelID, state);
171     }
172
173     @Override
174     public void updateStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail,
175             @Nullable String description) {
176         super.updateStatus(thingStatus, thingStatusDetail, description);
177     }
178
179     @Override
180     public void updateStatus(ThingStatus thingStatus) {
181         super.updateStatus(thingStatus);
182     }
183
184     /**
185      * Method to be called when an alarm event is received from the K1 hub. This should trigger a trigger channel. The
186      * method should be implemented in subclasses for the appropriate trigger channel if it applies.
187      */
188     public void triggerAlarm() {
189         // nothing by default
190     }
191 }