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.somfymylink.internal.handler;
15 import static org.openhab.binding.somfymylink.internal.SomfyMyLinkBindingConstants.CHANNEL_SHADELEVEL;
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;
33 * The {@link SomfyShadeHandler} is responsible for handling commands for shades
35 * @author Chris Johnson - Initial contribution
38 public class SomfyShadeHandler extends BaseThingHandler {
40 private final Logger logger = LoggerFactory.getLogger(SomfyShadeHandler.class);
42 public SomfyShadeHandler(Thing thing) {
47 public void initialize() {
52 public void bridgeStatusChanged(ThingStatusInfo bridgeStatusInfo) {
53 logger.info("Bridge status changed to {} updating {}", bridgeStatusInfo.getStatus(), getThing().getLabel());
55 if (bridgeStatusInfo.getStatus() == ThingStatus.ONLINE
56 && getThing().getStatusInfo().getStatusDetail() == ThingStatusDetail.BRIDGE_OFFLINE) {
58 } else if (bridgeStatusInfo.getStatus() == ThingStatus.OFFLINE) {
59 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
63 public void initDeviceState() {
64 Bridge bridge = getBridge();
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);
74 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
75 logger.debug("Initialized device state for shade {} {}", ThingStatus.OFFLINE,
76 ThingStatusDetail.BRIDGE_OFFLINE);
81 public void handleCommand(ChannelUID channelUID, Command command) {
83 if (CHANNEL_SHADELEVEL.equals(channelUID.getId())) {
84 String targetId = channelUID.getThingUID().getId();
86 if (command instanceof UpDownType) {
87 if (command.equals(UpDownType.DOWN)) {
88 getBridgeHandler().commandShadeDown(targetId);
90 getBridgeHandler().commandShadeUp(targetId);
94 if (command instanceof StopMoveType) {
95 getBridgeHandler().commandShadeStop(targetId);
98 } catch (SomfyMyLinkException e) {
99 logger.warn("Error handling command: {}", e.getMessage());
100 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
104 protected SomfyMyLinkBridgeHandler getBridgeHandler() {
105 Bridge bridge = this.getBridge();
106 if (bridge == null) {
107 throw new SomfyMyLinkException("No bridge was found");
110 BridgeHandler handler = bridge.getHandler();
111 if (handler == null) {
112 throw new SomfyMyLinkException("No handler was found");
115 return (SomfyMyLinkBridgeHandler) handler;