2 * Copyright (c) 2010-2022 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 java.util.HashMap;
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;
33 * The {@link ElroConnectsDeviceHandler} represents the thing handler for an ELRO Connects device.
35 * @author Mark Herwege - Initial contribution
38 public class ElroConnectsDeviceHandler extends BaseThingHandler {
40 protected int deviceId;
42 public ElroConnectsDeviceHandler(Thing thing) {
47 public void initialize() {
48 ElroConnectsDeviceConfiguration config = getConfigAs(ElroConnectsDeviceConfiguration.class);
49 deviceId = config.deviceId;
51 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
53 if (bridgeHandler != null) {
54 bridgeHandler.setDeviceHandler(deviceId, this);
55 updateProperties(bridgeHandler);
56 updateDeviceName(bridgeHandler);
57 refreshChannels(bridgeHandler);
62 public void dispose() {
63 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
65 if (bridgeHandler != null) {
66 bridgeHandler.unsetDeviceHandler(deviceId, this);
71 * Get the bridge handler for this thing handler.
73 * @return {@link ElroConnectsBridgeHandler}, null if no bridge handler set
75 protected @Nullable ElroConnectsBridgeHandler getBridgeHandler() {
76 Bridge bridge = getBridge();
78 String msg = String.format("@text/offline.no-bridge [ \"%d\" ]", deviceId);
79 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, msg);
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);
94 public void handleCommand(ChannelUID channelUID, Command command) {
95 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
96 if (bridgeHandler != null) {
97 if (command instanceof RefreshType) {
98 refreshChannels(bridgeHandler);
104 * Update thing properties.
106 * @param bridgeHandler
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);
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);
126 * Refresh all thing channels.
128 * @param bridgeHandler
130 protected void refreshChannels(ElroConnectsBridgeHandler bridgeHandler) {
131 ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
132 if (device != null) {
133 device.updateState();
138 public void updateState(String channelID, State state) {
139 super.updateState(channelID, state);
143 public void updateStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail,
144 @Nullable String description) {
145 super.updateStatus(thingStatus, thingStatusDetail, description);
149 public void updateStatus(ThingStatus thingStatus) {
150 super.updateStatus(thingStatus);
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.
157 public void triggerAlarm() {
158 // nothing by default