]> git.basschouten.com Git - openhab-addons.git/blob
9f775234aaf5bcbe31ac10c081a68a568044aa7c
[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.lutron.internal.radiora.handler;
14
15 import org.eclipse.jdt.annotation.NonNullByDefault;
16 import org.openhab.binding.lutron.internal.LutronBindingConstants;
17 import org.openhab.binding.lutron.internal.radiora.config.SwitchConfig;
18 import org.openhab.binding.lutron.internal.radiora.protocol.LocalZoneChangeFeedback;
19 import org.openhab.binding.lutron.internal.radiora.protocol.RadioRAFeedback;
20 import org.openhab.binding.lutron.internal.radiora.protocol.SetSwitchLevelCommand;
21 import org.openhab.binding.lutron.internal.radiora.protocol.ZoneMapFeedback;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.ChannelUID;
24 import org.openhab.core.thing.Thing;
25 import org.openhab.core.types.Command;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * Handler for RadioRA switches
31  *
32  * @author Jeff Lauterbach - Initial Contribution
33  *
34  */
35 @NonNullByDefault
36 public class SwitchHandler extends LutronHandler {
37
38     private final Logger logger = LoggerFactory.getLogger(SwitchHandler.class);
39     private @NonNullByDefault({}) SwitchConfig config;
40
41     public SwitchHandler(Thing thing) {
42         super(thing);
43     }
44
45     @Override
46     public void initialize() {
47         config = getConfigAs(SwitchConfig.class);
48         super.initialize();
49     }
50
51     @Override
52     public void handleCommand(ChannelUID channelUID, Command command) {
53         RS232Handler bridgeHandler = getRS232Handler();
54         if (LutronBindingConstants.CHANNEL_SWITCH.equals(channelUID.getId())) {
55             if (command instanceof OnOffType) {
56                 SetSwitchLevelCommand cmd = new SetSwitchLevelCommand(config.getZoneNumber(), (OnOffType) command,
57                         config.system);
58
59                 if (bridgeHandler != null) {
60                     bridgeHandler.sendCommand(cmd);
61                 }
62             }
63         }
64     }
65
66     @Override
67     public void handleFeedback(RadioRAFeedback feedback) {
68         if (feedback instanceof LocalZoneChangeFeedback) {
69             handleLocalZoneChangeFeedback((LocalZoneChangeFeedback) feedback);
70         } else if (feedback instanceof ZoneMapFeedback) {
71             handleZoneMapFeedback((ZoneMapFeedback) feedback);
72         }
73     }
74
75     private void handleZoneMapFeedback(ZoneMapFeedback feedback) {
76         if (!systemsMatch(feedback.getSystem(), config.system)) {
77             return;
78         }
79         char value = feedback.getZoneValue(config.getZoneNumber());
80
81         if (value == '1') {
82             updateState(LutronBindingConstants.CHANNEL_SWITCH, OnOffType.ON);
83         } else if (value == '0') {
84             updateState(LutronBindingConstants.CHANNEL_SWITCH, OnOffType.OFF);
85         }
86     }
87
88     private void handleLocalZoneChangeFeedback(LocalZoneChangeFeedback feedback) {
89         if (systemsMatch(feedback.getSystem(), config.system) && feedback.getZoneNumber() == config.getZoneNumber()) {
90             if (LocalZoneChangeFeedback.State.CHG.equals(feedback.getState())) {
91                 logger.debug("Not Implemented Yet - CHG state received from Local Zone Change Feedback.");
92             }
93
94             updateState(LutronBindingConstants.CHANNEL_SWITCH, OnOffType.valueOf(feedback.getState().toString()));
95         }
96     }
97 }