]> git.basschouten.com Git - openhab-addons.git/blob
1e84e5dad5479fbd116bf5d25ff966532c5b15cd
[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.pilight.internal.handler;
14
15 import static org.openhab.binding.pilight.internal.PilightBindingConstants.CHANNEL_STATE;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.pilight.internal.dto.Action;
20 import org.openhab.binding.pilight.internal.dto.Code;
21 import org.openhab.binding.pilight.internal.dto.Device;
22 import org.openhab.binding.pilight.internal.dto.Status;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.thing.ChannelUID;
25 import org.openhab.core.thing.Thing;
26 import org.openhab.core.types.Command;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * The {@link PilightSwitchHandler} is responsible for handling commands, which are
32  * sent to one of the channels.
33  *
34  * @author Stefan Röllin - Initial contribution
35  * @author Niklas Dörfler - Port pilight binding to openHAB 3 + add device discovery
36  */
37 @NonNullByDefault
38 public class PilightSwitchHandler extends PilightBaseHandler {
39
40     private final Logger logger = LoggerFactory.getLogger(PilightSwitchHandler.class);
41
42     public PilightSwitchHandler(Thing thing) {
43         super(thing);
44     }
45
46     @Override
47     protected void updateFromStatus(Status status) {
48         String state = status.getValues().get("state");
49         if (state != null) {
50             updateState(CHANNEL_STATE, OnOffType.valueOf(state.toUpperCase()));
51         }
52     }
53
54     @Override
55     void updateFromConfigDevice(Device device) {
56     }
57
58     @Override
59     protected @Nullable Action createUpdateCommand(ChannelUID unused, Command command) {
60         if (command instanceof OnOffType) {
61             Code code = new Code();
62             code.setDevice(getName());
63             code.setState(command.equals(OnOffType.ON) ? Code.STATE_ON : Code.STATE_OFF);
64
65             Action action = new Action(Action.ACTION_CONTROL);
66             action.setCode(code);
67             return action;
68         }
69
70         logger.warn("A pilight switch only accepts OnOffType commands.");
71         return null;
72     }
73 }