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.cm11a.internal.handler;
15 import java.io.IOException;
17 import org.openhab.binding.cm11a.internal.InvalidAddressException;
18 import org.openhab.binding.cm11a.internal.X10Interface;
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.types.Command;
25 import org.openhab.core.types.RefreshType;
26 import org.openhab.core.types.State;
27 import org.openhab.core.types.UnDefType;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
32 * Handler for Appliance (also called Switch) modules. These modules only support ON and OFF states
34 * @author Bob Raker - Initial contribution
37 public class Cm11aApplianceHandler extends Cm11aAbstractHandler {
39 private final Logger logger = LoggerFactory.getLogger(Cm11aApplianceHandler.class);
41 private State desiredState = UnDefType.UNDEF;
43 public Cm11aApplianceHandler(Thing thing) {
48 public void handleCommand(ChannelUID channelUID, Command command) {
49 logger.debug("**** Cm11aApplianceHandler handleCommand command = {}, channelUID = {}", command,
50 channelUID.getAsString());
53 Bridge bridge = getBridge();
55 logger.debug("Unable to handle command. Bridge is null.");
58 this.channelUID = channelUID;
60 Cm11aBridgeHandler cm11aHandler = (Cm11aBridgeHandler) bridge.getHandler();
61 if (cm11aHandler != null && cm11aHandler.getThing().getStatus().equals(ThingStatus.ONLINE)) {
62 if (command == OnOffType.ON) {
63 x10Function = X10Interface.FUNC_ON;
64 desiredState = OnOffType.ON;
65 } else if (command == OnOffType.OFF) {
66 x10Function = X10Interface.FUNC_OFF;
67 desiredState = OnOffType.OFF;
68 } else if (command instanceof RefreshType) {
69 x10Function = X10Interface.FUNC_OFF;
70 desiredState = OnOffType.OFF;
71 logger.info("Received REFRESH command for switch {}", houseUnitCode);
74 if (x10Function > 0) {
75 X10Interface x10Interface = cm11aHandler.getX10Interface();
76 x10Interface.scheduleHWUpdate(this);
78 logger.debug("Received invalid command for switch {} command: {}", houseUnitCode, command);
81 logger.debug("Attempted to change switch to {} for {} because the cm11a is not online", command,
87 public void updateHardware(X10Interface x10Interface) throws IOException, InvalidAddressException {
88 if (!desiredState.equals(currentState)) {
89 if (x10Interface.sendFunction(houseUnitCode, x10Function)) {
90 // Hardware update was successful so update openHAB
91 updateState(channelUID, desiredState);
92 setCurrentState(desiredState);