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.adorne.internal.handler;
15 import static org.openhab.binding.adorne.internal.AdorneBindingConstants.CHANNEL_POWER;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.adorne.internal.configuration.AdorneSwitchConfiguration;
19 import org.openhab.binding.adorne.internal.hub.AdorneHubController;
20 import org.openhab.core.library.types.OnOffType;
21 import org.openhab.core.thing.Bridge;
22 import org.openhab.core.thing.ChannelUID;
23 import org.openhab.core.thing.Thing;
24 import org.openhab.core.thing.ThingStatus;
25 import org.openhab.core.thing.ThingStatusDetail;
26 import org.openhab.core.thing.ThingStatusInfo;
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 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * The {@link AdorneSwitchHandler} is responsible for handling commands, which are
36 * sent to one of the channels.
38 * @author Mark Theiding - Initial contribution
41 public class AdorneSwitchHandler extends BaseThingHandler {
43 private final Logger logger = LoggerFactory.getLogger(AdorneSwitchHandler.class);
46 * The zone ID that represents this {@link AdorneSwitchHandler}'s thing
50 public AdorneSwitchHandler(Thing thing) {
55 * Handles refresh and on/off commands for channel
56 * {@link org.openhab.binding.adorne.internal.AdorneBindingConstants#CHANNEL_POWER}
59 public void handleCommand(ChannelUID channelUID, Command command) {
60 logger.trace("handleCommand (channelUID:{} command:{}", channelUID, command);
62 if (channelUID.getId().equals(CHANNEL_POWER)) {
63 if (command instanceof OnOffType) {
64 AdorneHubController adorneHubController = getAdorneHubController();
65 adorneHubController.setOnOff(zoneId, command.equals(OnOffType.ON));
66 } else if (command instanceof RefreshType) {
70 } catch (IllegalStateException e) {
71 // Hub controller could't handle our commands. Unfortunately the framework has no mechanism to report
72 // runtime errors. If we throw the exception up the framework logs it as an error - we don't want that - we
73 // want the framework to handle it gracefully. No point to update the thing status, since the
74 // AdorneHubController already does that. So we are forced to swallow the exception here.
75 logger.debug("Failed to execute command {} for channel {} for thing {} ({})", command, channelUID,
76 getThing().getLabel(), e.getMessage());
81 * Sets the handled thing to online.
84 public void initialize() {
85 logger.debug("Initializing switch {}", getThing().getLabel());
87 AdorneSwitchConfiguration config = getConfigAs(AdorneSwitchConfiguration.class);
88 Integer configZoneId = config.zoneId;
89 if (configZoneId != null) {
90 zoneId = configZoneId;
92 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
95 updateStatus(ThingStatus.ONLINE);
99 * Updates thing status in response to bridge status changes.
102 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
103 logger.trace("bridgeStatusChanged bridgeStatusInfo:{}", bridgeStatusInfo.getStatus());
104 if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
105 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
107 updateStatus(bridgeStatusInfo.getStatus());
112 * Returns the hub controller.
114 * @throws IllegalStateException if hub controller is not available yet.
116 protected AdorneHubController getAdorneHubController() {
118 AdorneHubHandler hubHandler;
119 AdorneHubController adorneHubController = null;
121 bridge = getBridge();
122 if (bridge != null) {
123 hubHandler = (AdorneHubHandler) bridge.getHandler();
124 if (hubHandler != null) {
125 adorneHubController = hubHandler.getAdorneHubController();
128 if (adorneHubController == null) {
129 throw new IllegalStateException("Hub Controller not available yet.");
131 return adorneHubController;
135 * Returns the zone ID that represents this {@link AdorneSwitchHandler}'s thing
139 public int getZoneId() {
144 * Refreshes the on/off state of our thing to the actual state of the device.
147 public void refreshOnOff() {
148 // Asynchronously get our onOff state from the hub controller and update our state accordingly
149 AdorneHubController adorneHubController = getAdorneHubController();
150 adorneHubController.getState(zoneId).thenAccept(state -> {
151 OnOffType onOffState = OnOffType.from(state.onOff);
152 updateState(CHANNEL_POWER, onOffState);
153 logger.debug("Refreshed switch {} with switch state {}", getThing().getLabel(), onOffState);
158 * Refreshes all supported channels.
161 public void refresh() {
166 * Provides a public version of updateState.
170 public void updateState(String channelID, State state) {
171 super.updateState(channelID, state);// Leverage our base class' protected method