]> git.basschouten.com Git - openhab-addons.git/blob
5fabec84ed8d2b4e68b1d756aaaa8a193c83375f
[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.somfymylink.internal.handler;
14
15 import static org.openhab.binding.somfymylink.internal.SomfyMyLinkBindingConstants.CHANNEL_SHADELEVEL;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.StopMoveType;
19 import org.openhab.core.library.types.UpDownType;
20 import org.openhab.core.thing.Bridge;
21 import org.openhab.core.thing.ChannelUID;
22 import org.openhab.core.thing.Thing;
23 import org.openhab.core.thing.ThingStatus;
24 import org.openhab.core.thing.ThingStatusDetail;
25 import org.openhab.core.thing.ThingStatusInfo;
26 import org.openhab.core.thing.binding.BaseThingHandler;
27 import org.openhab.core.thing.binding.BridgeHandler;
28 import org.openhab.core.types.Command;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * The {@link SomfyShadeHandler} is responsible for handling commands for shades
34  *
35  * @author Chris Johnson - Initial contribution
36  */
37 @NonNullByDefault
38 public class SomfyShadeHandler extends BaseThingHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(SomfyShadeHandler.class);
41
42     public SomfyShadeHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     public void initialize() {
48         initDeviceState();
49     }
50
51     @Override
52     public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
53         logger.info("Bridge status changed to {} updating {}", bridgeStatusInfo.getStatus(), getThing().getLabel());
54
55         if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
56                 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
57             initDeviceState();
58         } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
59             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
60         }
61     }
62
63     public void initDeviceState() {
64         Bridge bridge = getBridge();
65
66         if (bridge == null) {
67             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.CONFIGURATION_ERROR, "No bridge configured");
68             logger.debug("Initialized device state for shade {} {}", ThingStatus.OFFLINE,
69                     ThingStatusDetail.CONFIGURATION_ERROR);
70         } else if (bridge.getStatus() == ThingStatus.ONLINE) {
71             updateStatus(ThingStatus.ONLINE);
72             logger.debug("Initialized device state for shade {}", ThingStatus.ONLINE);
73         } else {
74             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
75             logger.debug("Initialized device state for shade {} {}", ThingStatus.OFFLINE,
76                     ThingStatusDetail.BRIDGE_OFFLINE);
77         }
78     }
79
80     @Override
81     public void handleCommand(ChannelUID channelUID, Command command) {
82         try {
83             if (CHANNEL_SHADELEVEL.equals(channelUID.getId())) {
84                 String targetId = channelUID.getThingUID().getId();
85
86                 if (command instanceof UpDownType) {
87                     if (command.equals(UpDownType.DOWN)) {
88                         getBridgeHandler().commandShadeDown(targetId);
89                     } else {
90                         getBridgeHandler().commandShadeUp(targetId);
91                     }
92                 }
93
94                 if (command instanceof StopMoveType) {
95                     getBridgeHandler().commandShadeStop(targetId);
96                 }
97             }
98         } catch (SomfyMyLinkException e) {
99             logger.warn("Error handling command: {}", e.getMessage());
100             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
101         }
102     }
103
104     protected SomfyMyLinkBridgeHandler getBridgeHandler() {
105         Bridge bridge = this.getBridge();
106         if (bridge == null) {
107             throw new SomfyMyLinkException("No bridge was found");
108         }
109
110         BridgeHandler handler = bridge.getHandler();
111         if (handler == null) {
112             throw new SomfyMyLinkException("No handler was found");
113         }
114
115         return (SomfyMyLinkBridgeHandler) handler;
116     }
117 }