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.luxom.internal.handler;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.luxom.internal.LuxomBindingConstants;
17 import org.openhab.binding.luxom.internal.protocol.LuxomAction;
18 import org.openhab.binding.luxom.internal.protocol.LuxomCommand;
19 import org.openhab.core.library.types.OnOffType;
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.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * The {@link LuxomSwitchHandler} is responsible for handling commands, which are
31 * sent to one of the channels.
33 * @author Kris Jespers - Initial contribution
36 public class LuxomSwitchHandler extends LuxomThingHandler {
37 private final Logger logger = LoggerFactory.getLogger(LuxomSwitchHandler.class);
39 public LuxomSwitchHandler(Thing thing) {
44 public void initialize() {
47 logger.debug("Initializing Switch handler for address {}", getAddress());
53 protected void initDeviceState() {
54 logger.debug("Initializing device state for Switch {}", getAddress());
55 Bridge bridge = getBridge();
57 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
58 } else if (ThingStatus.ONLINE.equals(bridge.getStatus())) {
59 updateStatus(ThingStatus.UNKNOWN, ThingStatusDetail.NONE, "@text/status.awaiting-initial-response");
60 ping(); // handleUpdate() will set thing status to online when response arrives
62 updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.BRIDGE_OFFLINE);
67 public void handleCommand(ChannelUID channelUID, Command command) {
68 logger.debug("switch at address {} received command {} for {}", getAddress(), command.toFullString(),
70 if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())) {
71 if (OnOffType.ON.equals(command)) {
73 ping(); // to make sure we know the current state
74 } else if (OnOffType.OFF.equals(command)) {
76 ping(); // to make sure we know the current state
82 public void handleCommandComingFromBridge(LuxomCommand command) {
83 if (LuxomAction.CLEAR_RESPONSE.equals(command.getAction())) {
84 updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.OFF);
85 updateStatus(ThingStatus.ONLINE);
86 } else if (LuxomAction.SET_RESPONSE.equals(command.getAction())) {
87 updateState(LuxomBindingConstants.CHANNEL_SWITCH, OnOffType.ON);
88 updateStatus(ThingStatus.ONLINE);
93 public void channelLinked(ChannelUID channelUID) {
94 logger.debug("switch at address {} linked to channel {}", getAddress(), channelUID);
95 if (LuxomBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())
96 || LuxomBindingConstants.CHANNEL_BRIGHTNESS.equals(channelUID.getId())) {
97 // Refresh state when new item is linked.