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_SCENECONTROL;
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;
31 * The {@link SomfySceneHandler} is responsible for handling commands for scenes
33 * @author Chris Johnson - Initial contribution
36 public class SomfySceneHandler extends BaseThingHandler {
38 private final Logger logger = LoggerFactory.getLogger(SomfySceneHandler.class);
40 public SomfySceneHandler(Thing thing) {
45 public void initialize() {
46 updateStatus(ThingStatus.ONLINE);
50 public void handleCommand(ChannelUID channelUID, Command command) {
52 if (CHANNEL_SCENECONTROL.equals(channelUID.getId()) && command instanceof OnOffType) {
53 Integer targetId = Integer.decode(channelUID.getThingUID().getId());
55 if (command.equals(OnOffType.ON)) {
56 getBridgeHandler().commandScene(targetId);
57 updateState(channelUID, OnOffType.OFF);
60 } catch (SomfyMyLinkException e) {
61 logger.warn("Error handling command: {}", e.getMessage());
62 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR, e.getMessage());
66 protected SomfyMyLinkBridgeHandler getBridgeHandler() {
67 Bridge bridge = this.getBridge();
69 throw new SomfyMyLinkException("No bridge was found");
72 BridgeHandler handler = bridge.getHandler();
73 if (handler == null) {
74 throw new SomfyMyLinkException("No handler was found");
77 return (SomfyMyLinkBridgeHandler) handler;