]> git.basschouten.com Git - openhab-addons.git/blob
6298a51423a3fb44df69c699d55dc6c4149f6b80
[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.adorne.internal.handler;
14
15 import static org.openhab.binding.adorne.internal.AdorneBindingConstants.CHANNEL_POWER;
16
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;
33
34 /**
35  * The {@link AdorneSwitchHandler} is responsible for handling commands, which are
36  * sent to one of the channels.
37  *
38  * @author Mark Theiding - Initial contribution
39  */
40 @NonNullByDefault
41 public class AdorneSwitchHandler extends BaseThingHandler {
42
43     private final Logger logger = LoggerFactory.getLogger(AdorneSwitchHandler.class);
44
45     /**
46      * The zone ID that represents this {@link AdorneSwitchHandler}'s thing
47      */
48     protected int zoneId;
49
50     public AdorneSwitchHandler(Thing thing) {
51         super(thing);
52     }
53
54     /**
55      * Handles refresh and on/off commands for channel
56      * {@link org.openhab.binding.adorne.internal.AdorneBindingConstants#CHANNEL_POWER}
57      */
58     @Override
59     public void handleCommand(ChannelUID channelUID, Command command) {
60         logger.trace("handleCommand (channelUID:{} command:{}", channelUID, command);
61         try {
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) {
67                     refreshOnOff();
68                 }
69             }
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());
77         }
78     }
79
80     /**
81      * Sets the handled thing to online.
82      */
83     @Override
84     public void initialize() {
85         logger.debug("Initializing switch {}", getThing().getLabel());
86
87         AdorneSwitchConfiguration config = getConfigAs(AdorneSwitchConfiguration.class);
88         Integer configZoneId = config.zoneId;
89         if (configZoneId != null) {
90             zoneId = configZoneId;
91         } else {
92             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR);
93             return;
94         }
95         updateStatus(ThingStatus.ONLINE);
96     }
97
98     /**
99      * Updates thing status in response to bridge status changes.
100      */
101     @Override
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);
106         } else {
107             updateStatus(bridgeStatusInfo.getStatus());
108         }
109     }
110
111     /**
112      * Returns the hub controller.
113      *
114      * @throws IllegalStateException if hub controller is not available yet.
115      */
116     protected AdorneHubController getAdorneHubController() {
117         Bridge bridge;
118         AdorneHubHandler hubHandler;
119         AdorneHubController adorneHubController = null;
120
121         bridge = getBridge();
122         if (bridge != null) {
123             hubHandler = (AdorneHubHandler) bridge.getHandler();
124             if (hubHandler != null) {
125                 adorneHubController = hubHandler.getAdorneHubController();
126             }
127         }
128         if (adorneHubController == null) {
129             throw new IllegalStateException("Hub Controller not available yet.");
130         }
131         return adorneHubController;
132     }
133
134     /**
135      * Returns the zone ID that represents this {@link AdorneSwitchHandler}'s thing
136      *
137      * @return zone ID
138      */
139     public int getZoneId() {
140         return zoneId;
141     }
142
143     /**
144      * Refreshes the on/off state of our thing to the actual state of the device.
145      *
146      */
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);
154         });
155     }
156
157     /**
158      * Refreshes all supported channels.
159      *
160      */
161     public void refresh() {
162         refreshOnOff();
163     }
164
165     /**
166      * Provides a public version of updateState.
167      *
168      */
169     @Override
170     public void updateState(String channelID, State state) {
171         super.updateState(channelID, state);// Leverage our base class' protected method
172     }
173 }