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.pilight.internal.handler;
15 import static org.openhab.binding.pilight.internal.PilightBindingConstants.CHANNEL_STATE;
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;
31 * The {@link PilightSwitchHandler} is responsible for handling commands, which are
32 * sent to one of the channels.
34 * @author Stefan Röllin - Initial contribution
35 * @author Niklas Dörfler - Port pilight binding to openHAB 3 + add device discovery
38 public class PilightSwitchHandler extends PilightBaseHandler {
40 private final Logger logger = LoggerFactory.getLogger(PilightSwitchHandler.class);
42 public PilightSwitchHandler(Thing thing) {
47 protected void updateFromStatus(Status status) {
48 String state = status.getValues().get("state");
50 updateState(CHANNEL_STATE, OnOffType.valueOf(state.toUpperCase()));
55 void updateFromConfigDevice(Device device) {
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);
65 Action action = new Action(Action.ACTION_CONTROL);
70 logger.warn("A pilight switch only accepts OnOffType commands.");