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.lcn.internal;
15 import java.math.BigDecimal;
16 import java.util.Optional;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.openhab.binding.lcn.internal.common.DimmerOutputCommand;
20 import org.openhab.binding.lcn.internal.common.LcnDefs;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.thing.profiles.ProfileCallback;
24 import org.openhab.core.thing.profiles.ProfileContext;
25 import org.openhab.core.thing.profiles.ProfileTypeUID;
26 import org.openhab.core.thing.profiles.StateProfile;
27 import org.openhab.core.types.Command;
28 import org.openhab.core.types.State;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * A profile to control multiple dimmer outputs simultaneously with ramp.
35 * @author Fabian Wolter - Initial Contribution
38 public class DimmerOutputProfile implements StateProfile {
39 private final Logger logger = LoggerFactory.getLogger(DimmerOutputProfile.class);
40 /** The Profile's UID */
41 static final ProfileTypeUID UID = new ProfileTypeUID(LcnBindingConstants.BINDING_ID, "output");
42 private final ProfileCallback callback;
44 private boolean controlAllOutputs;
45 private boolean controlOutputs12;
47 public DimmerOutputProfile(ProfileCallback callback, ProfileContext profileContext) {
48 this.callback = callback;
50 Optional<Object> ramp = getConfig(profileContext, "ramp");
51 Optional<Object> allOutputs = getConfig(profileContext, "controlAllOutputs");
52 Optional<Object> outputs12 = getConfig(profileContext, "controlOutputs12");
55 if (b instanceof BigDecimal decimalValue) {
56 rampMs = (int) (decimalValue.doubleValue() * 1000);
58 logger.warn("Could not parse 'ramp', unexpected type, should be float: {}", ramp);
62 allOutputs.ifPresent(b -> {
63 if (b instanceof Boolean) {
64 controlAllOutputs = true;
66 logger.warn("Could not parse 'controlAllOutputs', unexpected type, should be true/false: {}", b);
70 outputs12.ifPresent(b -> {
71 if (b instanceof Boolean) {
72 controlOutputs12 = true;
74 logger.warn("Could not parse 'controlOutputs12', unexpected type, should be true/false: {}", b);
79 private Optional<Object> getConfig(ProfileContext profileContext, String key) {
80 return Optional.ofNullable(profileContext.getConfiguration().get(key));
84 public void onCommandFromItem(Command command) {
85 if (rampMs != 0 && rampMs != LcnDefs.FIXED_RAMP_MS && controlOutputs12) {
86 logger.warn("Unsupported 'ramp' setting. Will be forced to 250ms: {}", rampMs);
89 if (command instanceof DecimalType decimalCommand) {
90 value = decimalCommand.toBigDecimal();
91 } else if (command instanceof OnOffType onOffCommand) {
92 value = onOffCommand == OnOffType.ON ? BigDecimal.valueOf(100) : BigDecimal.ZERO;
94 logger.warn("Unsupported type: {}", command.toFullString());
97 callback.handleCommand(new DimmerOutputCommand(value, controlAllOutputs, controlOutputs12, rampMs));
101 public void onStateUpdateFromHandler(State state) {
102 callback.sendUpdate(state);
106 public ProfileTypeUID getProfileTypeUID() {
111 public void onCommandFromHandler(Command command) {
116 public void onStateUpdateFromItem(State state) {