2 * Copyright (c) 2010-2024 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.lgwebos.internal;
15 import java.util.Optional;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.lgwebos.internal.handler.LGWebOSHandler;
20 import org.openhab.binding.lgwebos.internal.handler.command.ServiceSubscription;
21 import org.openhab.binding.lgwebos.internal.handler.core.CommandConfirmation;
22 import org.openhab.binding.lgwebos.internal.handler.core.ResponseListener;
23 import org.openhab.core.library.types.DecimalType;
24 import org.openhab.core.library.types.IncreaseDecreaseType;
25 import org.openhab.core.library.types.OnOffType;
26 import org.openhab.core.library.types.PercentType;
27 import org.openhab.core.library.types.StringType;
28 import org.openhab.core.types.Command;
29 import org.openhab.core.types.RefreshType;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Handles TV Control Volume Commands. Allows to set a volume to an absolute number or increment and decrement the
35 * volume. If used with On Off type commands it will mute volume when receiving OFF and unmute when receiving ON.
37 * @author Sebastian Prehn - initial contribution
40 public class VolumeControlVolume extends BaseChannelHandler<Float> {
41 private final Logger logger = LoggerFactory.getLogger(VolumeControlVolume.class);
43 private final ResponseListener<CommandConfirmation> objResponseListener = createResponseListener();
46 public void onReceiveCommand(String channelId, LGWebOSHandler handler, Command command) {
47 final PercentType percent;
48 if (RefreshType.REFRESH == command) {
49 handler.getSocket().getVolume(createResponseListener(channelId, handler));
52 if (command instanceof PercentType percentCommand) {
53 percent = percentCommand;
54 } else if (command instanceof DecimalType decimalCommand) {
55 percent = new PercentType(decimalCommand.toBigDecimal());
56 } else if (command instanceof StringType stringCommand) {
57 percent = new PercentType(stringCommand.toString());
62 if (percent != null) {
63 handler.getSocket().setVolume(percent.floatValue() / 100.0f, objResponseListener);
64 } else if (IncreaseDecreaseType.INCREASE == command) {
65 handler.getSocket().volumeUp(objResponseListener);
66 } else if (IncreaseDecreaseType.DECREASE == command) {
67 handler.getSocket().volumeDown(objResponseListener);
68 } else if (OnOffType.OFF == command || OnOffType.ON == command) {
69 handler.getSocket().setMute(OnOffType.OFF == command, objResponseListener);
71 logger.info("Only accept PercentType, DecimalType, StringType, RefreshType. Type was {}.",
77 protected Optional<ServiceSubscription<Float>> getSubscription(String channelUID, LGWebOSHandler handler) {
78 return Optional.of(handler.getSocket().subscribeVolume(createResponseListener(channelUID, handler)));
81 private ResponseListener<Float> createResponseListener(String channelUID, LGWebOSHandler handler) {
82 return new ResponseListener<Float>() {
85 public void onError(@Nullable String error) {
86 logger.debug("Error in retrieving volume: {}.", error);
90 public void onSuccess(@Nullable Float value) {
91 if (value != null && !Float.isNaN(value)) {
92 handler.postUpdate(channelUID, new PercentType(Math.round(value * 100)));