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 refreshChannels(bridgeHandler);
61 public void dispose() {
62 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
64 if (bridgeHandler != null) {
65 bridgeHandler.unsetDeviceHandler(deviceId, this);
70 * Get the bridge handler for this thing handler.
72 * @return {@link ElroConnectsBridgeHandler}, null if no bridge handler set
74 protected @Nullable ElroConnectsBridgeHandler getBridgeHandler() {
75 Bridge bridge = getBridge();
77 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR,
78 "No bridge defined for device " + String.valueOf(deviceId));
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));
93 public void handleCommand(ChannelUID channelUID, Command command) {
94 ElroConnectsBridgeHandler bridgeHandler = getBridgeHandler();
95 if (bridgeHandler != null) {
96 if (command instanceof RefreshType) {
97 refreshChannels(bridgeHandler);
103 * Update thing properties.
105 * @param bridgeHandler
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);
117 * Refresh all thing channels.
119 * @param bridgeHandler
121 protected void refreshChannels(ElroConnectsBridgeHandler bridgeHandler) {
122 ElroConnectsDevice device = bridgeHandler.getDevice(deviceId);
123 if (device != null) {
124 device.updateState();
129 public void updateState(String channelID, State state) {
130 super.updateState(channelID, state);
134 public void updateStatus(ThingStatus thingStatus, ThingStatusDetail thingStatusDetail,
135 @Nullable String description) {
136 super.updateStatus(thingStatus, thingStatusDetail, description);
140 public void updateStatus(ThingStatus thingStatus) {
141 super.updateStatus(thingStatus);
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.
148 public void triggerAlarm() {
149 // nothing by default