2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.elroconnects.internal.handler;
15 import static org.openhab.binding.elroconnects.internal.ElroConnectsBindingConstants.*;
17 import java.util.HashMap;
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;
37 * The {@link ElroConnectsDeviceHandler} represents the thing handler for an ELRO Connects device.
39 * @author Mark Herwege - Initial contribution
42 public class ElroConnectsDeviceHandler extends BaseThingHandler {
44 protected int deviceId;
46 public ElroConnectsDeviceHandler(Thing thing) {
51 public void initialize() {
52 ElroConnectsDeviceConfiguration config = getConfigAs(ElroConnectsDeviceConfiguration.class);
53 deviceId = config.deviceId;
55 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
56 if (bridgeHandler == null) {
57 // Thing status has already been updated in getBridgeHandler()
61 if (bridgeHandler.getThing().getStatus() == ThingStatus.ONLINE) {
62 ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
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);
69 bridgeHandler.setDeviceHandler(deviceId, this);
70 updateProperties(bridgeHandler);
71 updateDeviceName(bridgeHandler);
72 refreshChannels(bridgeHandler);
75 String msg = String.format("@text/offline.invalid-device-id [ \"%d\" ]", deviceId);
76 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
84 public void dispose() {
85 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
87 if (bridgeHandler != null) {
88 bridgeHandler.unsetDeviceHandler(deviceId, this);
93 * Get the bridge handler for this thing handler.
95 * @return {@link ElroConnectsBridgeHandler}, null if no bridge handler set
97 protected @Nullable ElroConnectsBridgeHandler getBridgeHandler() {
98 Bridge bridge = getBridge();
100 String msg = String.format("@text/offline.no-bridge [ \"%d\" ]", deviceId);
101 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
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);
112 return bridgeHandler;
116 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
117 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE) {
120 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
125 public void handleCommand(ChannelUID channelUID, Command command) {
126 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
127 if (bridgeHandler != null) {
128 if (command instanceof RefreshType) {
129 refreshChannels(bridgeHandler);
135 * Update thing properties.
137 * @param bridgeHandler
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);
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);
157 * Refresh all thing channels.
159 * @param bridgeHandler
161 protected void refreshChannels(ElroConnectsBridgeHandler bridgeHandler) {
162 ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
163 if (device != null) {
164 device.updateState();
169 public void updateState(String channelID, State state) {
170 super.updateState(channelID, state);
174 public void updateStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail,
175 @Nullable String description) {
176 super.updateStatus(thingStatus, thingStatusDetail, description);
180 public void updateStatus(ThingStatus thingStatus) {
181 super.updateStatus(thingStatus);
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.
188 public void triggerAlarm() {
189 // nothing by default