]> git.basschouten.com Git - openhab-addons.git/blob
484641a7457b6db03e41d3ed95bdf795f9cdbe5b
[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_SCENECONTROL;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.core.library.types.OnOffType;
19 import org.openhab.core.thing.Bridge;
20 import org.openhab.core.thing.ChannelUID;
21 import org.openhab.core.thing.Thing;
22 import org.openhab.core.thing.ThingStatus;
23 import org.openhab.core.thing.ThingStatusDetail;
24 import org.openhab.core.thing.binding.BaseThingHandler;
25 import org.openhab.core.thing.binding.BridgeHandler;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link SomfySceneHandler} is responsible for handling commands for scenes
32  *
33  * @author Chris Johnson - Initial contribution
34  */
35 @NonNullByDefault
36 public class SomfySceneHandler extends BaseThingHandler {
37
38     private final Logger logger = LoggerFactory.getLogger(SomfySceneHandler.class);
39
40     public SomfySceneHandler(Thing thing) {
41         super(thing);
42     }
43
44     @Override
45     public void initialize() {
46         updateStatus(ThingStatus.ONLINE);
47     }
48
49     @Override
50     public void handleCommand(ChannelUID channelUID, Command command) {
51         try {
52             if (CHANNEL_SCENECONTROL.equals(channelUID.getId()) && command instanceof OnOffType) {
53                 Integer targetId = Integer.decode(channelUID.getThingUID().getId());
54
55                 if (command.equals(OnOffType.ON)) {
56                     getBridgeHandler().commandScene(targetId);
57                     updateState(channelUID, OnOffType.OFF);
58                 }
59             }
60         } catch (SomfyMyLinkException e) {
61             logger.warn("Error handling command: {}", e.getMessage());
62             updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
63         }
64     }
65
66     protected SomfyMyLinkBridgeHandler getBridgeHandler() {
67         Bridge bridge = this.getBridge();
68         if (bridge == null) {
69             throw new SomfyMyLinkException("No bridge was found");
70         }
71
72         BridgeHandler handler = bridge.getHandler();
73         if (handler == null) {
74             throw new SomfyMyLinkException("No handler was found");
75         }
76
77         return (SomfyMyLinkBridgeHandler) handler;
78     }
79 }