]> git.basschouten.com Git - openhab-addons.git/blob
1edc64e7ac0115c5b1ed13fcd91ca348b33c7106
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2024 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.serial.internal.channel;
14
15 import java.util.Optional;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.serial.internal.transform.ValueTransformationProvider;
19 import org.openhab.core.library.types.OnOffType;
20 import org.openhab.core.types.Command;
21
22 /**
23  * The {@link SwitchChannel} channel provides mappings for the ON and OFF commands
24  *
25  * @author Mike Major - Initial contribution
26  */
27 @NonNullByDefault
28 public class SwitchChannel extends DeviceChannel {
29
30     public SwitchChannel(final ValueTransformationProvider valueTransformationProvider, final ChannelConfig config) {
31         super(valueTransformationProvider, config);
32     }
33
34     @Override
35     public Optional<String> mapCommand(final Command command) {
36         String data;
37
38         final String onValue = config.onValue;
39         final String offValue = config.offValue;
40
41         if (onValue != null && OnOffType.ON.equals(command)) {
42             data = onValue;
43         } else if (offValue != null && OnOffType.OFF.equals(command)) {
44             data = offValue;
45         } else {
46             data = command.toFullString();
47         }
48
49         final Optional<String> result = transformCommand(data);
50
51         logger.debug("Mapped command is '{}'", result.orElse(null));
52
53         return result;
54     }
55 }